-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractionRunner.java
executable file
·246 lines (207 loc) · 8.68 KB
/
FractionRunner.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package apcs.fraction;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class FractionRunner {
public static void main(String[] args) throws InterruptedException {
Fraction f1 = new Fraction (1, 2); // represents one-half
System.out.print ("One Half = ");
System.out.println (f1); // should output 1 / 2
f1.setNumerator (3); // change numerator to 3
f1.setDenominator (4); // change denominator to 4
int num = f1.getNumerator (); // get the numerator
int den = f1.getDenominator (); // get the denominator
System.out.print ("Three Quarters = ");
System.out.println (num + "/" + den); // should output 3 / 4
Fraction f2 = new Fraction (2, 3);
System.out.print ("Two Thirds = ");
System.out.println (f2);
Fraction sum = f1.add(f2);
System.out.println (f1 + "+" + f2 + "=" + sum);
f2 = new Fraction (1, 2);
sum = f1.add(f2);
Fraction difference = f1.subtract(f2);
Fraction product = f1.multiply(f2);
Fraction quotient = f1.divide(f2);
System.out.println ("f1: " + f1);
System.out.println ("f2: " + f2);
System.out.println ("sum: " + sum);
System.out.println ("difference: " + difference);
System.out.println ("product: " + product);
System.out.println ("quotient: " + quotient);
f1 = new Fraction (1, 2);
f2 = new Fraction (1, 2);
System.out.println (f1 + "==" + f2 + " = " + f1.equals (f2));
System.out.println (f2 + "==" + f1 + " = " + f2.equals (f1));
f2 = new Fraction (1, 3);
System.out.println (f1 + "==" + f2 + " = " + f1.equals (f2));
System.out.println (f2 + "==" + f1 + " = " + f2.equals (f1));
f2 = new Fraction (2, 4);
System.out.println (f1 + "==" + f2 + " = " + f1.equals (f2));
System.out.println (f2 + "==" + f1 + " = " + f2.equals (f1));
//noinspection InfiniteLoopStatement
while (true) {
System.out.println("Compute-A-Fraction 5000.... Booting");
TimeUnit.SECONDS.sleep(2);
System.out.println("Ready for user input...");
System.out.print("Please specify a fraction. First, enter the numerator: ");
Scanner reader = new Scanner(System.in);
int n1 = reader.nextInt();
System.out.print("Now, specify a denominator: ");
int d1 = reader.nextInt();
Fraction firstFraction = new Fraction(n1, d1);
boolean repeat;
do {
System.out.print("What would you like to do today? (\"help\" for more options) ");
String input = reader.next().toLowerCase();
repeat = false;
switch (input) {
case "help":
System.out.println("The available commands are:\nexit, add, addInt, decrement, divide, divideInt, equals, greaterThan, increment, lessThan, lessThanOrEqualTo, multiply, multiplyInt, negate, simplify, subtract, and subtractInt.");
repeat = true;
break;
case "exit":
case "cancel":
case "stop":
case "restart":
repeat = false;
break;
case "add":
case "+":
case "plus":
System.out.println(
firstFraction.add(getSecondFraction())
);
break;
case "addint":
case "addi":
case "+i":
case "+int":
System.out.println(
firstFraction.addInt(getInt())
);
break;
case "decrement":
case "dec":
case "--":
System.out.println(
firstFraction.decrement()
);
break;
case "divide":
case "div":
case "/":
System.out.println(
firstFraction.divide(getSecondFraction())
);
break;
case "divideint":
case "divint":
case "divi":
case "/int":
case "/i":
System.out.println(
firstFraction.divideInt(getInt())
);
break;
case "equals":
case "equal":
case "=":
case "==":
case "===":
System.out.println(
firstFraction.equals(getSecondFraction())
);
break;
case "greaterThan":
case ">":
case "greater":
System.out.println(
firstFraction.greaterThan(getSecondFraction())
);
break;
case "increment":
case "inc":
case "++":
System.out.println(
firstFraction.increment()
);
break;
case "lessthan":
case "less":
case "<":
System.out.println(
firstFraction.lessThan(getSecondFraction())
);
break;
case "lessthanorequalto":
case "lessequal":
case "<=":
System.out.println(
firstFraction.lessThanOrEqualTo(getSecondFraction())
);
break;
case "multiply":
case "mult":
case "*":
System.out.println(
firstFraction.multiply(getSecondFraction())
);
break;
case "multiplyint":
case "multiplyi":
case "multint":
case "multi":
case "*int":
case "*i":
System.out.println(
firstFraction.multiplyInt(getInt())
);
break;
case "negate":
System.out.println(
firstFraction.negate()
);
break;
case "simplify":
System.out.println(
firstFraction.simplify()
);
break;
case "subtract":
case "sub":
case "-":
case "minus":
System.out.println(
firstFraction.subtract(getSecondFraction())
);
break;
case "subtractint":
case "subi":
case "subint":
case "-int":
case "-i":
System.out.println(
firstFraction.subtractInt(getInt())
);
default:
System.out.println("Command not recognised. Type \"help\" to receive a list of commands.");
repeat = true;
break;
}
} while (repeat);
}
}
public static Fraction getSecondFraction() {
System.out.print("Please specify a 2nd fraction. First, enter the numerator: ");
Scanner reader = new Scanner(System.in);
int n2 = reader.nextInt();
System.out.print("Now, specify a denominator: ");
int d2 = reader.nextInt();
return new Fraction(n2, d2);
}
public static int getInt() {
System.out.print("Please specify an int: ");
Scanner reader = new Scanner(System.in);
return reader.nextInt();
}
}