-
Notifications
You must be signed in to change notification settings - Fork 0
/
FractionCalculator.java
146 lines (124 loc) · 6.48 KB
/
FractionCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Part 2 - FractionCalculator Class
*/
import java.util.Scanner;
public class FractionCalculator {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// Part 3 - Putting it all together!
// 1. Write a short introduction method that describes the calculator program and welcomes your user
System.out.println(" \t NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
System.out.println(" \t NN NN");
System.out.println(" \t NN FRACTION CALCULATOR NN");
System.out.println(" \t NN NN");
System.out.println(" \t NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
System.out.println();
System.out.println(" \t This program allows the user to perform standard");
System.out.println(" \t mathematical functions on regular fractions. It");
System.out.println(" \t add, subtract, multiply and divide fractions until");
System.out.println(" \t you type Q to quit.");
System.out.println();
System.out.println("Please enter your fractions in the form a/b, where a and b are integers.");
System.out.println("-------------------------------------------------------------------------");
System.out.println();
int i = 0;
do {
String operation = getOperation();
Fraction frac1 = getFraction();
Fraction frac2 = getFraction();
if (operation.equals("+")) {
Fraction sum = frac1.add(frac2);
System.out.print(frac1.toString() + " + " + frac2.toString() + " = " + sum.toString());
Fraction sumLow = new Fraction(sum.getNumerator(), sum.getDenominator());
sumLow.toLowestTerms();
if (!(sum.toString().equals(sumLow.toString()))) { System.out.print(" -> " + sumLow.toString()); }
System.out.print("\n");
} else if (operation.equals("-")) {
Fraction rem = frac1.subtract(frac2);
System.out.println(frac1.toString() + " - " + frac2.toString() + " = " + rem.toString());
Fraction remLow = new Fraction(rem.getNumerator(), rem.getDenominator());
remLow.toLowestTerms();
if (!(rem.toString().equals(remLow.toString()))) { System.out.print(" -> " + remLow.toString()); }
System.out.print("\n");
} else if (operation.equals("/")) {
Fraction quotient = frac1.divide(frac2);
System.out.println(frac1.toString() + " / " + frac2.toString() + " = " + quotient.toString());
Fraction quoLow = new Fraction(quotient.getNumerator(), quotient.getDenominator());
quoLow.toLowestTerms();
if (!(quotient.toString().equals(quoLow.toString()))) { System.out.print(" -> " + quoLow.toString()); }
System.out.print("\n");
} else if (operation.equals("*")) {
Fraction product = frac1.multiply(frac2);
System.out.println(frac1.toString() + " * " + frac2.toString() + " = " + product.toString());
Fraction pdtLow = new Fraction(product.getNumerator(), product.getDenominator());
pdtLow.toLowestTerms();
if (!(product.toString().equals(pdtLow.toString()))) { System.out.print(" -> " + pdtLow.toString()); }
System.out.print("\n");
} else {
boolean answer = frac1.equals(frac2);
System.out.println(frac1.toString() + " = " + frac2.toString() + " is " + answer);
}
System.out.println("-------------------------------------------------------------------------");
} while (i == 0);
}
public static String getOperation() {
System.out.print("Please enter an operation (+, -, /, *, = or Q to quit): ");
String perform = input.nextLine();
if (!(perform.equals("+")) && !(perform.equals("-")) && !(perform.equals("/")) && !(perform.equals("*")) && !(perform.equals("=")) && !(perform.equalsIgnoreCase("Q"))) {
while (!(perform.equals("+")) && !(perform.equals("-")) && !(perform.equals("/")) && !(perform.equals("*")) && !(perform.equals("=")) && !(perform.equalsIgnoreCase("Q"))) {
System.out.print("Invalid input (+, -, /, *, = or Q to quit): ");
perform = input.nextLine();
}
}
if (perform.equalsIgnoreCase("q")) {
System.exit(0);
}
return perform;
}
public static boolean validFraction(String check) {
if (check.contains("/")) {
String[] data = check.split("/");
if (data[0].matches("-?\\d+")) {
if (data[1].matches("-?\\d+")) {
int data1 = Integer.parseInt(data[1]);
if (data1 > 0) {
return true;
} else { // Negative denom. passed!
return false;
}
} else { // Non-integer denom. passed!
return false;
}
} else { // Non-integer numerator passed!
return false;
}
} else {
if (check.matches("-?\\d+")) {
return true;
} else { // Non-integer data passed!
return false;
}
}
}
public static Fraction getFraction() {
System.out.print("Please enter a fraction (a/b) or integer (a): ");
String usrIn = input.nextLine();
if (!validFraction(usrIn)) {
while (!validFraction(usrIn)) {
System.out.print("Invalid Fraction. Please enter (a/b) or (a), where a and b are integers and b is not zero: ");
usrIn = input.nextLine();
}
}
// Convert to integers to pass as Fraction
int[] toInts = new int[2];
if (usrIn.contains("/")) {
String[] split = usrIn.split("/");
toInts[0] = Integer.parseInt(split[0]);
toInts[1] = Integer.parseInt(split[1]);
} else {
toInts[0] = Integer.parseInt(usrIn);
toInts[1] = 1;
}
return new Fraction(toInts[0], toInts[1]);
}
}