Fraction Java Program
This is for just for learning.
1)
Save it as fractionClass.java
package package_name;
/**
*
* @author <name>
*/
public class fractionClass {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FractionUtil.menuAssignment5();
}
}
2)
save it as Fraction.java
package package_name;
public class Fraction implements Cloneable, Comparable<Fraction> {
private int num;
private int denom;
public Fraction() {
denom = 1;
}
public Fraction(Fraction fr) {
num = fr.num;
denom = fr.denom;
}
public Fraction(int arg) {
num = arg;
denom = 1;
}
public Fraction(int arg1, int arg2) {
int gcd;
if (arg2 == 0) {
// You should check and throw and exception.
// For here, let's make it simple!
num = 0;
denom = 1;
} else { // You may want to update the logic below.
if (arg2 < 0) {
arg1 = -arg1;
arg2 = -arg2;
}
if (arg1 != 0) {
do { // Check if the loop is needed!
gcd = computeGcd(arg1, arg2);
num = arg1 / gcd;
denom = arg2 / gcd;
} while (gcd != 1);
} else {
// should we throw an exception here and return null?
num = 0;
denom = 1;
}
}
}
public int getNum() {
return num;
}
public int getDenom() {
return denom;
}
public void setNum(int arg) {
num = arg;
}
public void setDenom(int arg) {
if (arg < 0) {
num = -num;
denom = -arg;
} else if (arg > 0) {
denom = arg;
} else {
// You should check and throw and exception.
// For here, let's make it simple!
num = 0;
denom = 1;
}
}
// Let's use this gcd()
public int computeGcd(int arg1, int arg2) {
int gcd = 1;
int i;
arg1 = (arg1 < 0) ? -arg1 : arg1;
for (i = 2; i <= arg1 && i <= arg2; i++) {
if (arg1 % i == 0 && arg2 % i == 0) {
gcd = i;
}
}
return gcd;
}
public Fraction add(Fraction rOp) {
return new Fraction(num * rOp.denom + denom * rOp.num,
denom * rOp.denom);
}
public Fraction subtract(Fraction rOp) {
return new Fraction(num * rOp.denom - denom * rOp.num,
denom * rOp.denom);
}
public Fraction multiply(Fraction rOp) {
return new Fraction(num * rOp.num, denom * rOp.denom);
}
public Fraction divide(Fraction rOp) {
try {
Fraction fr1 = new Fraction(num * rOp.denom, denom * rOp.num);
return fr1;
} catch (ArithmeticException e) {
//System.out.println(e.getMessage());
//e.printStackTrace();
return null;
}
/*
The java.lang.throwable class provides the following methods:
+getMesage(): String
Returns the message the describes the exception object.
+toString(): String
Returns the concatenated string of (1) the fullname of the
exception class; (2) “: ” (a colon and a space); and (3) the
getMessage() method.
+printStackTrace(): void
Prints the Throwable object and its call stack trace
information.
+getStackTrace(): StackTraceElement[]
Returns an array of trace elements representing the stack
trace for the exception object.
*/
}
public void print() {
System.out.print("\n" + num + "/" + denom + "\n");
}
@Override
public Object clone() {
try {
//Fraction fr = (Fraction) super.clone();
//fr.num = num;
//fr.denom = denom;
//return fr;
return (Fraction) super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
@Override
public int compareTo(Fraction f) {
if (num * f.denom > denom * f.num)
return 1;
else if (num * f.denom < denom * f.num)
return -1;
else
return 0;
}
@Override
public String toString() {
return super.toString() + "\n num : " + num + "\n denom : " + denom;
}
}
3.
save it as FractionUtil.java
package package_name;
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;
}
}
Super mama
ReplyDelete