Repository navigation
Expand file tree
/
Copy pathCalculatorModel.java
More file actions
560 lines (510 loc) · 12.3 KB
/
Copy pathCalculatorModel.java
File metadata and controls
560 lines (510 loc) · 12.3 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import java.util.*;
/**
* CalculatorModel.java
*
* CS 204 Final Project- Graphing Calculator
*
* @author Abby Lewis, Jamaica Lammi, Danny Hanson, Sam Keyes
*
* Class for implementing the correct button actions for the
* calculator. This class is the model, so it contains
* everything the calculator needs to know about calculations.
*
*/
public class CalculatorModel {
private ArrayList<String> currentEq;
private ScriptEngine solver;
private int size;
/**
* Constructor creates a script engine to solve equations,
* then initializes an ArrayList and sets the size to zero.
*/
public CalculatorModel() {
ScriptEngineManager mgr = new ScriptEngineManager();
solver = mgr.getEngineByName("JavaScript");
currentEq = new ArrayList<String>();
size = 0;
}
/**
* Adds the correct symbol to the equation based on the String
* parameter action and returns an Array of Strings.
*/
public String[] performAction(String action) {
// Initializes an Array of Strings of size 2
String[] finalEquation = new String[2];
// If the input is "Enter" evaluate the equation
if (action.equals("Enter")) {
return this.evaluate();
}
// basic operators
else if (action.equals("+")) {
this.plus();
}
else if (action.equals("-")) {
this.minus();
}
else if (action.equals("*")) {
this.times();
}
else if (action.equals("/")) {
this.divide();
}
else if (action.equals("(-)")) {
this.negative();
}
// parentheses
else if (action.equals("(")) {
this.openParen();
}
else if (action.equals(")")) {
this.closeParen();
}
// trigonometric operations
else if (action.equals("sin()")) {
this.sin();
}
else if (action.equals("cos()")) {
this.cos();
}
else if (action.equals("tan()")) {
this.tan();
}
else if (action.equals("pi")) {
this.pi();
}
// logarithms
else if (action.equals("ln()")) {
this.ln();
}
else if (action.equals("e")) {
this.e();
}
// powers
else if (action.equals("x^2")) {
this.square();
}
else if (action.equals("sqrt")) {
this.sqrt();
}
else if (action.equals("^")) {
this.power();
}
// delete
else if (action.equals("Delete")) {
this.delete();
}
// clear
else if (action.equals("Clear")) {
this.clear();
}
else if (action.equals("<html>"+"Clear"+"<br>"+"All"+"<html>")) {
this.clear();
}
else if (action.equals("<html>"+"Clear"+"<br>"+"Graph"+"<html>")) {
this.clear();
}
// decimal point
else if (action.equals(".")) {
this.decimal();
}
// digits
else if (action.matches("[0-9]")) {
this.number(action);
}
// variable x
else if (action.matches("x")) {
this.number(action);
}
// Returns the displayable form of the equation
finalEquation[0]=equationToNiceForm(this.copyEquation());
return finalEquation;
}
/**
* Clears the current equation by making a new ArrayList of Strings
* of size 0.
*/
public void clear() {
currentEq = new ArrayList<String>();
size = 0;
}
/**
* Deletes the most recent character in the equation
*/
public void delete() {
if (currentEq.size() > 0) {
String temp = currentEq.get(size-1);
// Ensures only one digit is deleted
if (isNum(temp)) {
if (temp.length()>1) {
currentEq.set(size-1, temp.substring(0,temp.length()-1));
} else {
currentEq.remove(size-1);
size--;
}
}
// Ensures that the entire power function is deleted
else if (temp.charAt(0)==',') {
currentEq.remove(size-1);
int i = 2;
while (!currentEq.get(size-i).equals("Math.pow(")) {
i++;
}
currentEq.remove(size-i);
size-=2;
}
// Otherwise delete the most recent thing entered
else {
currentEq.remove(size-1);
size--;
}
}
}
/**
* Adds a digit to the equation
*/
public void number(String num) {
if (size>0) {
String temp = currentEq.get(size-1);
if (isNum(temp)) {
currentEq.set(size-1, temp+num);
} else {
currentEq.add(num);
size++;
}
} else {
currentEq.add(num);
size++;
}
}
/**
* Adds a decimal point to the equation
*/
public void decimal() {
if (size>0) {
String temp = currentEq.get(size-1);
if (isNum(temp)) {
currentEq.set(size-1, temp+".");
} else {
currentEq.add(".");
size++;
}
} else {
currentEq.add(".");
size++;
}
}
/**
* The next few functions add the basic operators
*/
public void plus() {
currentEq.add("+");
size++;
}
public void minus() {
currentEq.add("-");
size++;
}
public void times() {
currentEq.add("*");
size++;
}
public void divide() {
currentEq.add("/");
size++;
}
public void negative() {
currentEq.add("(-1)*");
size++;
}
/**
* The next few functions add the parentheses
*/
public void openParen() {
currentEq.add("(");
size++;
}
public void closeParen() {
currentEq.add(")");
size++;
}
/**
* The next few functions add the trigonometric operations
*/
public void sin() {
currentEq.add("Math.sin(");
size++;
}
public void cos() {
currentEq.add("Math.cos(");
size++;
}
public void tan() {
currentEq.add("Math.tan(");
size++;
}
public void pi() {
currentEq.add("Math.PI");
size++;
}
/**
* The next few functions add the logarithms
*/
public void ln() {
currentEq.add("Math.log(");
size++;
}
public void e() {
currentEq.add("Math.E");
size++;
}
/**
* Adds the "x" variable
*/
public void x() {
currentEq.add("x");
size++;
}
/**
* Adds the sqrt function
*/
public void sqrt() {
currentEq.add("Math.sqrt(");
size++;
}
/**
* Adds the "square" function
*/
public void square() {
// Checks for different cases to figure out where to add "Math.pow(" and ",2)"
if (size>0) {
String temp = currentEq.get(size-1);
Stack<String> tempStack = new Stack<String>();
if (isNum(temp) || temp.equals("x")) {
currentEq.add(size-1, "Math.pow(");
currentEq.add(",2)");
size+=2;
} else if (temp.equals(")")) {
tempStack.push(")");
int i = 2;
while (!tempStack.empty()) {
String temp2 = currentEq.get(size-i);
if (temp2.equals(")")) {
tempStack.push(")");
} else if (temp2.equals("(")) {
tempStack.pop();
} else if (temp2.matches("Math.+[(]")) {
tempStack.pop();
}
i ++;
}
currentEq.add(size-i, "Math.pow(");
currentEq.add(",2)");
size+=2;
} else if (temp.matches("Math.(E|(PI))")) {
currentEq.add(size-1, "Math.pow(");
currentEq.add(",2)");
size+=2;
}
}
}
/**
* Adds the power function
*/
public void power() {
// Checks for different cases to figure out where to add "Math.pow(" and ","
if (size>0) {
String temp = currentEq.get(size-1);
Stack<String> tempStack = new Stack<String>();
if (isNum(temp) || temp.equals("x")) {
currentEq.add(size-1, "Math.pow(");
currentEq.add(",");
size+=2;
} else if (temp.equals(")")) {
tempStack.push(")");
int i = 2;
while (!tempStack.empty()) {
String temp2 = currentEq.get(size-i);
if (temp2.equals(")")) {
tempStack.push(")");
} else if (temp2.equals("(")) {
tempStack.pop();
} else if (temp2.matches("Math.+[(]")) {
tempStack.pop();
}
i ++;
}
i--;
currentEq.add(size-i, "Math.pow(");
currentEq.add(",");
size+=2;
} else if (temp.matches("Math.(E|(PI))")) {
currentEq.add(size-1, "Math.pow(");
currentEq.add(",");
size+=2;
}
}
}
/**
* Used so we can get a copy of the current equation,
* it returns an ArrayList of Strings which represent
* the equation.
*/
public ArrayList<String> copyEquation() {
return (ArrayList<String>) currentEq.clone();
}
/**
* Evaluates the equation and returns an Array of Strings
* where the first entry is the displayable form of the equation,
* and the second entry is the answer.
*/
public String[] evaluate() {
String[] evaluatedEquation = new String[2];
// Creates a displayable form of the equation
String displayableEquation = equationToNiceForm(copyEquation());
evaluatedEquation[0] = displayableEquation;
// Makes the equation readable by a ScriptEngine
String readableEquation = javascriptEquation();
String fixedParen = parenthesesChecker(readableEquation);
// Rounds to 6 decimal points
String withRounding = "Math.round(1000000*(" + fixedParen + "))/1000000";
String tempSolution = "";
// Equation is evaluated here and catches any errors
try {
tempSolution = solver.eval(withRounding).toString();
} catch (Exception e) {
tempSolution = "Error";
}
evaluatedEquation[1] = tempSolution;
// Resets the equation to size 0
currentEq = new ArrayList<String>();
size=0;
return evaluatedEquation;
}
/************************************
* SECTION CHANGE: GRAPH MODEL HERE *
***********************************/
/**
* Evaluates the equation over the range x = -10 to x = 10,
* and returns an Array of Strings which contain the y-values
* for the graph.
*/
public String[] evaluateGraph() {
String[] solutionArray = new String[600];
// Gets the equation to a form that the ScriptEngine can read
String readableEquation = javascriptEquation();
String fixedParen = parenthesesChecker(readableEquation);
String scaledEquation = "30*(" + fixedParen + ")";
// Loops through possible x values
for (int i =-300; i<300; i++) {
// Scales x value so it fits with 20 by 20 grid
double scaleFactor = i*1/30.0;
// Solves equations at a given x value
String graphedEq = replaceX(scaledEquation, Double.toString(scaleFactor));
String tempSolution = "";
try {
tempSolution = solver.eval(graphedEq).toString();
} catch (Exception e) {
tempSolution = "0";
}
solutionArray[i+300] = tempSolution;
}
// Resets current equation
currentEq = new ArrayList<String>();
size=0;
return solutionArray;
}
/******************
* HELPER METHODS *
*****************/
/**
* Returns a String with all x's in the parameter equation with a number
*/
private String replaceX(String equation, String num){
String output = new String(equation);
// Loops through the output equation and if an "x" is found replace it with the correct number
for (int i=0;i<output.length();i++){
if (output.charAt(i) == 'x'){
String firstPart = output.substring(0, i);
String secondPart = output.substring(i+1);
output = "";
output = output.concat(firstPart);
output = output.concat(num);
output = output.concat(secondPart);
}
}
return output;
}
/**
* Checks if a value is a number and returns a boolean
*/
private boolean isNum(String nm) {
if (nm.matches("[0-9]+.?[0-9]*")) {
return true;
}
return false;
}
/**
* Converts the current equation to readable form for a ScriptEngine
*/
private String javascriptEquation() {
String currentEquation = "";
for (int i = 0; i<size; i++) {
if (i<size-1) {
if (isNum(currentEq.get(i)) && currentEq.get(i+1).matches("Math.+")) {
currentEq.add(i+1, "*");
size++;
}
}
currentEquation += currentEq.get(i);
}
return currentEquation;
}
/**
* Converts the equation to a displayable form for text areas
*/
private String equationToNiceForm(ArrayList<String> eq) {
String currentEquation = "";
for (int i = 0; i<eq.size(); i++) {
if (i<eq.size()-1) {
if (isNum(eq.get(i)) && eq.get(i+1).matches("Math.+")) {
eq.add(i+1, "*");
}
} if (eq.get(i).equals("Math.pow(")) {
eq.remove(i);
} if (eq.get(i).matches("Math.+")) {
String replace = eq.get(i).substring(5);
eq.set(i, replace);
} if (eq.get(i).equals(",")) {
eq.set(i, "^(");
} if (eq.get(i).equals(",2)")) {
eq.set(i, "^2");
} if (eq.get(i).equals("(-1)*")) {
eq.set(i, "(-)");
}
currentEquation += eq.get(i);
}
return currentEquation;
}
/**
* Adds in all missing parentheses
*/
private String parenthesesChecker(String checkedEq) {
String withParens = new String(checkedEq);
Stack<String> parenStack = new Stack<String>();
for (int i=0; i<checkedEq.length(); i++) {
if (withParens.charAt(i) == '(') {
parenStack.push("off cliff");
}
if (withParens.charAt(i) == ')' && !parenStack.empty()) {
parenStack.pop();
}
}
while (!parenStack.empty()) {
withParens += ")";
parenStack.pop();
}
return withParens;
}
}