QUESTION

When we test your Fraction.java we will use the givenFractionTester.java file. Your Fraction.java file -MUST-...

When we test your Fraction.java we will use the givenFractionTester.java file. Your Fraction.java file -MUST- workperfectly with the supplied FractionTester.java file. You may NOTmodify the FractionTester to make your Fraction class workright.

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.javafile

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

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

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

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

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

private void reduce() Does not return aFraction. It just modifies this Fraction by reducing it to itslowest form.

You must keep every Fraction reduced at all times. Every newfraction constructed it must be reduced before the constructorexits. No Fraction at any time may be stored in a form other thanits reduced form. This makes the outputted value of the Fractionconsistent in all cases.

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

/*        FractionTester.java  A program that declares Fraction variables        We will test your Fraction class on THIS tester.*/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        } // END main} // EOF
/* Fraction.java  A class (data type) definition file   This file just defines what a Fraction is   This file is NOT a program   ** data members are PRIVATE   ** method members are PUBLIC*/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                {                        // error msg OR exception OR exit etc.                }        }        // 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 )        {                // you could calc the gcd(n,d) here and then divide both by gcd in the setter calls                setNumer(n); // i.e. setNumer( n/gcd );                setDenom(d); // 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()        {                // reduces this fraction to lowest form        }}// EOF

Public Answer

MMU0RK The First Answerer