Hello I am incredibly confused on how to create this code Is
Hello! I am incredibly confused on how to create this code. Is there anyone here that can help? The assignment parameters are below.
Write a java program that works with fractions. You are to first 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. As a reminder for fraction arithmetic… ! ! + ! ! = !! + !! !! ! ! ! ! = !! !! !! ! ! × ! ! = !! !! ! ! ÷ ! ! = !! !! 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. 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 are two example runs of the program:
Enter the numerator for the first fraction: 1 Enter the denominator for the first fraction: 2 Enter the numerator for the second fraction: -4 Enter the denominator for the second fraction: 8 Enter the operation (+, -, *, /): + 1/2 + -4/8 = 0
Enter the numerator for the first fraction: 7 Enter the denominator for the first fraction: 8 Enter the numerator for the second fraction: 1 Enter the denominator for the second fraction: 3 Enter the operation (+, -, *, /): - 7/8 - 1/3 = 13/24
Solution
import java.util.*;
public class Calculator
{
public static void main(String []args)
{
int[][] num = new int[2][2];
char sign;
Scanner scan = new Scanner(System.in);
Scanner scan2 = new Scanner(System.in);
System.out.println(\"Enter the numerator for the first fraction: \");
num[0][0] = scan.nextInt();
System.out.println(\"Enter the denominator for the first fraction: \");
num[0][1] = scan.nextInt();
System.out.println(\"Enter the numerator for the second fraction: \");
num[1][0] = scan.nextInt();
System.out.println(\"Enter the denominator for the second fraction: \");
num[1][1] = scan.nextInt();
if((num[0][1] != 0) && (num[1][1] != 0))
{
System.out.println(\"Enter the operation (+, -, *, /): \");
sign = scan2.next().charAt(0);
switch(sign)
{
case \'+\': add(num); break;
case \'-\': sub(num); break;
case \'*\': multi(num); break;
case \'/\':
if(num[0][1] != 0)
div(num);
else
System.out.println(\"Numerarator of second fraction should not be zero\");
break;
default: System.out.println(\"Please enter right sign!\");
}
}
else
{
System.out.println(\"Denominator of both fractions should not be zero\");
}
}
public static void add(int[][] num)
{
int[] num2 = {0,0};
num2[0] = (num[0][0] * num[1][1]) + (num[0][1] * num[1][0]);
num2[1] = (num[0][1] * num[1][1]);
if(num2[0] == num2[1])
System.out.println(num[0][0]+\"/\"+num[0][1]+\" + \" + num[1][0]+\"/\"+num[1][1]+\" = 1\");
else if((num2[1] != 1) && (num2[0] != 0))
System.out.println(num[0][0]+\"/\"+num[0][1]+\" + \" + num[1][0]+\"/\"+num[1][1]+\" = \"+num2[0]+\"/\"+num2[1]);
else
System.out.println(num[0][0]+\"/\"+num[0][1]+\" + \" + num[1][0]+\"/\"+num[1][1]+\" = \"+num2[0]);
}
public static void sub(int[][] num)
{
int[] num2 = {0,0};
num2[0] = (num[0][0] * num[1][1]) - (num[0][1] * num[1][0]);
num2[1] = (num[0][1] * num[1][1]);
if(num2[0] == num2[1])
System.out.println(num[0][0]+\"/\"+num[0][1]+\" + \" + num[1][0]+\"/\"+num[1][1]+\" = 1\");
else if((num2[1] != 1) && (num2[0] != 0))
System.out.println(num[0][0]+\"/\"+num[0][1]+\" - \" + num[1][0]+\"/\"+num[1][1]+\" = \"+num2[0]+\"/\"+num2[1]);
else
System.out.println(num[0][0]+\"/\"+num[0][1]+\" - \" + num[1][0]+\"/\"+num[1][1]+\" = \"+num2[0]);
}
public static void multi(int[][] num)
{
int[] num2 = {0,0};
num2[0] = (num[0][0] * num[1][0]);
num2[1] = (num[0][1] * num[1][1]);
if(num2[0] == num2[1])
System.out.println(num[0][0]+\"/\"+num[0][1]+\" + \" + num[1][0]+\"/\"+num[1][1]+\" = 1\");
else if((num2[1] != 1) && (num2[0] != 0))
System.out.println(num[0][0]+\"/\"+num[0][1]+\" * \" + num[1][0]+\"/\"+num[1][1]+\" = \"+num2[0]+\"/\"+num2[1]);
else
System.out.println(num[0][0]+\"/\"+num[0][1]+\" * \" + num[1][0]+\"/\"+num[1][1]+\" = \"+num2[0]);
}
public static void div(int[][] num)
{
int[] num2 = {0,0};
num2[0] = (num[0][0] * num[1][1]);
num2[1] = (num[0][1] * num[1][0]);
if(num2[0] == num2[1])
System.out.println(num[0][0]+\"/\"+num[0][1]+\" + \" + num[1][0]+\"/\"+num[1][1]+\" = 1\");
else if((num2[1] != 1) && (num2[0] != 0))
System.out.println(num[0][0]+\"/\"+num[0][1]+\" / \" + num[1][0]+\"/\"+num[1][1]+\" = \"+num2[0]+\"/\"+num2[1]);
else
System.out.println(num[0][0]+\"/\"+num[0][1]+\" / \" + num[1][0]+\"/\"+num[1][1]+\" = \"+num2[0]);
}
}


