Write a program that works with fractions You are first to i

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-element 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-element 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 from the previous lab.

Once the operation methods are complete and pass the JUnit tests, now 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).

Here is the outline code given:

public class LA5a {

  

   /**

   * Error to output if either denominator is zero

   */

   static final String E_DEN_ZERO = \"Denominator cannot be zero.\";

  

   /**

   * Error to output if dividing by zero

   */

   static final String E_DIV_ZERO = \"Cannot divide by zero.\";

  

   /**

   * Error to output if the operation is invalid

   */

   static final String E_OP_INVALID = \"Invalid operation.\";

  

   /**

   * Returns the greatest common divisor (gcd) of two integers

   *

   * @param num1 integer 1

   * @param num2 integer 2

   * @return gcd of integers 1 and 2

   */

   public static int gcd(int num1, int num2) {

       int t;

       while (num2 != 0) {

           t = num2;

           num2 = num1 % num2;

           num1 = t;

       }

      

       return num1;

   }

  

   /**

   * Returns the simplified form of a fraction

   *

   * @param f fraction (numerator=[0], denominator=[1])

   * @return simplified fraction (numerator=[0], denominator=[1])

   */

   public static int[] simplifyFraction(int[] f) {

       final int gcd = gcd(f[0], f[1]);

       int[] result = {f[0]/gcd, f[1]/gcd};

      

       if ((result[0]<0 && result[1]<0) || (result[1]<0)) {

           result[0] = -result[0];

           result[1] = -result[1];

       }

      

       return result;

   }

  

   /**

   * Adds two fractions

   *

   * @param f1 first fraction (numerator=[0], denominator=[1])

   * @param f2 second fraction (numerator=[0], denominator=[1])

   * @return result of adding parameters (numerator=[0], denominator=[1])

   */

   public static int[] addFractions(int[] f1, int[] f2) {

       int[] result = new int[2];

       result[0] = (f1[0] * f2[1]) + (f2[0] * f1[1]);

       result[1] = f1[1] * f2[1];

      

       return simplifyFraction(result);

   }

  

   /**

   * Subtracts two fractions

   *

   * @param f1 first fraction (numerator=[0], denominator=[1])

   * @param f2 second fraction (numerator=[0], denominator=[1])

   * @return result of subtracting parameter f2 from f1 (numerator=[0], denominator=[1])

   */

   public static int[] subtractFractions(int[] f1, int[] f2) {

      

      

      

      

      

       return new int[2];

   }

  

   /**

   * Multiplies two fractions

   *

   * @param f1 first fraction (numerator=[0], denominator=[1])

   * @param f2 second fraction (numerator=[0], denominator=[1])

   * @return result of multiplying parameters (numerator=[0], denominator=[1])

   */

   public static int[] multiplyFractions(int[] f1, int[] f2) {

      

      

      

      

      

      

       return new int[2];

   }

  

   /**

   * Divides two fractions

   *

   * @param f1 first fraction (numerator=[0], denominator=[1])

   * @param f2 second fraction (numerator=[0], denominator=[1])

   * @return result of dividing parameter f2 by f1 (numerator=[0], denominator=[1])

   */

   public static int[] divideFractions(int[] f1, int[] f2) {

      

      

      

      

      

       return new int[2];

   }

   public static void main(String[] args) {

      

   }

}

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Scanner;

public class LA5a {

   /**

   * Error to output if either denominator is zero

   */

   static final String E_DEN_ZERO = \"Denominator cannot be zero.\";

   /**

   * Error to output if dividing by zero

   */

   static final String E_DIV_ZERO = \"Cannot divide by zero.\";

   /**

   * Error to output if the operation is invalid

   */

   static final String E_OP_INVALID = \"Invalid operation.\";

   /**

   * Returns the greatest common divisor (gcd) of two integers

   *

   * @param num1 integer 1

   * @param num2 integer 2

   * @return gcd of integers 1 and 2

   */

   public static int gcd(int num1, int num2) {

       int t;

       while (num2 != 0) {

           t = num2;

           num2 = num1 % num2;

           num1 = t;

       }

       return num1;

   }

   /**

   * Returns the simplified form of a fraction

   *

   * @param f fraction (numerator=[0], denominator=[1])

   * @return simplified fraction (numerator=[0], denominator=[1])

   */

   public static int[] simplifyFraction(int[] f) {

       final int gcd = gcd(f[0], f[1]);

       int[] result = {f[0]/gcd, f[1]/gcd};

       if ((result[0]<0 && result[1]<0) || (result[1]<0)) {

           result[0] = -result[0];

           result[1] = -result[1];

       }

       return result;

   }

   /**

   * Adds two fractions

   *

   * @param f1 first fraction (numerator=[0], denominator=[1])

   * @param f2 second fraction (numerator=[0], denominator=[1])

   * @return result of adding parameters (numerator=[0], denominator=[1])

   */

   public static int[] addFractions(int[] f1, int[] f2) {

       int[] result = new int[2];

       result[0] = (f1[0] * f2[1]) + (f2[0] * f1[1]);

       result[1] = f1[1] * f2[1];

       return simplifyFraction(result);

   }

   /**

   * Subtracts two fractions

   *

   * @param f1 first fraction (numerator=[0], denominator=[1])

   * @param f2 second fraction (numerator=[0], denominator=[1])

   * @return result of subtracting parameter f2 from f1 (numerator=[0], denominator=[1])

   */

   public static int[] subtractFractions(int[] f1, int[] f2) {

       int[] result = new int[2];

       result[0] = (f1[0] * f2[1]) - (f2[0] * f1[1]);

       result[1] = f1[1] * f2[1];

       return simplifyFraction(result);

   }

   /**

   * Multiplies two fractions

   *

   * @param f1 first fraction (numerator=[0], denominator=[1])

   * @param f2 second fraction (numerator=[0], denominator=[1])

   * @return result of multiplying parameters (numerator=[0], denominator=[1])

   */

   public static int[] multiplyFractions(int[] f1, int[] f2) {

       int[] result = new int[2];

       result[0] = f1[0] * f2[0];

       result[1] = f1[1] * f2[1];

       return simplifyFraction(result);

   }

   /**

   * Divides two fractions

   *

   * @param f1 first fraction (numerator=[0], denominator=[1])

   * @param f2 second fraction (numerator=[0], denominator=[1])

   * @return result of dividing parameter f2 by f1 (numerator=[0], denominator=[1])

   */

   public static int[] divideFractions(int[] f1, int[] f2) {

       int[] result = new int[2];

       result[0] = f1[0] * f2[1];

       result[1] = f1[1] * f2[0];

       return simplifyFraction(result);

   }

  

   public static void inputFraction1(Scanner sc, int[] f1){

       System.out.println(\"Enter fraction: \");

       System.out.print(\"Numerator: \");

       f1[0] = sc.nextInt();

       System.out.print(\"Denominator: \");

       f1[1] = sc.nextInt();

       while(f1[1] == 0){

           System.out.print(\"Denominator (not zero): \");

           f1[1] = sc.nextInt();

       }

   }

  

   public static void inputFraction2(Scanner sc, int[] f2){

       System.out.println(\"Enter fraction: \");

       System.out.print(\"Numerator: \");

       f2[0] = sc.nextInt();

       while(f2[0] == 0){

           System.out.print(\"Numerator (not zero): \");

           f2[0] = sc.nextInt();

       }

       System.out.print(\"Denominator: \");

       f2[1] = sc.nextInt();

       while(f2[1] == 0){

           System.out.print(\"Denominator (not zero): \");

           f2[1] = sc.nextInt();

       }

   }

  

   public static void print(int[] f){

       System.out.println(f[0]+\"/\"+f[1]);

   }

  

   public static void main(String[] args) {

      

       int f1[] = new int[2];

       int f2[] = new int[2];

       int f[];

      

       Scanner sc = new Scanner(System.in);

      

       while(true){

           char op;

           int num, denom;

          

           System.out.println(\"Enter +. add -.subtract *.multiply /.divide e.Exit\");

           op = sc.next().charAt(0);

          

           switch(op){

           case \'+\' :

               inputFraction1(sc, f1);

               inputFraction1(sc, f2);

              

               f = addFractions(f1, f2);

               print(f);

               break;

           case \'-\' :

               inputFraction1(sc, f1);

               inputFraction1(sc, f2);

              

               f = subtractFractions(f1, f2);

               print(f);

               break;

           case \'*\' :

               inputFraction1(sc, f1);

               inputFraction1(sc, f2);

              

               f = multiplyFractions(f1, f2);

               print(f);

               break;

           case \'/\' :

               inputFraction1(sc, f1);

               inputFraction2(sc, f2);

              

               f = divideFractions(f1, f2);

               print(f);

               break;

           case \'e\' :

      

               break;

           default:

               System.out.println(\"Invalid opertor\");

           }

          

           if(op == \'e\')

               break;

          

       }

   }

}

/*

Sample run:

Enter +. add -.subtract *.multiply /.divide e.Exit

+

Enter fraction:

Numerator: 4

Denominator: 0

Denominator (not zero): 5

Enter fraction:

Numerator: 1

Denominator: 2

13/10

Enter +. add -.subtract *.multiply /.divide e.Exit

/

Enter fraction:

Numerator: 5

Denominator: 4

Enter fraction:

Numerator: 0

Numerator (not zero): 7

Denominator: 0

Denominator (not zero): 8

10/7

Enter +. add -.subtract *.multiply /.divide e.Exit

e

*/

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site