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

Java :Fraction Class Imagine that the company you work for is going to create a

ID: 3826413 • Letter: J

Question

Java :Fraction Class

Imagine that the company you work for is going to create a lot of things on Fractions. You are to create a robust Fraction class that will have all of the following  (all examples are for a fraction half that has a numerator of 1 and a denominator of 2):

Private integers numerator and denominator ;

All public getter and setter functions for the numerator and denominator;

Safeguard that the denominator will NEVER become 0!

a default constructor with no arguments;

a constructor that accepts both the numerator and denominator;

a toDecimal method that returns the decimal value of the fraction, example: 1/2 will be 0.5;

a toString method that will return the fraction as a string, , example: 1/2 will be "1/2";

a reduce method that will change the numerator and denominator by finding a common denominator and reducing the fraction. Example 3/12 becomes 1/4;

A class should not interact with the user. If an invalid value is received it simple does not accept it. It does not display a message, it does not exit. It is up to the calling program to check if the value was accepted

Explanation / Answer

/**
*
*/

/**
* class to define fraction
*
* @author
*
*/
public class Fraction {

   private int numerator;
   private int denominator;

   public Fraction() {
       // TODO Auto-generated constructor stub
       setNumerator(0);
       setDenominator(1);
   }

   /**
   * @param numerator
   * @param denominator
   */
   public Fraction(int numerator, int denominator) {
       setNumerator(numerator);
       setDenominator(denominator);
   }

   /**
   * @return the numerator
   */
   public int getNumerator() {
       return numerator;
   }

   /**
   * @return the denominator
   */
   public int getDenominator() {
       return denominator;
   }

   /**
   * @param numerator
   * the numerator to set
   */
   public void setNumerator(int numerator) {
       this.numerator = numerator;
   }

   /**
   * @param denominator
   * the denominator to set
   */
   public void setDenominator(int denominator) {

       // if it is less than zero then it sets to 1
       if (denominator > 0)
           this.denominator = denominator;
       else
           this.denominator = 1;
   }

   /**
   * method that returns the decimal value of the fraction
   *
   * @return
   */
   public double toDecimal() {

       return (double) getNumerator() / (double) getDenominator();

   }

   @Override
   public String toString() {
       // TODO Auto-generated method stub
       return getNumerator() + "/" + getDenominator();
   }

   /**
   * method that will change the numerator and denominator by finding a common
   * denominator and reducing the fraction
   */
   public void reduce() {
       int GCD = gcd(getNumerator(), getDenominator());
       setNumerator(numerator / GCD);
       setDenominator(denominator / GCD);
   }

   /**
   * greatest common denominator, used to reduce fractions
   *
   * @param n1
   * @param n2
   * @return
   */
   public int gcd(int n1, int n2) {
       int M, N, R;
       if (n1 < n2) {
           N = n1;
           M = n2;
       } else {
           N = n2;
           M = n1;
       }
       R = M % N;
       while (R != 0) {
           M = N;
           N = R;
           R = M % N;
       }
       return N;
   }

   /**
   * @param args
   */
   public static void main(String[] args) {

       // TODO Auto-generated method stub
       Fraction fraction1 = new Fraction();
       System.out.println("fraction with default constructor:"
               + fraction1.toString());

       Fraction fraction2 = new Fraction(5, 10);
       System.out.println("fraction with parameterized constructor:"
               + fraction2.toString());

       fraction2.reduce();
       System.out.println("Reduced Fraction : " + fraction2);

       System.out.println("To decimal fraction :" + fraction2.toDecimal());

   }

}

OUTPUT:

fraction with default constructor:0/1
fraction with parameterized constructor:5/10
Reduced Fraction : 1/2
To decimal fraction :0.5

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