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

Background: Project #6 requires you to enhance the Fraction Class, we have been

ID: 3802643 • Letter: B

Question

Background:

Project #6 requires you to enhance the Fraction Class, we have been studying in class. You are to add several methods as described below.

When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST- work perfectly with the supplied FractionTester.java file. You may NOT modify the FractionTester to make your Fraction class work right.

Starter Fraction File: Fraction.java ADD METHODS TO THIS FILE. HAND IN THIS FILE ONLY.

********************************************************************************

**************************************************************************************************

Given/Completed Tester File: FractionTester.java DO NOT MODIFY. DO NOT HAND IN.

************************************************************************************************

****************************************************************************************************************

You are to add the following methods to the given Fraction.java file

public Fraction add( Fraction other) returns a Fraction that is the sum of the two Fractions.

public Fraction subtract( Fraction other) returns a Fraction that is the difference between this Fraction minus the other Fraction.

public Fraction multiply( Fraction other) returns a Fraction that is the product of the two Fractions.

public Fraction divide( Fraction other) returns a Fraction that is the quotient of the two Fractions.

public Fraction reciprocal() returns a Fraction that is the reciprocal of this Fractions.

private void reduce() Does not return a Fraction. It just modifies this Fraction by reducing it to its lowest form.

You must keep every Fraction reduced at all times. Every new fraction constructed it must be reduced before the constructor exits. No Fraction at any time may be stored in a form other than its reduced form. This makes the outputted value of the Fraction consistent in all cases.

All methods except reduce() must be a single return statement of the form:
return new Fraction(...); -OR- return (a call to another Fraction method)

Correct Output:

f1=22/7

f2=3/2

65/14

23/14

33/7

44/21

7/22

Explanation / Answer

Fraction.java

public class Fraction {
   private int numer;
private int denom;

// ACCESSORS
public int getNumer()
{
return numer;
}
public int getDenom()
{
return denom;
}
public String toString()
{
return numer + "/" + denom;
}

// MUTATORS
public void setNumer( int n )
{
numer = n;
}
public void setDenom( int d )
{
if (d!=0)
denom=d;
else
{
throw new IllegalArgumentException("** Denominator Must not be Zero **");
}
}

// DEFAULT CONSTRUCTOR - no args passed in
public Fraction( )
{
this( 0, 1 ); // "this" means call a fellow constructor
}

// 1 arg CONSTRUCTOR - 1 arg passed in
// assume user wants whole number
public Fraction( int n )
{
this( n, 1 ); // "this" means call a fellow constructor

}

// FULL CONSTRUCTOR - an arg for each class data member
public Fraction( int n, int d )
{
   int gcd = this.gcd(n, d);
       // if GCD is negative, change to positive
       if (gcd < 0)
       {
       gcd = -gcd;
       }
setNumer(n/gcd); // i.e. setNumer( n/gcd );
setDenom(d/gcd); // same here
}

// COPY CONSTRUCTOR - takes ref to some already initialized Fraction object
public Fraction( Fraction other )
{
this( other.numer, other.denom ); // call my full C'Tor with other Fraction's data
}

private void reduce()
{
   // determine the greatest common divisor
       int gcd = this.gcd(numer, denom);
       // if GCD is negative, change to positive
       if (gcd < 0)
       {
       gcd = -gcd;
       }
       // divide gcd into both numerator and denominator
       numer = numer / gcd;
       denom = denom / gcd;   
}


//Method which will find Greatest Common Divisor
private Integer gcd(Integer a, Integer b)
{
   // % is modulus which is the remainder of a division
   // base case
   if ((a % b) == 0) {
   return b;
   }
   // recursive case
   else {
   return gcd(b, a % b);
   }
   }
  
//This method is used to add two Fractions
public Fraction add(Fraction f)
{
  

int a=this.numer;
int b=this.denom;
int c=f.numer;
int d=f.denom;
int num=(a*d + b*c);
int den=(b*d);
   Fraction frac=new Fraction(num, den);
frac.reduce();
  
return frac;

}
//This method is used to subtract two Fractions
public Fraction subtract(Fraction f)
{
   int a=this.numer;
int b=this.denom;
int c=f.numer;
int d=f.denom;
int num=(a*d - b*c);
int den=(b*d);
    Fraction frac=new Fraction(num, den);
frac.reduce();
return frac;
}

//This method is used to multiply two Fractions
public Fraction multiply(Fraction f)
{
   int a=this.numer;
int b=this.denom;
int c=f.numer;
int d=f.denom;
int num=(a*c);
int den=(b*d);
    Fraction frac=new Fraction(num, den);
frac.reduce();
return frac;

}

//This method is used to Divide two Fractions
public Fraction divide(Fraction f)
{
   int a=this.numer;
int b=this.denom;
int c=f.numer;
int d=f.denom;
int num=(a*d);
int den=(c*b);
   Fraction frac=new Fraction(num, den);
frac.reduce();
return frac;
}


//This method is used to find the reciprocal of a Fraction
public Fraction reciprocal()
{
   Fraction Frac=new Fraction(denom,numer);
   reduce();
   return Frac;
}
}

___________________

FractionTester.java

public class FractionTester
{
public static void main( String args[] )
{
// use the word Fraction as if were a Java data type
Fraction f1 = new Fraction( 44,14 ); // reduces to 22/7
System.out.println( "f1=" + f1 ); // should output 22/7

Fraction f2 = new Fraction( 21,14 ); // reduces to 3/2
System.out.println( "f2=" + f2 ); // should output 3/2

System.out.println( f1.add( f2 ) ); // should output 65/14
System.out.println( f1.subtract( f2 ) ); // should output 23/14
System.out.println( f1.multiply( f2 ) ); // should output 33/7
System.out.println( f1.divide( f2 ) ); // should output 44/21
System.out.println( f1.reciprocal() ); // should output 7/22

}
}

_______________________

Output:

f1=22/7
f2=3/2
65/14
23/14
33/7
44/21
7/22

________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote