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

in java Write a Fraction class – that is, write your own class that will represe

ID: 3766905 • Letter: I

Question

in java

Write a Fraction class – that is, write your own class that will represent a fraction in your program. Each variable of type Fraction represents a single fraction. That means that the class should have at least two member variables that should be private: the numerator and the denominator of the fraction. The class also must have the following public member functions:

• Fraction(int n, int d); // constructor that defines a fraction n/d

• Fraction(); // constructor that defines a default fraction of 0/1

• Fraction(Scanner s); // constructor that defines a fraction via Scanner input

• double toDecimal(); // returns the decimal value of the fraction

• String toString(); // returns the string form of the fraction

o "numerator" if denominator is 1

o "numerator/denominator (decimal, with three decimal places)" otherwise

• int getNumerator(); // returns the numerator of the fraction

• int getDenominator(); // returns the denominator of the fraction

• Fraction plus(Fraction f); // returns fraction + parameter

• Fraction minus(Fraction f); // returns fraction - parameter

• Fraction times(Fraction f); // returns fraction * parameter

• Fraction divides(Fraction f); // returns fraction / parameter

You have been provided a private method:

• int[] simplifyFraction(int[] f); // returns simplified version of parameter

Using this method, all constructors should make sure that the fraction is in reduced form. You may not change the public API – that is, any methods not listed above must be made private.

Explanation / Answer

Sample code: