Answer:
Explanation:
The following code is written in Java, It asks the user to enter the numerator and denominator for both fraction 1 and 2. Then it prompts the user with a menu to choose the desired operation. The choice is passed into a switch statement and calls the correct function.
import java.util.Scanner;
class Brainly {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1, num2, den1, den2;
System.out.println("Enter numerator for fraction 1: ");
num1 = in.nextInt();
System.out.println("Enter denominator for fraction 1: ");
den1 = in.nextInt();
System.out.println("Enter numerator for fraction 2: ");
num2 = in.nextInt();
System.out.println("Enter denominator for fraction 2: ");
den2 = in.nextInt();
System.out.println("Menu:");
System.out.println("+ = add fractions");
System.out.println("- = subtract fractions");
System.out.println("/ = divide fractions");
System.out.println("* = multiply fractions");
String answer = in.next();
switch (answer.charAt(0)) {
case '+': add(num1, den1, num2, den2); break;
case '-': subtract(num1, den1, num2, den2); break;
case '*': multiply(num1, den1, num2, den2); break;
case '/': divide(num1, den1, num2, den2); break;
}
}
public static void add(int num1, int den1, int num2, int den2) {
int num3 = (num1 * den2) + (num2 * den1);
int den3 = den1 * den2;
System.out.println("New Fraction: " + num3 + " / " + den3);
}
public static void subtract(int num1, int den1, int num2, int den2) {
int num3 = (num1 * den2) - (num2 * den1);
int den3 = den1 * den2;
System.out.println("New Fraction: " + num3 + " / " + den3);
}
public static void divide(int num1, int den1, int num2, int den2) {
int num3 = num1 * den2;
int den3 = den1 * num2;
System.out.println("New Fraction: " + num3 + " / " + den3);
}
public static void multiply(int num1, int den1, int num2, int den2) {
int num3 = num1 * num2;
int den3 = den1 * den2;
System.out.println("New Fraction: " + num3 + " / " + den3);
}
}