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

Fraction Class In this assignment, you will implement and create a Fraction clas

ID: 3710950 • Letter: F

Question

Fraction Class In this assignment, you will implement and create a Fraction class for mixed fraction numbers Complete the following interface for the Fraction class and save it as Fraction.java: public class Fraction private int wholei private int numerator private int denominator // Create a paramaterless constructor, which sets /1 the whole number and numerator to 0 and the denominator to1 public Fraction() / Create a constructor that accepts two paraneters /1 for the nunerator and denominator. When the constructor // is used, the whole number value is 0 public Fraction int numerator, int denominator) // Create a constructor that accepts three parameters /1 for the whole number, numerator, and denominator public Fraction(int whole, int numerator, int denominator) // This calculates greatest common divisor (CCD) // I expect that you will find it useful int ged(int a, int b) return ai return ged(b,ab

Explanation / Answer

//The interface for the Fraction class program

public class Fraction
{
      private int whole;
      private int numerator;
      private int denominator;
  
      //Create a paramterless constructor , which sets
      // the whole number and numerator to 0 and the denominator to 1.
  
      public Fraction()
      {
         whole = 0;
       numerator = 0;
       denominator = 1;
      }
  
      //Creat a constructor that accepts two paramters
      //for the numerator and denominator.When the constructor
      //is used, the whole number is 0.
  
      public Fraction(int numerator, int denominator)
      {
          numerator = numerator;
          denominator = denominator;
          whole = 0;
      }
  
      // Create a constructor that accepts three parameters
      // for the numerator, denominator and whole.
  
      public Fraction(int whole, int numerator, int denominator)
      {
          whole = whole;
          numerator = numerator;
          denominator = denominator;
      }
  
      // This calculates greatest common divisor (GCD)
      // I expect that you will find it useful
  
      int gcd(int a, int b)
      {
         if(b == 0)
            return a;
          return gcd(b, a%b);
   }
  
      // Reduce the Fraction value to the lowest possible denominator
      // example 72/30 should be reduced to 18/5 , which is also 3 3/5.
  
      public void reduce()
      {

          int commonNum = gcd(numerator ,denominator);
       numerator = numerator /commonNum;
         denominator = denominator /commonNum;
      }
  
      // Add the current Fraction to Fration A , retrn the result as a new Fration ( reduced , of course)
  
      public Fraction add(Fraction A)
      {    
            //Convert mixed fraction to simple fraction
          
           int num1 = ((this.whole*this.denominator) + (this.numerator));
           int num2 = ((A.whole*A.denominator) + (A.numerator));  
            int denomPart = this.denominator * A.denominator;
           int n1 = num1 * A.denominator;
           int n2 = num2 * this.denominator;
           int wholePart = (n1+n2)/denomPart;
           int numPart = (n1+n2)%denomPart;
          
           Fraction fraction1 = new Fraction(wholePart,numPart, denomPart);
           fraction1.reduce();
           return fraction1;
      }
  
      // Substract the current Fraction fraction A, return the result as a new Fraction ( reduced , of course)
  
      public Fraction substract(Fraction A)
      {
             //Convert mixed fraction to simple fraction
          
           //Convert mixed fraction to simple fraction
          
           int num1 = ((this.whole*this.denominator) + (this.numerator));
           int num2 = ((A.whole*A.denominator) + (A.numerator));  
            int denomPart = this.denominator * A.denominator;
           int n1 = num1 * A.denominator;
           int n2 = num2 * this.denominator;
           int wholePart = (n1-n2)/denomPart;
           int numPart = (n1-n2)%denomPart;
          
           Fraction fraction1 = new Fraction(wholePart,numPart, denomPart);
           fraction1.reduce();
           return fraction1;
          
      }
  
      // Multiply the current Fraction to Fraction A , return the result as a new Fraction(reduced , of course)
  
      public Fraction multiply(Fraction A)
      {
             //Convert mixed fraction to simple fraction
          
           int num1 = ((this.whole*this.denominator) + (this.numerator));
           int num2 = ((A.whole*A.denominator) + (A.numerator));  
           int denomPart = (this.denominator)*(A.denominator);
           int wholePart = (num1*num2)/denomPart;
           int numPart = (num1*num2)% denomPart;
           Fraction result = new Fraction(wholePart , numPart,denomPart);
           result.reduce();
           return result;
      }
  
       // Divide the current Fraction to Fraction A , return the result as a new Fraction(reduced , of course)
  
      public Fraction divide(Fraction A)
      {    
            int num1 = ((this.whole*this.denominator) + (this.numerator));
           int num2 = ((A.whole*A.denominator) + (A.numerator));  
           int denomPart = (this.denominator)*(A.denominator);
            int wholePart = (num1*denomPart)/num2;
           int numPart = (num1*denomPart)% num2;
           Fraction result = new Fraction(wholePart , numPart,denomPart);
           result.reduce();
           return result;
      }
  
  
      //toString api for fraction TBD
  
      public string toString()
      {
          if ( whole != 0)
          {
             return whole + "" + numerator + "/" + denominator;
          }
          else
          {
            return numerator + "/" + denominator;
          }
          if ( numerator == 0)
          {
            return whole;
          }
          if (denominator == 1)
          {
            return numerator;
          }
      }
   }
        

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