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

/*******************************************************************************

ID: 3674764 • Letter: #

Question

/*************************************************************************************
*
* This class represents a fraction whose numerator and denominator are integers.
*
* Requirements:
* Implement interfaces: SimpleFractionInterface 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 a fraction 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 SimpleFractionException in case of errors, do not throw other types of exception objects
* 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 SimpleFractionInterface to SimpleFraction if
* you want to use it as SimpleFraction. See add, subtract, multiply and divide methods
*
* 4. Use "this" to access this object if it is needed
*
************************************************************************************/

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

package PJ1;

public interface SimpleFractionInterface
{
/** Task: Sets a fraction to a given value.
* @param num is the integer numerator
* @param den is the integer denominator
* @throw SimpleFractionException if denominator is 0 */
public void setSimpleFraction(int num, int den);

   /** 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 is 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 SimpleFractionInterface add(SimpleFractionInterface 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 SimpleFractionInterface subtract(SimpleFractionInterface 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 SimpleFractionInterface multiply(SimpleFractionInterface 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
* @throw SimpleFractionException if secondFraction is 0 */
   public SimpleFractionInterface divide(SimpleFractionInterface secondFraction);

   /** Task: Get's the fraction's reciprocal
   * @return the reciprocal of the invoking fraction
* @throw SimpleFractionException if the new number with denominator is 0*/
   public SimpleFractionInterface getReciprocal();

}

*************************************************************************************************************************************/

package PJ1;

public class SimpleFraction implements SimpleFractionInterface, Comparable<SimpleFraction>
{
   // integer numerator and denominator
   private   int num;  
   private   int den;  

   public SimpleFraction()
   {
       // implement this method!
       // set fraction to default = 0/1
   }   // end default constructor

   public SimpleFraction(int num, int den)
   {
       // implement this method!
   }   // end constructor

   public void setSimpleFraction(int num, int den)
   {
       // implement this method!
       // return SimpleFractionException if initialDenominator is 0
   }   // end setSimpleFraction

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

   public SimpleFractionInterface add(SimpleFractionInterface secondFraction)
   {
// implement this method!
       // a/b + c/d is (ad + cb)/(bd)
// return result which is a new SimpleFraction object
       return null;
   }   // end add

   public SimpleFractionInterface subtract(SimpleFractionInterface secondFraction)
   {
// implement this method!
       // a/b - c/d is (ad - cb)/(bd)
// return result which is a new SimpleFraction object
       return null;
   }   // end subtract

   public SimpleFractionInterface multiply(SimpleFractionInterface secondFraction)
   {
// implement this method!
       // a/b * c/d is (ac)/(bd)
// return result which is a new SimpleFraction object
       return null;
   }   // end multiply

   public SimpleFractionInterface divide(SimpleFractionInterface secondFraction)
   {
// implement this method!
       // a/b / c/d is (ad)/(bc)
       // return SimpleFractionException if secondFraction is 0
// return result which is a new SimpleFraction object
       return null;
   }   // end divide

   public SimpleFractionInterface getReciprocal()
   {
// implement this method!
       // return SimpleFractionException if secondFraction is 0
// return result which is a new SimpleFraction object
       return null;
   } // end getReciprocal


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


   public int compareTo(SimpleFraction 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 reduceSimpleFractionToLowestTerms()
   {
// implement this method!
//
// Outline:
// compute GCD of num & den
// GCD works for + numbers.
// So, you should eliminate - sign
// then reduce numbers : num/GCD and den/GCD
      
   }   // end reduceSimpleFractionToLowestTerms

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

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

       return result;
   }   // end GCD

Explanation / Answer

package PJ1;

public interface SimpleFractionInterface
{
/** Task: Sets a fraction to a given value.
* @param num is the integer numerator
* @param den is the integer denominator
* @throw SimpleFractionException if denominator is 0 */

        int numerator,denominator;

        double result;

        try


public void setSimpleFraction(int num, int den)

{

numerator = num;

denominator = den;

if(denominator==0) throw new SimpleFractionException

}

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

{

result= numerator/ denominator;

return result;

}

   /** Task: Adds two fractions.
   * @param secondFraction is 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 SimpleFractionInterface add(SimpleFractionInterface secondFraction)

{

if (!(secondFraction instanceof SimpleFractionInterface))

                           return null;

                          

                 SimpleFractionInterface f = (SimpleFractionInterface)obj;

                

/* calculate the numerator and denominator achieved with a common denominator.*/

       

int num = this.numerator*f.denominator + this.denominator*f.numerator;

int den = this.denominator*f.denominator;

                

// Return the new object with method calling for adding farction.

        SimpleFractionInterface a;

          return a.setSimpleFraction(num, den);

        }

   /** 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 SimpleFractionInterface subtract(SimpleFractionInterface secondFraction)

{

int num = this.numerator*f.denominator - this.denominator*f.numerator;

int den = this.denominator*f.denominator;

SimpleFractionInterface s;

return s.setSimpleFraction(num, den);

}

/** 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 SimpleFractionInterface multiply(SimpleFractionInterface secondFraction)

{

int num = this.numerator* secondFraction.numerator;

                 int den = this.denominator* secondFraction.denominator;

                 SimpleFractionInterface m;

          return m.setSimpleFraction(num, den);

        }

   /** 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
* @throw SimpleFractionException if secondFraction is 0 */
   public SimpleFractionInterface divide(SimpleFractionInterface secondFraction)

{

   SimpleFractionInterface ans;

ans. setSimpleFraction(1,1);

// Multiply the current object exp times, storing the answer in ans.

                 for (int i=0; i<exp; i++)

                           ans = ans.multiply(this);

                 return ans;

}

   public SimpleFractionInterface getReciprocal()

{

SimpleFractionInterface r;

return r. setSimpleFraction(den, num);

}

}//end interface

package PJ1;

public class SimpleFraction implements SimpleFractionInterface, Comparable<SimpleFraction>
{
   // integer numerator and denominator
   private   int num;  
   private   int den;

//creating an object of class for calling interface methods

SimpleFractionInterface    i1=new SimpleFraction();

public int getNumerator()

{

        return this.numerator;

    }

    public int getDenominator()

{

        return this.denominator;

    }

public SimpleFraction()
   {

denominator=0;

   numerator=1;
       }

   public SimpleFraction(int num, int den)
   {
      denominator=den;

       numerator=num;
   }   // end constructor

   public void setSimpleFraction(int num, int den)
   { numerator = num;

denominator = den;

if(denominator==0) throw new SimpleFractionException

   }   // end setSimpleFraction

   public double toDouble()
   {  

result= numerator/ denominator;

return result; }   // end toDouble

   public SimpleFractionInterface add(SimpleFractionInterface secondFraction)
   {  

SimpleFractionInterface. add(SimpleFractionInterface secondFraction);

return null;


   }   // end add

   public SimpleFractionInterface subtract(SimpleFractionInterface secondFraction)
{

SimpleFractionInterface. sub(SimpleFractionInterface secondFraction);

  return null;
   }   // end subtract

   public SimpleFractionInterface multiply(SimpleFractionInterface secondFraction)
   {      

SimpleFractionInterface.multiply(SimpleFractionInterface secondFraction);

return null;
   }   // end multiply

   public SimpleFractionInterface divide(SimpleFractionInterface secondFraction)
   {

SimpleFractionInterface.divide(SimpleFractionInterface secondFraction);

       return null;
   }   // end divide

   public SimpleFractionInterface getReciprocal()
   {

SimpleFractionInterface. getReciprocal();

       return null;
   } // end getReciprocal


   public boolean equals(Object f)
   {
if (f instanceof SimpleFraction) {

                           SimpleFraction tmp = (SimpleFraction)f;

                           return (Math.abs((double)numerator/denominator - (double)tmp.numerator/tmp.denominator) < EPSILON);

                 }

                 return false;

   } // end equals


   public int compareTo(SimpleFraction other)
   {

long t = this.getNumerator() * other.getDenominator();

        long f1 = frac.getNumerator() * other.getDenominator();

        int result = 0;

        if(t>f1) {

            result1 = 1;

        }

        else if(f1>t) {

            result1 = -1;

        }

        return result1;

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 reduceSimpleFractionToLowestTerms()
   {

int smaller = num < den ? num : den;

        int HCF = -1;

        for (int i = smaller; i > 0; --i) {

            if (num%i==0&&den%i==0) {

                HCF = i;

                System.out.println("Reduced form: "+(num/HCF)+"/"+(den/HCF));

   }   // end reduceSimpleFractionToLowestTerms

   private int GCD(int integerOne, int integerTwo)
   {
       int result;

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

       return result;
   }   // end GCD

}