This is for just for learning.
import java.util.Scanner;
public class FractionUtil {
public static Fraction add(Fraction f1, Fraction f2) {
return new Fraction(f1.getNum() * f2.getDenom() +
f1.getDenom() * f2.getNum(), f1.getDenom() * f2.getDenom());
}
public static Fraction subtract(Fraction f1, Fraction f2) {
return new Fraction(f1.getNum() * f2.getDenom() -
f1.getDenom() * f2.getNum(), f1.getDenom() * f2.getDenom());
}
public static Fraction multiply(Fraction f1, Fraction f2) {
return new Fraction(f1.getNum() * f2.getNum(), f1.getDenom() * f2.getDenom());
}
public static Fraction divide(Fraction f1, Fraction f2) {
return new Fraction(f1.getNum() * f2.getDenom(), f1.getDenom() * f2.getNum());
}
public static void addMenu(Fraction[] fAry) {
//Fraction[] frAry = new Fraction[2];
Fraction res = null;
int option;
Scanner scanner = new Scanner(System.in);
//fAry[1] = new Fraction(1, 3);
//fAry[2] = new Fraction(5, 6);
do {
System.out.print("\n\n*****" +
"\n* MENU -- addMenu()" +
"\n* (1) Member add()" +
"\n* (2) Stand-Alone add()" +
"\n* (3) Quit - addMenu()" +
"\n*****" +
"\nEnter your option (1 - 3): ");
option = scanner.nextInt();
switch (option) {
case 1:
System.out.print("\n Using lFr.add(rFr)\n");
res = fAry[0].add(fAry[1]);
// Fill in your code to display the result here
//Output of the Member add()
System.out.print(res);
break;
case 2:
System.out.print("\n Using lFr.add(rFr)\n");
res = add(fAry[0], fAry[1]);
// Fill in your code to display the result here
System.out.print(res);
break;
case 3:
System.out.println("\n Fun operations -- Quit!");
break;
default:
System.out.println("\n WRONG OPTION!");
}
} while (option != 3);
}
public static void subtractMenu(Fraction[] fAry) {
//Fraction[] frAry = new Fraction[2];
Fraction res = null;
int option;
Scanner scanner = new Scanner(System.in);
//fAry[1] = new Fraction(1, 3);
//fAry[2] = new Fraction(5, 6);
do {
System.out.print("\n\n*****" +
"\n* MENU -- subMenu()" +
"\n* (1) Member subtract()" +
"\n* (2) Stand-Alone subtract()" +
"\n* (3) Quit - subtractMenu()" +
"\n*****" +
"\nEnter your option (1 - 3): ");
option = scanner.nextInt();
switch (option) {
case 1:
System.out.print("\n Using lFr.subtract(rFr)\n");
res = fAry[0].subtract(fAry[1]);
// Fill in your code to display the result here
//Output of the Member subtract()
System.out.print(res);
break;
case 2:
System.out.print("\n Using lFr.subtract(rFr)\n");
res = subtract(fAry[0], fAry[1]);
// Fill in your code to display the result here
System.out.print(res);
break;
case 3:
System.out.println("\n Fun operations -- Quit!");
break;
default:
System.out.println("\n WRONG OPTION!");
}
} while (option != 3);
}
public static void multiplyMenu(Fraction[] fAry) {
//Fraction[] frAry = new Fraction[2];
Fraction res = null;
int option;
Scanner scanner = new Scanner(System.in);
//fAry[1] = new Fraction(1, 3);
//fAry[2] = new Fraction(5, 6);
do {
System.out.print("\n\n*****" +
"\n* MENU -- multiplyMenu()" +
"\n* (1) Member multiply()" +
"\n* (2) Stand-Alone multiply()" +
"\n* (3) Quit - multiplyMenu()" +
"\n*****" +
"\nEnter your option (1 - 3): ");
option = scanner.nextInt();
switch (option) {
case 1:
System.out.print("\n Using lFr.multiply(rFr)\n");
res = fAry[0].multiply(fAry[1]);
// Fill in your code to display the result here
//Output of the Member multiply()
System.out.print(res);
break;
case 2:
System.out.print("\n Using lFr.multiply(rFr)\n");
res = multiply(fAry[0], fAry[1]);
// Fill in your code to display the result here
System.out.print(res);
break;
case 3:
System.out.println("\n Fun operations -- Quit!");
break;
default:
System.out.println("\n WRONG OPTION!");
}
} while (option != 3);
}
public static void divideMenu(Fraction[] fAry) {
//Fraction[] frAry = new Fraction[2];
Fraction res = null;
int option;
Scanner scanner = new Scanner(System.in);
//fAry[1] = new Fraction(1, 3);
//fAry[2] = new Fraction(5, 6);
do {
System.out.print("\n\n*****" +
"\n* MENU -- divideMenu()" +
"\n* (1) Member divide()" +
"\n* (2) Stand-Alone divide()" +
"\n* (3) Quit - divideMenu()" +
"\n*****" +
"\nEnter your option (1 - 3): ");
option = scanner.nextInt();
switch (option) {
case 1:
System.out.print("\n Using lFr.divide(rFr)\n");
res = fAry[0].divide(fAry[1]);
// Fill in your code to display the result here
//Output of the Member divide()
System.out.print(res);
break;
case 2:
System.out.print("\n Using lFr.divide(rFr)\n");
res = divide(fAry[0], fAry[1]);
// Fill in your code to display the result here
System.out.print(res);
break;
case 3:
System.out.println("\n Fun operations -- Quit!");
break;
default:
System.out.println("\n WRONG OPTION!");
}
} while (option != 3);
}
public static void init(Fraction[] fAry) {
int option;
int n;
int d;
Scanner scanner = new Scanner(System.in);
do {
System.out.print("\n\n*****" +
"\n* MENU -- init()" +
"\n* (1) Creating 2 Fraction Objects" +
"\n* (2) Updating 2 Fraction Objects" +
"\n* (3) Updating LEFT Fraction Object" +
"\n* (4) Updating RIGHT Fraction Object" +
"\n* (5) Quit - init()" +
"\n*****" +
"\nEnter your option (1 - 5): ");
option = scanner.nextInt();
switch (option) {
case 1:
System.out.print("\nCreating 2 Fraction objects ..." +
"\n For the left Fraction -" +
"\n Enter the numerator: ");
n = scanner.nextInt();
System.out.print("\n Enter the denominator: ");
d = scanner.nextInt();
fAry[0] = new Fraction(n, d);
System.out.print("\n For the right Fraction -" +
"\n Enter the numerator: ");
n = scanner.nextInt();
System.out.print("\n Enter the denominator: ");
d = scanner.nextInt();
fAry[1] = new Fraction(n, d);
//SsaiK Added
//addMenu(fAry);
break;
case 2: // There MUST be 2 existing Fraction objects
System.out.print("\nUpdatting 2 Fraction objects ..." +
"\n For the left Fraction -" +
"\n Enter the numerator: ");
n = scanner.nextInt();
fAry[0].setNum(n);
System.out.print("\n Enter the denominator: ");
d = scanner.nextInt();
fAry[0].setDenom(d);
System.out.print("\n For the right Fraction -" +
"\n Enter the numerator: ");
n = scanner.nextInt();
fAry[1].setNum(n);
System.out.print("\n Enter the denominator: ");
d = scanner.nextInt();
fAry[1].setDenom(d);
break;
case 3:
System.out.print("\nUpdating left Fraction object ...!\n Enter the left numerator: ");
n = scanner.nextInt();
fAry[0].setNum(n);
System.out.print("\n Enter the left denominator: ");
d = scanner.nextInt();
fAry[0].setDenom(d);
System.out.println("Left fraction updated successfully");
break;
case 4:
System.out.print("\nUpdating right Fraction object ...!\n Enter the right numerator: ");
n = scanner.nextInt();
fAry[1].setNum(n);
System.out.print("\n Enter the right denominator: ");
d = scanner.nextInt();
fAry[1].setDenom(d);
System.out.println("Right fraction updated successfully");
break;
case 5:
System.out.print("\nQuit ...!\n");
break;
default:
System.out.print("\nWRONG OPTION!\n");
}
} while (option != 5);
}
public static void menuAssignment5() {
Fraction[] frAry = new Fraction[2];
int option;
Scanner scanner = new Scanner(System.in);
do {
System.out.print("\n\n*****" +
"\n* MENU -- Assignment 5" +
"\n* (1) Calling init()" +
"\n* (2) Addition" +
"\n* (3) Subtraction" +
"\n* (4) Multiplication" +
"\n* (5) Division" +
"\n* (6) GCD" +
"\n* (7) Quit" +
"\n*****" +
"\nEnter your option (use integer value only): ");
option = scanner.nextInt();
switch (option) {
case 1:
System.out.print("\nCalling init() ...");
init(frAry); // FractionUtil.init(frAry);
break;
case 2:
System.out.print("\nPerforming the addition ...!\n");
addMenu(frAry);
break;
case 3:
System.out.print("\nPerforming the subtraction ...!\n");
subtractMenu(frAry);
break;
case 4:
System.out.print("\nPerforming the multiplication ...!\n");
multiplyMenu(frAry);
break;
case 5:
System.out.print("\nPerforming the GCD ...!\n");
divideMenu(frAry);
break;
case 6:
System.out.print("\nPerforming the GCD ...!\n");
System.out.print("GCD of the left fraction is"+getGcd(frAry[0].getNum(),frAry[0].getDenom()));
System.out.print("GCD of the right fraction is"+getGcd(frAry[1].getNum(),frAry[1].getDenom()));
break;
case 7:
System.out.print("\nQuit ...!\n");
break;
default:
System.out.print("\nWRONG OPTION!\n");
}
} while (option != 7);
}
// Let's use this gcd()
public static int getGcd(int arg1, int arg2) {
int gcd = 1;
int i;
arg1 = (arg1 < 0) ? -arg1 : arg1;
arg2 = (arg2 < 0) ? -arg2 : arg2;
for (i = 2; i <= arg1 && i <= arg2; i++) {
if (arg1 % i == 0 && arg2 % i == 0) {
gcd = i;
}
}
return gcd;
}
}