Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this program I am trying to run. I\'ve tried multiple things bu

ID: 3564010 • Letter: I

Question

I need help with this program I am trying to run. I've tried multiple things but the casting is causing me problems. Note the instructions/constraints specified in the the beginning. We just have to add certain parts without modifying anything else to make it run smoothly. Thanks for any help

/*************************************************************************************
*
* This class represents a fraction whose numerator and denominator are integers.
*
* Requirements:
*      Implement interfaces: FractionInterface and Comparable (i.e. compareTo())
*      Implement methods equals() and toString() from class Object
*
*      Should work for both positive and negative fractions
*      Must always reduce fraction to lowest term
*      For number such as 3/-10, it is same as -3/10 (see hints 2. below)
*      Must display negative fraction as -x/y,
*         example: (-3)/10 or 3/(-10), must display as -3/10
*      Must throw only FractionExcpetion in case of errors
*      Must not add new or modify existing data fields
*      Must not add new public methods
*      May add private methods
*
* Hints:
*
* 1. To reduce a fraction such as 4/8 to lowest terms, you need to divide both
*    the numerator and the denominator by their greatest common denominator.
*    The greatest common denominator of 4 and 8 is 4, so when you divide
*    the numerator and denominator of 4/8 by 4, you get the fraction 1/2.
*    The recursive algorithm which finds the greatest common denominator of
*    two positive integers is implemnted (see code)
*     
* 2. It will be easier to determine the correct sign of a fraction if you force
*    the fraction's denominator to be positive. However, your implementation must
*    handle negative denominators that the client might provide.
*         
* 3. You need to downcast reference parameter FractionInterface to Fraction if
*    you want to use it as Fraction. See add, subtract, multiply and divide methods
*
* 4. Use "this" to access this object if it is needed
*
************************************************************************************/

package PJ1;

public class Fraction implements FractionInterface, Comparable
{
   private   int num;   // Numerator  
   private   int den;   // Denominator  

   public Fraction()
   {
       // set fraction to default = 0/1
       setFraction(0, 1);
   }   // end default constructor

   public Fraction(int numerator, int denominator)
   {
       // implement this method!
   }   // end constructor

   public void setFraction(int numerator, int denominator)
   {
       // return FractionException if initialDenominator is 0
       // implement this method!
   }   // end setFraction

   public double toDouble()
   {
       // return double floating point value
       // implement this method!
       return 0.0;
   }   // end toDouble

   public FractionInterface add(FractionInterface secondFraction)
   {
       // a/b + c/d is (ad + cb)/(bd)
       // implement this method!
                return null;
   }   // end add

   public FractionInterface subtract(FractionInterface secondFraction)
   {
       // a/b - c/d is (ad - cb)/(bd)
       // implement this method!
                return null;
   }   // end subtract

   public FractionInterface multiply(FractionInterface secondFraction)
   {
       // a/b * c/d is (ac)/(bd)
       // implement this method!
                return null;
   }   // end multiply

   public FractionInterface divide(FractionInterface secondFraction)
   {
       // return FractionException if secondFraction is 0
       // a/b / c/d is (ad)/(bc)
       // implement this method!
                return null;
   }   // end divide

   public FractionInterface getReciprocal()
   {
       // return FractionException if secondFraction is 0
       // implement this method!
                return null;
   } // end getReciprocal


   public boolean equals(Object other)
   {
       // implement this method!
       return false;
   } // end equals


   public int compareTo(Fraction other)
   {
       // implement this method!
       return 0;
   } // end compareTo

  
   public String toString()
   {
       return num + "/" + den;
   } // end toString


   /** Task: Reduces a fraction to lowest terms. */

        //-----------------------------------------------------------------
        // private methods start here
        //-----------------------------------------------------------------

   private void reduceToLowestTerms()
   {
                // implement this method!
                //
                // Outline:
                // compute GCD of num & den
                // greatestCommonDivisor works for + numbers.
                // So, you should eliminate - sign
                // then reduce numbers : num/GCD and den/GCD
   }   // end reduceToLowestTerms

   /** Task: Computes the greatest common secondFraction of two integers.
   * @param integerOne   an integer
   * @param integerTwo   another integer
   * @return the greatest common divisor of the two integers */
   private int greatestCommonDivisor(int integerOne, int integerTwo)
   {
       int result;

       if (integerOne % integerTwo == 0)
           result = integerTwo;
       else
           result = greatestCommonDivisor(integerTwo, integerOne % integerTwo);

       return result;
   }   // end greatestCommonDivisor


   //-----------------------------------------------------------------
   // Simple test is provided here

   public static void main(String[] args)
   {
       FractionInterface firstOperand = null;
       FractionInterface secondOperand = null;
       FractionInterface result = null;
                double doubleResult = 0.0;

       Fraction nineSixteenths = new Fraction(9, 16);   // 9/16
       Fraction Fraction(1, 4);        // 1/4

       System.out.println(" =========================================");
       // 7/8 + 9/16
       firstOperand = new Fraction(7, 8);
       result = firstOperand.add(nineSixteenths);
       System.out.println("The sum of " + firstOperand + " and " +
               nineSixteenths + " is " + result);

       // 9/16 - 7/8
       firstOperand = nineSixteenths;
       secondOperand = new Fraction(7, 8);
       result = firstOperand.subtract(secondOperand);
       System.out.println("The difference of " + firstOperand   +
               " and " +   secondOperand + " is " + result);

       // 15/-2 * 1/4
       firstOperand.setFraction(15, -2);
       result = firstOperand.multiply(oneFourth);
       System.out.println("The product of " + firstOperand   +
               " and " +   oneFourth + " is " + result);

       // (-21/2) / (3/7)
       firstOperand.setFraction(-21, 2);
       secondOperand.setFraction(3, 7);
       result = firstOperand.divide(secondOperand);
       System.out.println("The quotient of " + firstOperand   +
               " and " +   secondOperand + " is " + result);

       // -21/2 + 7/8
       firstOperand.setFraction(-21, 2);
       secondOperand.setFraction(7, 8);
       result = firstOperand.add(secondOperand);
       System.out.println("The sum of " + firstOperand   +
               " and " +   secondOperand + " is " + result);


                // 0/10, 5/(-15), (-22)/7
       firstOperand.setFraction(0, 10);
                doubleResult = firstOperand.toDouble();
       System.out.println("The double floating point value of " + firstOperand   + " is " + doubleResult);
       firstOperand.setFraction(1, -3);
                doubleResult = firstOperand.toDouble();
       System.out.println("The double floating point value of " + firstOperand   + " is " + doubleResult);
       firstOperand.setFraction(-22, 7);
                doubleResult = firstOperand.toDouble();
       System.out.println("The double floating point value of " + firstOperand   + " is " + doubleResult);
       System.out.println(" =========================================");
       firstOperand.setFraction(-21, 2);
       System.out.println("First = " + firstOperand);
       // equality check
       System.out.println("check First equals First: ");
       if (firstOperand.equals(firstOperand))
           System.out.println("Identity of fractions OK");
       else
           System.out.println("ERROR in identity of fractions");

       secondOperand.setFraction(-42, 4);
       System.out.println(" Second = " + secondOperand);
       System.out.println("check First equals Second: ");
       if (firstOperand.equals(secondOperand))
           System.out.println("Equality of fractions OK");
       else
           System.out.println("ERROR in equality of fractions");

       // comparison check
       Fraction first = (Fraction)firstOperand;
       Fraction second = (Fraction)secondOperand;
      
       System.out.println(" check First compareTo Second: ");
       if (first.compareTo(second) == 0)
           System.out.println("Fractions == operator OK");
       else
           System.out.println("ERROR in fractions == operator");

       second.setFraction(7, 8);
       System.out.println(" Second = " + secondOperand);
       System.out.println("check First compareTo Second: ");
       if (first.compareTo(second) < 0)
           System.out.println("Fractions < operator OK");
       else
           System.out.println("ERROR in fractions < operator");

       System.out.println(" check Second compareTo First: ");
       if (second.compareTo(first) > 0)
           System.out.println("Fractions > operator OK");
       else
           System.out.println("ERROR in fractions > operator");

       System.out.println(" =========================================");

       System.out.println(" check FractionException: 1/0");
       try {
           Fraction a1 = new Fraction(1, 0);          
       }
       catch ( FractionException fe )
              {
                     System.err.printf( "Exception: %s ", fe );
              } // end catch

       System.out.println(" check FractionException: division");
       try {
           Fraction a2 = new Fraction();          
           Fraction a3 = new Fraction(1, 2);          
           a3.divide(a2);
       }
       catch ( FractionException fe )
              {
                     System.err.printf( "Exception: %s ", fe );
              } // end catch

   }   // end main
} // end Fraction

Here is the fraction exception file file

/************************************************************************************
* Do not modify this file.
* FractionException class. It is used by Fraction class
*************************************************************************************/

package PJ1;

public class FractionException extends RuntimeException
{
    public FractionException()
    {
   this("");
    }

    public FractionException(String errorMsg)
    {
   super(errorMsg);
    }

}

and the fraction interface file

/* This file specifies methods for FractionInterface       */
/* Do not modify this file!!                             */

package PJ1;

public interface FractionInterface
{
   /** Task: Sets a fraction to a given value.
   * @param numerator the integer numerator
   * @param denominator the integer denominator
         * @throws FractionException if denominator=0 */
   public void setFraction(int numerator, int denominator);

   /** Task: Convert a fraction to double value
   * @return the double floating point value of a fraction */
   public double toDouble();

   /** Task: Adds two fractions.
   * @param secondFraction a fraction that is the second operand of the addition
   * @return a fraction which is the sum of the invoking fraction and the secondFraction */
   public FractionInterface add(FractionInterface secondFraction);

   /** Task: Subtracts two fractions.
   * @param secondFraction a fraction that is the second operand of the subtraction
   * @return a fraction which is the difference of the invoking fraction and the second operand */
   public FractionInterface subtract(FractionInterface secondFraction);

   /** Task: Multiplies two fractions.
   * @param secondFraction a fraction that is the second operand of the multiplication
   * @return a fraction which is the product of the invoking fraction and the secondFraction*/
   public FractionInterface multiply(FractionInterface secondFraction);

   /** Task: Divides two fractions.
   * @param secondFraction a fraction that is the second operand of the division
   * @return a fraction which the quotient of the invoking fraction and the secondFraction
         * @throws FractionException if secondFraction=0 */
   public FractionInterface divide(FractionInterface secondFraction);

   /** Task: Get's the fraction's reciprocal
   * @return the reciprocal of the invoking fraction
         * @throws FractionException if the new number with denominator=0*/
   public FractionInterface getReciprocal();

}

Explanation / Answer

//Here is the modified Fraction class. Since FractionInterface need not to be modified.

//Modified code is highlighted(bold).Get back to me if you have any doubt.

public class Fraction implements FractionInterface, Comparable<Fraction>
{
   private   int num;   // Numerator
   private   int den;   // Denominator

   public Fraction()
   {
       // set fraction to default = 0/1
       setFraction(0, 1);
   }   // end default constructor

   public Fraction(int numerator, int denominator)
   {
       // implement this method!
    setFraction(numerator, denominator);
   }   // end constructor

   public void setFraction(int numerator, int denominator)
   {
       // return FractionException if initialDenominator is 0
       // implement this method!
    if(denominator==0)

     throw new ArithmeticException("Denominator can't be 0");


     // handle negative denominators to make it easy

     if(denominator<0){

     //swap the negative sign of denominator to numerator

     this.den = Math.abs(denominator);

     this.num = -numerator;

     }

     else

     {

     this.num = numerator;

     this.den = denominator;

   }
   }   // end setFraction

//Add new methods getNumerator and getDenominator()
   public int getNumerator()

   {

   // implement this method!

   return num;

   } // end getNumerator


   public int getDenominator()

   {

   // implement this method!

   return den;

   } // end getDenominator
   public double toDouble()
   {
       // return double floating point value
       // implement this method!
       return 0.0;
   }   // end toDouble

   public FractionInterface add(FractionInterface secondFraction)
   {
       // a/b + c/d is (ad + cb)/(bd)
       // implement this method!
    int resultDen;

    int resultNum;


    resultDen = this.getDenominator()*secondFraction.getDenominator();

    resultNum = (this.getNumerator()*secondFraction.getDenominator()())+(secondFraction.getNumerator()*this.getDenominator());


    Fraction sum = new Fraction(resultNum, resultDen);

    sum.reduceToLowestTerms();

    return sum;
               // return null;

   }   // end add

   public FractionInterface subtract(FractionInterface secondFraction)
   {
       // a/b - c/d is (ad - cb)/(bd)
       // implement this method!
                //return null;
    int resultDen;

    int resultNum;


    resultDen = (this.getDenominator())*(secondFraction.getDenominator());

    resultNum = (this.getNumerator()*secondFraction.getDenominator())-(secondFraction.getNumerator()*this.getDenominator());


    Fraction difference = new Fraction(resultNum, resultDen);

    difference.reduceToLowestTerms();

    return difference;
   }   // end subtract

   public FractionInterface multiply(FractionInterface secondFraction)
   {
       // a/b * c/d is (ac)/(bd)
       // implement this method!
                //return null;
// a/b * c/d is (ac)/(bd)

// implement this method!

int resultDen;

int resultNum;


resultNum = this.getNumerator()*secondFraction.getNumerator();

resultDen = this.getDenominator()*secondFraction.getDenominator();


Fraction product = new Fraction(resultNum, resultDen);

product.reduceToLowestTerms();

return product;
   }   // end multiply

   public FractionInterface divide(FractionInterface secondFraction)
   {
      // return FractionException if secondFraction is 0
       // a/b / c/d is (ad)/(bc)
       // implement this method!
                //return null;
// return ArithmeticException if secondFraction is 0


    if(secondFraction.getNumerator()==0)

    throw new ArithmeticException("Second fraction is 0");

    // a/b / c/d is (ad)/(bc)

    // implement this method!

    int resultDen;

    int resultNum;


    resultNum = this.getNumerator()*secondFraction.getDenominator();

    resultDen = this.getDenominator()*secondFraction.getNumerator();


    Fraction divideResult = new Fraction(resultNum, resultDen);

    divideResult.reduceToLowestTerms();

    return divideResult;
   }   // end divide

   public FractionInterface getReciprocal()
   {
       // return FractionException if secondFraction is 0
       // implement this method!
                //return null;
// return ArithmeticException if secondFraction is 0

    if(this.getNumerator()==0)

    throw new ArithmeticException("Reciprocal is not valid");

    // implement this method!

    return new Fraction(this.den, this.num);
   } // end getReciprocal


   public boolean equals(Object other)
   {
       // implement this method!
       //return false;
// implement this method!

    FractionInterface secondFraction = (FractionInterface) other;

    if(this.num==secondFraction.getNumerator() && this.den==secondFraction.getDenominator()){

    return true;

    }

    else

    return false;
   
   } // end equals


   public int compareTo(Fraction other)
   {
       // implement this method!
      // return 0;
// implement this method!

    double fraction = (double)num/(double)den;

    double otherFraction = (double)other.getNumerator()/(double)other.getDenominator();


    if(otherFraction==fraction)

    return 0;

    else

    return (int) (otherFraction-fraction);
   } // end compareTo


   public String toString()
   {
       return num + "/" + den;
   } // end toString


   /** Task: Reduces a fraction to lowest terms. */

        //-----------------------------------------------------------------
        // private methods start here
        //-----------------------------------------------------------------

   private void reduceToLowestTerms()
   {
                // implement this method!
                //
                // Outline:
                // compute GCD of num & den
                // greatestCommonDivisor works for + numbers.
                // So, you should eliminate - sign
                // then reduce numbers : num/GCD and den/GCD
   
    int gcd = greatestCommonDivisor(num, den);
    gcd = Math.abs(gcd);
    this.num = num/gcd;

    this.den = den/gcd;
   }   // end reduceToLowestTerms

   /** Task: Computes the greatest common secondFraction of two integers.
   * @param integerOne   an integer
   * @param integerTwo   another integer
   * @return the greatest common divisor of the two integers */
   private int greatestCommonDivisor(int integerOne, int integerTwo)
   {
       int result;

       if (integerOne % integerTwo == 0)
           result = integerTwo;
       else
           result = greatestCommonDivisor(integerTwo, integerOne % integerTwo);

       return result;
   }   // end greatestCommonDivisor


   //-----------------------------------------------------------------
   // Simple test is provided here

   // Simple test is provided here

public static void main(String[] args)
{
FractionInterface firstOperand = null;
FractionInterface secondOperand = null;
FractionInterface result = null;
double doubleResult = 0.0;

Fraction nineSixteenths = new Fraction(9, 16); // 9/16
Fraction Fraction(1, 4); // 1/4

System.out.println(" =========================================");
// 7/8 + 9/16
firstOperand = new Fraction(7, 8);
result = firstOperand.add(nineSixteenths);
System.out.println("The sum of " + firstOperand + " and " +
nineSixteenths + " is " + result);

// 9/16 - 7/8
firstOperand = nineSixteenths;
secondOperand = new Fraction(7, 8);
result = firstOperand.subtract(secondOperand);
System.out.println("The difference of " + firstOperand +
" and " + secondOperand + " is " + result);

// 15/-2 * 1/4
firstOperand.setFraction(15, -2);
result = firstOperand.multiply(oneFourth);
System.out.println("The product of " + firstOperand +
" and " + oneFourth + " is " + result);

// (-21/2) / (3/7)
firstOperand.setFraction(-21, 2);
secondOperand.setFraction(3, 7);
result = firstOperand.divide(secondOperand);
System.out.println("The quotient of " + firstOperand +
" and " + secondOperand + " is " + result);

// -21/2 + 7/8
firstOperand.setFraction(-21, 2);
secondOperand.setFraction(7, 8);
result = firstOperand.add(secondOperand);
System.out.println("The sum of " + firstOperand +
" and " + secondOperand + " is " + result);


// 0/10, 5/(-15), (-22)/7
firstOperand.setFraction(0, 10);
doubleResult = firstOperand.toDouble();
System.out.println("The double floating point value of " + firstOperand + " is " + doubleResult);
firstOperand.setFraction(1, -3);
doubleResult = firstOperand.toDouble();
System.out.println("The double floating point value of " + firstOperand + " is " + doubleResult);
firstOperand.setFraction(-22, 7);
doubleResult = firstOperand.toDouble();
System.out.println("The double floating point value of " + firstOperand + " is " + doubleResult);
System.out.println(" =========================================");
firstOperand.setFraction(-21, 2);
System.out.println("First = " + firstOperand);
// equality check
System.out.println("check First equals First: ");
if (firstOperand.equals(firstOperand))
System.out.println("Identity of fractions OK");
else
System.out.println("ERROR in identity of fractions");

secondOperand.setFraction(-42, 4);
System.out.println(" Second = " + secondOperand);
System.out.println("check First equals Second: ");
if (firstOperand.equals(secondOperand))
System.out.println("Equality of fractions OK");
else
System.out.println("ERROR in equality of fractions");

// comparison check
Fraction first = (Fraction)firstOperand;
Fraction second = (Fraction)secondOperand;

System.out.println(" check First compareTo Second: ");
if (first.compareTo(second) == 0)
System.out.println("Fractions == operator OK");
else
System.out.println("ERROR in fractions == operator");

second.setFraction(7, 8);
System.out.println(" Second = " + secondOperand);
System.out.println("check First compareTo Second: ");
if (first.compareTo(second) < 0)
System.out.println("Fractions < operator OK");
else
System.out.println("ERROR in fractions < operator");

System.out.println(" check Second compareTo First: ");
if (second.compareTo(first) > 0)
System.out.println("Fractions > operator OK");
else
System.out.println("ERROR in fractions > operator");

System.out.println(" =========================================");

System.out.println(" check FractionException: 1/0");
try {
Fraction a1 = new Fraction(1, 0);
}
catch ( FractionException fe )
{
System.err.printf( "Exception: %s ", fe );
} // end catch

System.out.println(" check FractionException: division");
try {
Fraction a2 = new Fraction();
Fraction a3 = new Fraction(1, 2);
a3.divide(a2);
}
catch ( FractionException fe )
{
System.err.printf( "Exception: %s ", fe );
} // end catch

} // end main
} // end Fraction