-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
327 lines (282 loc) · 11 KB
/
Calculator.java
File metadata and controls
327 lines (282 loc) · 11 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//Name: Wyatt Bechtle
//Date: 9 April 2023
//Program: Calculator Class
//----------------------------------------
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Calculator {
//Private instance variables and their default values.
private BigDecimal rx = new BigDecimal(0);
private BigDecimal ry = new BigDecimal(0);
private BigDecimal rm = new BigDecimal(0);
private BigDecimal rz = new BigDecimal(0);
private BigDecimal value;
private String command;
private String register;
//No arg. constructor.
public Calculator() {
}
//Calculate Methods...
//
//This method is used to parse and categories user input.
public String parseInput(String input) {
String errorMessageCommand = "ERROR: Invalid Command";
//Get string length.
int inputLength = input.length();
//Selection statements used to avoid exception.
if (inputLength >= 3) {
//Extract command.
command = input.substring(0, 3);
}
else {
command = "Short Input";
}
//Swith finds what command to call.
switch (input.length()) {
//ADD, SUB, MUL, DIV, ZMM
case 3:
//ADD
if (command.equals("ADD")) {
this.addCommand();
}
//SUB
else if (command.equals("SUB")) {
this.subCommand();
}
//MUL
else if (command.equals("MUL")) {
this.mulCommand();
}
//DIV
else if (command.equals("DIV")) {
this.divCommand();
}
//ZMM
else if (command.equals("ZMM")) {
this.zmmCommand();
}
else {
return errorMessageCommand;
}
break;
//SHO, CLR, MEM
case 6:
//Extract register.
register = input.substring(4, inputLength);
//SHO
if (command.equals("SHO")) {
String registerContents = this.shoCommand(register);
return registerContents;
}
//CLR
else if (command.equals("CLR")) {
String possibleErrorCode = this.clrCommand(register);
return possibleErrorCode;
}
//MEM
else if (command.equals("MEM")) {
String possibleErrorCode = this.memCommand(register);
return possibleErrorCode;
}
else {
return errorMessageCommand;
}
//MOV or Incorrect input.
default:
//Determine if MOV command.
if (command.equals("MOV")) {
//Extract register.
register = input.substring(input.indexOf(',') + 2, inputLength);
//Extract value and create variable to validate input.
String valueString = input.substring(4, input.indexOf(','));
boolean isNumeric = true;
int dotCount = 0;
//Check each character in the valueString.
for (int i = 0; i < valueString.length(); i++) {
char c = valueString.charAt(i);
//Check if not digit.
if (!Character.isDigit(c)) {
//Check if decimal point.
if (c == '.') {
if (dotCount == 0) {
dotCount++;
}
else {
//Can only be one decimal place.
isNumeric = false;
break;
}
}
else {
//Not a decimal or a digit, invalid.
isNumeric = false;
break;
}
}
}
//switch used to avoid exception.
switch (register) {
//move value to rx
case "rx":
//Only if numeric value.
if (isNumeric) {
//Create and pass big decimal value and register.
BigDecimal valueRX = new BigDecimal(valueString);
this.movCommand(valueRX, register);
}
else {
return "ERROR: Invalid Input";
}
break;
//move value to ry
case "ry":
//Only if numeric value.
if (isNumeric) {
//Create and pass big decimal value and register.
BigDecimal valueRY = new BigDecimal(valueString);
this.movCommand(valueRY, register);
}
else {
return "ERROR: Invalid Input";
}
break;
//move value to rm
case "rm":
//Only if numeric value.
if (isNumeric) {
//Create and pass big decimal value and register.
BigDecimal valueRM = new BigDecimal(valueString);
this.movCommand(valueRM, register);
}
else {
return "ERROR: Invalid Input";
}
break;
//Error, invalid register is entered.
default:
return errorMessageCommand;
}
}
//Error, invalid command is entered.
else {
return errorMessageCommand;
}
}
//Return empty string if successfull.
return "";
}
//This method is used to move operand one into operand two.
public String movCommand(BigDecimal value, String register) {
String errorMessageRegister = "ERROR: Invalid Register";
//switch used to avoid exception.
switch (register) {
//move value to rx
case "rx":
this.rx = value;
break;
//move value to ry
case "ry":
this.ry = value;
break;
//move value to rm
case "rm":
this.rm = value;
break;
//Error, invalid register is entered.
default:
return errorMessageRegister;
}
//Return empty string if successfull.
return "";
}
//This method is used to multiply x with y register.
public void mulCommand() {
this.rz = this.rx.multiply(this.ry);
}
//This method is used to divide x with y register.
public void divCommand() {
//.stripTrailingZeros() was not in the book, however, I learned about it while looking
//in RoundingMode.CEILING.
this.rz = this.rx.divide(this.ry, 20, RoundingMode.CEILING).stripTrailingZeros();
}
//This method is used to add x with y register.
public void addCommand() {
this.rz = this.rx.add(this.ry);
}
//This method is used to subtract x with y register.
public void subCommand() {
this.rz = this.rx.subtract(this.ry);
}
//This method is used to show the value of operand.
public String shoCommand(String register) {
String errorMessageRegister = "ERROR: Invalid Register";
switch (register) {
//Return rx register value.
case "rx":
return this.rx.toString();
//Return ry register value.
case "ry":
return this.ry.toString();
//Return rz register value.
case "rz":
return this.rz.toString();
//Return rm register value.
case "rm":
return this.rm.toString();
//Return ERROR message.
default:
return errorMessageRegister;
}
}
//This method is used to clear the specified operand.
public String clrCommand(String register) {
String errorMessageRegister = "ERROR: Invalid Register";
switch (register) {
//Clear rx register value and set default to 0.
case "rx":
this.rx = new BigDecimal(0);
break;
//Clear ry register value and set default to 0.
case "ry":
this.ry = new BigDecimal(0);
break;
//Clear rz register value and set default to 0.
case "rz":
this.rz = new BigDecimal(0);
break;
//Clear rm register value and set default to 0.
case "rm":
this.rm = new BigDecimal(0);
break;
//Return ERROR message.
default:
return errorMessageRegister;
}
//Return empty string if successfull.
return "";
}
//This method is used to move rz into rm and zero out rz.
public void zmmCommand() {
this.rm = this.rz;
this.rz = new BigDecimal(0);
}
//This method is used to assign a value from mem to the specified operand.
public String memCommand(String register) {
String errorMessageRegister = "ERROR: Invalid Register";
switch (register) {
//Assign rx register value.
case "rx":
this.rx = this.rm;
break;
//Assign ry register value.
case "ry":
this.ry = this.rm;
break;
//Return ERROR message.
default:
return errorMessageRegister;
}
//Return empty string if successfull.
return "";
}
}