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

Design a class called Fraction . This class is used to represent a ratio of two

ID: 3827811 • Letter: D

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

           */

       }

   }

Explanation / Answer

Fraction.java

public class Fraction {

   //Declaring instance variables
   private int numerator;
   private int denominator;

   //Zero argumented constructor
   public Fraction() {
       super();

   }
   //Parameterized constructor
   public Fraction(int numerator, int denominator) {
       super();
       this.numerator = numerator;
       this.denominator = denominator;
   }

   //getters and setters
   public int getNumerator() {
       return numerator;
   }

   public void setNumerator(int numerator) {
       this.numerator = numerator;
   }

   public int getDenominator() {
       return denominator;
   }

   public void setDenominator(int denominator) {
       this.denominator = denominator;
   }

   //This method will return the decimal type value limiting to 4 decimal places
   public double eval() {
       double res = (double) numerator / denominator;
       res = res * 10000;
       res = (int) res;
       res = res / 10000;
       return res;
   }

   //This method will return the fraction value after reduced to lowest terms
   public String asFraction() {
       int res = gcd(numerator, denominator);
       int numer = numerator / res;
       int denom = denominator / res;

       return numer + "/" + denom;
   }

   //This method will find the gcd of numerator and denominator
   private int gcd(int numerator, int denominator) {
       // % is modulus which is the remainder of a division
       // base case
       if ((numerator % denominator) == 0) {
           return denominator;
       }
       // recursive case
       else {
           return gcd(denominator, numerator % denominator);
       }
   }

}

___________________

Tester.java

public class Tester {
   public static void main(String[] args) {
       //Creating The Fraction class object
       Fraction f1 = new Fraction();

           //Creating The Fraction class object by passing the values as arguments
Fraction f2 = new Fraction (20,30);

//calling the setter methods
f1.setNumerator(25);
f1.setDenominator(50);

//Displaying the results
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());

   }

}

_____________________

Output:

f1 eval() -> 0.5
f1 toFraction() -> 1/2
f2 eval() -> 0.6666
f2 toFraction() -> 2/3

____________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