Question: Design a class called Fraction . This class is used to represent a ratio of two integers. Include accessor and mutator methods that allow the
Design a class called Fraction . This class is used to represent a ratio of two integers. Include accessor and mutator methods that allow the user to set and get the numerator and the denominator. Have two constructors, one a no argument constructor and another that takes two arguments, the numerator and the denominator. All include a methods that returns the value of numerator divided by the denominator as a double up to four decimal places. Call this method eval . Include an additional method that outputs the value of the fraction reduced to the lowest terms (e.g. instead of outputting 20/60, the method should return a string 1/3 as output). This method which returns a string should be called asFraction(). This will require finding the the greatest common divisor (GCD) for the numerator and the denominator, then dividing both by that number. Test your class with the a tester program. Use the one supplied. Do not modify it in any way.
Tester program:
package assignment5;
public class tester {
public static void main(String[] args) {
Fraction f1 = new Fraction();
Fraction f2 = new Fraction (20,30);
f1.setNumerator(25);
f1.setDenominator(50);
System.out.println("f1 eval() -> " + f1.eval());
System.out.println("f1 toFraction() -> " + f1.asFraction());
System.out.println("f2 eval() -> " + f2.eval());
System.out.println("f2 toFraction() -> " + f2.asFraction());
/**
* Your output should be:
*
* f1 eval() -> 0.5
* f1 toFraction() -> 1/2
* f2 eval() -> 0.6666
* f2 toFraction() -> 2/3
*/
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
