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

Write a class that implements the functionality of fractions. (What are the priv

ID: 3562259 • Letter: W

Question

Write a class that implements the functionality of fractions. (What are the private values that are needed?) This class will include read method and methods add, subtract, multiply and divide with fraction parameters with integer parameters and with two integers representing the numerator and denominator of a fraction.

Other methods that will be needed are a toString method, an equals method and a method calledgreaterThan() that has one parameter and returns true if the this is greater than the parameter. Include three constructors: a default constructor, an explicit constructor that brings in a value for the numerator and a value for the denominator, and an explicit constructor with an integer argument. Consider the following pairs sent to the explicit constructor: (1,2), (-1,2), (1,-2), (1,0), (10,20). I recommend keeping the sign in the numerator.

Exit the program with an appropriate error message if the denominator is 0.

(Hint: do not leave the constructor till the fraction is perfect)

****An algorithm for determining the greatest common divisor.****

public class FractionDemoSimple
{
   public static void main (String[] args)
   {
       Fraction r1 = new Fraction (1, 2);
       Fraction r2 = new Fraction (2, 3);
       Fraction r3, r4, r5, r6, r7, r8, r9;

       System.out.println ("First fraction number: " + r1);
       System.out.println ("Second fraction number: " + r2);

       if (r1.equals(r2))
           System.out.println ("r1 and r2 are equal.");
       else
           System.out.println ("r1 and r2 are NOT equal.");

       r3 = r2.reciprocal();
       System.out.println ("The reciprocal of r2 is: " + r3);

       r4 = r1.add(r2);
       r5 = r1.subtract(r2);
       r6 = r1.multiply(r2);
       r7 = r1.divide(r2);
       r8 = r1.add(5);
       r9 = r1.multiply(5);
       System.out.println ("r1 + r2: " + r4);
       System.out.println ("r1 - r2: " + r5);
       System.out.println ("r1 * r2: " + r6);
       System.out.println ("r1 / r2: " + r7);
       System.out.println ("r1 + 5: " + r8);
       System.out.println ("r1 * 5: " + r8);
   }

   //IF YOU KEEP THE SIGN IN THE NUMERATOR NOTE THE PRECONDITIONS!
   public static int greatestCommonDivisor(int a, int b)
   // pre: a>= 0 && b> 0
   // post: Function Value == Greatest common divisor of a and b
   {
       int temp = a % b;
       while(temp > 0)
       {
           a = b;
           b = temp;
           temp = a % b;
       }// ASERT: b == Greatest common divisor of a <entry> and b <entry>
       return b;
   }
}

Explanation / Answer

public class FractionDemoSimple

{

   public static void main (String[] args)

   {

       Fraction r1 = new Fraction (1, 2);

       Fraction r2 = new Fraction (2, 3);

       Fraction r3, r4, r5, r6, r7, r8, r9;

       System.out.println ("First fraction numeratorber: " + r1);

       System.out.println ("Second fraction numeratorber: " + r2);

       if (r1.equals(r2))

           System.out.println ("r1 and r2 are equal.");

       else

           System.out.println ("r1 and r2 are NOT equal.");

       r3 = r2.reciprocal();

       System.out.println ("The reciprocal of r2 is: " + r3);

       r4 = r1.add(r2);

       r5 = r1.subtract(r2);

       r6 = r1.multiply(r2);

       r7 = r1.divide(r2);

       r8 = r1.add(5);

       r9 = r1.multiply(5);

       System.out.println ("r1 + r2: " + r4);

       System.out.println ("r1 - r2: " + r5);

       System.out.println ("r1 * r2: " + r6);

       System.out.println ("r1 / r2: " + r7);

       System.out.println ("r1 + 5: " + r8);

       System.out.println ("r1 * 5: " + r8);

   }

   public static int greatestCommonDivisor(int a, int b)

   {

       int temp = a % b;

       while(temp > 0)

       {

           a = b;

           b = temp;

           temp = a % b;

       }

       return b;

   }

}

------------------------------------------------------------------------------------------------------------------------------------

public class Fraction

{

                private int numerator;

                private int denominator;

              

                public Fraction(int n, int d)

                {          

                                if(d != 0)

                                {

                                                numerator = n;

                                                denominator = d;

                                }

                                else

                                                System.exit(0);                               

                }

                private static int gcd(int x, int y)

                {

                                int mod;

                                if(x < y)

                                {

                                   mod = x;

                                   x = y;

                                   y = mod;                         

                                }

                                int r = x % y;

                                while (r != 0)

                                {

                                x = y;

                                y = r;

                                r = x % y;

                                }

                                return y;                              

                }

                private Fraction reduce(int n, int d)

                {

                                int gcdNumerator = gcd(n,d);                              

                                d = d / gcdNumerator;

                                n = n / gcdNumerator;

                                return new Fraction(n,d);          

                }

                public Fraction add(Fraction b)

                {

                                int numerator1 = (this.numerator * b.denominator) + (b.numerator * this.denominator);

                                int  numerator2 = this.denominator * b.denominator;

                                return reduce(numerator1,numerator2);

                }

         public Fraction add(int b)

                {

                                int numerator1 = (this.numerator * 1) + (b * this.denominator);

                                int numerator2 = this.denominator * 1;

                                return reduce(numerator1,numerator2);

                }

       

                public Fraction subtract(Fraction b)

                {

                                int numerator1 = (this.numerator * b.denominator) - (b.numerator * this.denominator);

                                int numerator2 = this.denominator * b.denominator;

                                return reduce(numerator1,numerator2);

                }

   

     public Fraction subtract(int b)

     {

                                int numerator1 = (this.numerator * 1) - (b * this.denominator);

                                int numerator2 = this.denominator * 1;

                                return reduce(numerator1,numerator2);

                }

                public Fraction multiply(Fraction b)

                {

                                int numerator1 = this.numerator * b.numerator;

                                int numerator2 = this.denominator * b.denominator;

                                return reduce(numerator1,numerator2);

                }

   

         public Fraction multiply(int b)

                {

                                int numerator1 = this.numerator * b;

                                int  numerator2 = this.denominator * 1;

                                return reduce(numerator1,numerator2);

                }

   

     public Fraction reciprocal()

     {                       

                                int numerator1 = this.denominator ;

                                int numerator2 = this.numerator;

                                return reduce(numerator1, numerator2);

                }

                public Fraction divide(Fraction b)

                {                            

                                int numerator1 = this.numerator * b.denominator;

                                int numerator2 = this.denominator * b.numerator;

                                return reduce(numerator1, numerator2);

                }

   

   

     public Fraction divide(int b)

     {                       

                                int numerator1 = this.numerator * 1;

                                int numerator2 = this.denominator * b;

                                return reduce(numerator1, numerator2);

                }

   

                public String toString()

                {             

                  if(numerator > denominator && denominator > 1)                                                        

                    return (numerator + "/" + denominator + " or " + (numerator/denominator) + " " + (numerator % denominator) + "/"

                              + denominator);                     

                   else

                      return(numerator + "/" + denominator);

                }

}   
--------------------------------------------------------------------------------------------------------------------

Sample Output:

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