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

Write a program that works with fractions. You are first to implement three meth

ID: 3763823 • Letter: W

Question

Write a program that works with fractions. You are first to implement three methods, each to perform a different calculation on a pair of fractions: subtract, multiply, and divide. For each of these methods, you are supplied two fractions as arguments, each a two-dimensional array (the numerator is at index 0, the denominator is at index 1), and you are to return a resulting, simplified fraction as a new two-dimensional array (again, with the numerator at index 0, and denominator at index 1). You have been provide an add method as an example. You must compute the resulting fraction using fraction-based math (working with numerators and denominators) – do not convert the fractions to double values (like 1.5), do the math, and convert back to a fraction. You have been provided a method to simplify a fraction using the gcd method a previous last lab. As a reminder for fraction arithmetic… n_1/d_1 +n_2/d_2 =(n_1 d_2+n_2 d_1)/(d_1 d_2 ) n_1/d_1 -n_2/d_2 =(n_1 d_2-n_2 d_1)/(d_1 d_2 ) n_1/d_1 ×n_2/d_2 =(n_1 n_2)/(d_1 d_2 ) n_1/d_1 ÷n_2/d_2 =(n_1 d_2)/(d_1 n_2 ) Once the operation methods are complete, focus your attention on the main method. You first need to input the two fractions from the keyboard (numerator then denominator for each; you can assume integers) as well as one of the four valid operations (+, -, *, /). Then validate the inputs: make sure a valid operation was input, make sure neither of the denominators are zero, and make sure that the numerator of the second fraction isn’t zero if the operation is division (error messages have been provided for each of these situations). Finally, compute the result of the operation and output the answer. Note that if the denominator of the answer is 1, you should just output the numerator (this includes if the answer is 0). (in Java)

Explanation / Answer

package current;

public class FractionOpertions {

   public static void main(String[] args) {
       FractionOpertions oper = new FractionOpertions();
      
       int[] n1 = {5, 6};
       int[] n2 = {6,5};
      
       System.out.println("Fraction 1: "+n1[0]+"/"+n1[1]+"   Fraction 2: "+n2[0]+"/"+n2[1]);
       int[] result = oper.add(n1, n2);
       System.out.println("Addition: "+result[0]+"/"+result[1]);
       result = oper.subtract(n1, n2);
       System.out.println("subtract: "+result[0]+"/"+result[1]);
       result = oper.multiply(n1, n2);
       System.out.println("multiply: "+result[0]+"/"+result[1]);
       result = oper.divide(n1, n2);
       System.out.println("Division: "+result[0]+"/"+result[1]);
   }
  
   public int[] add(int[] f1, int[] f2) {
       // n_1/d_1 +n_2/d_2 =(n_1 d_2+n_2 d_1)/(d_1 d_2 )
       int denom = f1[1] * f2[1];
       int numer = f1[0] * f2[1] + f2[0] * f1[1];
       return simplify(numer, denom);
      
   }
  
   public int[] subtract(int[] f1, int[] f2) {
       int denom = f1[1] * f2[1];
       //n_1/d_1 -n_2/d_2 =(n_1 d_2-n_2 d_1)/(d_1 d_2 )
       int numer = f1[0] * f2[1] - f2[0] * f1[1];
       int[] result = simplify(numer < 0 ? (-1* numer) : numer, denom);
       //System.out.println(numer+" # "+denom+"# "+result[0]+"/"+result[1]);
       if(numer < 0)
           result[0] *= -1;
       return result;
      
   }
  
   public int[] multiply(int[] f1, int[] f2) {
       //n_1/d_1 ×n_2/d_2 =(n_1 n_2)/(d_1 d_2 )
       int denom = f1[1] * f2[1];
       int numer = f1[0] * f2[0];
      
       return simplify(numer, denom);
   }
  
   public int[] divide(int[] f1, int[] f2) {
       //n_1/d_1 ÷n_2/d_2 =(n_1 d_2)/(d_1 n_2 )
       int denom = f1[1] * f2[0];
       int numer = f1[0] * f2[1];
      
       return simplify(numer, denom);
   }
  
   private int[] simplify(int numer, int denom) {
       int gcd = gcd(numer, denom);
       int[] fraction = new int[2];
       fraction[0] = numer/gcd;
       fraction[1] = denom/gcd;
       return fraction;
   }
  
   private int gcd(int n1, int n2) {
        int r=0, a, b;

        a = (n1 > n2) ? n1 : n2;

        b = (n1 < n2) ? n1 : n2;


        r = b;

        while(a % b != 0)

        {

            r = a % b;

            a = b;

            b = r;

        }

        return r;
   }
}

------------output------------------

Fraction 1: 5/6   Fraction 2: 6/5
Addition: 61/30
subtract: -11/30
multiply: 1/1
Division: 25/36

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