|
@@ -17,6 +17,20 @@ import rocks.zipcode.calcskin.CalcEngine;
|
17
|
17
|
import java.util.HashMap;
|
18
|
18
|
import java.util.Map;
|
19
|
19
|
|
|
20
|
+/*
|
|
21
|
+
|
|
22
|
+- Making a simple 4-function calculator should be trivial for you at this stage.
|
|
23
|
+
|
|
24
|
+- BUT, how about adding a column of buttons for x-squared, square-root, inverse (1/x), and exponent (x^y).
|
|
25
|
+
|
|
26
|
+- Then you might add a column of buttons that do the Memory functions. (What should you add to the UI to make this easier??)
|
|
27
|
+
|
|
28
|
+Finally, work on adding the trig and log functions from the original lab.
|
|
29
|
+
|
|
30
|
+Feel free to work in your original groups, get everyone up to speed on this!
|
|
31
|
+
|
|
32
|
+ */
|
|
33
|
+
|
20
|
34
|
// a simple JavaFX calculator.
|
21
|
35
|
public class CalcSkin extends Application {
|
22
|
36
|
|
|
@@ -24,19 +38,24 @@ public class CalcSkin extends Application {
|
24
|
38
|
launch(args);
|
25
|
39
|
}
|
26
|
40
|
private static final String[][] template = {
|
27
|
|
- { "7", "8", "9", "/" },
|
28
|
|
- { "4", "5", "6", "*" },
|
29
|
|
- { "1", "2", "3", "-" },
|
30
|
|
- { "0", "c", "=", "+" }
|
|
41
|
+ { "7", "8", "9", "/" },
|
|
42
|
+ { "4", "5", "6", "*" },
|
|
43
|
+ { "1", "2", "3", "-" },
|
|
44
|
+ { "0", "c", "=", "+" },
|
|
45
|
+ { "x^2", "rt", "1/x", "x^y" },
|
|
46
|
+ { "cos", "sin", "tan", "log10" },
|
|
47
|
+ { "acos","asin", "atan" },
|
|
48
|
+ {"store", "use","reset" }
|
31
|
49
|
};
|
32
|
50
|
|
33
|
51
|
private final Map<String, Button> accelerators = new HashMap<>();
|
34
|
52
|
|
35
|
53
|
private DoubleProperty previousValue = new SimpleDoubleProperty();
|
36
|
54
|
private DoubleProperty currentValue = new SimpleDoubleProperty();
|
|
55
|
+ private DoubleProperty memoryValue = new SimpleDoubleProperty();
|
37
|
56
|
private CalcEngine calcEngine = new CalcEngine();
|
38
|
57
|
|
39
|
|
- private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
|
|
58
|
+ private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUARE, ROOT, INVERSE, EXPO, COS, SIN, TAN, LOG, ACOS, ASIN, ATAN, STORE, USE, RESET }
|
40
|
59
|
|
41
|
60
|
private Op curOp = Op.NOOP;
|
42
|
61
|
private Op stackOp = Op.NOOP;
|
|
@@ -73,6 +92,7 @@ public class CalcSkin extends Application {
|
73
|
92
|
public void handle(KeyEvent keyEvent) {
|
74
|
93
|
Button activated = accelerators.get(keyEvent.getText());
|
75
|
94
|
if (activated != null) {
|
|
95
|
+ System.out.println("line 94 fired");
|
76
|
96
|
activated.fire();
|
77
|
97
|
}
|
78
|
98
|
}
|
|
@@ -84,7 +104,7 @@ public class CalcSkin extends Application {
|
84
|
104
|
screen.setStyle("-fx-background-color: aquamarine;");
|
85
|
105
|
screen.setAlignment(Pos.CENTER_RIGHT);
|
86
|
106
|
screen.setEditable(false);
|
87
|
|
- screen.textProperty().bind(Bindings.format("%.0f", currentValue));
|
|
107
|
+ screen.textProperty().bind(Bindings.format("%.2f", currentValue));
|
88
|
108
|
return screen;
|
89
|
109
|
}
|
90
|
110
|
|
|
@@ -109,14 +129,40 @@ public class CalcSkin extends Application {
|
109
|
129
|
} else {
|
110
|
130
|
final ObjectProperty<Op> triggerOp = determineOperand(s);
|
111
|
131
|
if (triggerOp.get() != Op.NOOP) {
|
|
132
|
+ System.out.println(triggerOp.get());
|
112
|
133
|
makeOperandButton(button, triggerOp);
|
113
|
134
|
} else if ("c".equals(s)) {
|
114
|
135
|
makeClearButton(button);
|
115
|
136
|
} else if ("=".equals(s)) {
|
116
|
137
|
makeEqualsButton(button);
|
|
138
|
+ } else if("x^2".equals(s)) {
|
|
139
|
+ makeSquareBtn(button);
|
|
140
|
+ } else if("rt".equals(s)) {
|
|
141
|
+ makeSquareRtBtn(button);
|
|
142
|
+ } else if("1/x".equals(s)){
|
|
143
|
+ makeInverseBtn(button);
|
|
144
|
+ } else if("cos".equals(s)){
|
|
145
|
+ makeCOSBtn(button);
|
|
146
|
+ } else if("sin".equals(s)){
|
|
147
|
+ makeSINBtn(button);
|
|
148
|
+ } else if("tan".equals(s)){
|
|
149
|
+ makeTANBtn(button);
|
|
150
|
+ } else if("log10".equals(s)){
|
|
151
|
+ makeLOGBtn(button);
|
|
152
|
+ } else if("acos".equals(s)){
|
|
153
|
+ makeCOSBtn(button);
|
|
154
|
+ } else if("asin".equals(s)){
|
|
155
|
+ makeSINBtn(button);
|
|
156
|
+ } else if("atan".equals(s)){
|
|
157
|
+ makeTANBtn(button);
|
|
158
|
+ }else if("store".equals(s)){
|
|
159
|
+ makeStoreBtn(button);
|
|
160
|
+ } else if("use".equals(s)){
|
|
161
|
+ makeUseBtn(button);
|
|
162
|
+ } else if("reset".equals(s)) {
|
|
163
|
+ makeResetBtn(button);
|
117
|
164
|
}
|
118
|
165
|
}
|
119
|
|
-
|
120
|
166
|
return button;
|
121
|
167
|
}
|
122
|
168
|
|
|
@@ -127,6 +173,7 @@ public class CalcSkin extends Application {
|
127
|
173
|
case "-": triggerOp.set(Op.SUBTRACT); break;
|
128
|
174
|
case "*": triggerOp.set(Op.MULTIPLY); break;
|
129
|
175
|
case "/": triggerOp.set(Op.DIVIDE); break;
|
|
176
|
+ case "x^y" : triggerOp.set(Op.EXPO); break;
|
130
|
177
|
}
|
131
|
178
|
return triggerOp;
|
132
|
179
|
}
|
|
@@ -136,7 +183,9 @@ public class CalcSkin extends Application {
|
136
|
183
|
button.setOnAction(new EventHandler<ActionEvent>() {
|
137
|
184
|
@Override
|
138
|
185
|
public void handle(ActionEvent actionEvent) {
|
|
186
|
+ System.out.println("Fire line: 169");
|
139
|
187
|
curOp = triggerOp.get();
|
|
188
|
+ System.out.println(curOp);
|
140
|
189
|
}
|
141
|
190
|
});
|
142
|
191
|
}
|
|
@@ -180,15 +229,170 @@ public class CalcSkin extends Application {
|
180
|
229
|
button.setOnAction(new EventHandler<ActionEvent>() {
|
181
|
230
|
@Override
|
182
|
231
|
public void handle(ActionEvent actionEvent) {
|
|
232
|
+ System.out.println("Fire line 221");
|
183
|
233
|
switch (stackOp) {
|
184
|
234
|
case ADD: currentValue.set(calcEngine.add(previousValue.get(), currentValue.get())); break;
|
185
|
235
|
case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
|
186
|
236
|
case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
|
187
|
237
|
case DIVIDE: currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
|
|
238
|
+ case EXPO: currentValue.set(calcEngine.expo(previousValue.get(), currentValue.get())); break;
|
188
|
239
|
}
|
189
|
240
|
}
|
190
|
241
|
});
|
191
|
242
|
}
|
192
|
|
-}
|
193
|
243
|
|
|
244
|
+ //CREATE THE SQUARED FUNCTION FOR THE X^2 BUTTON
|
|
245
|
+ private void makeSquareBtn(Button button) {
|
|
246
|
+ button.setStyle("-fx-base: lightgray;");
|
|
247
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
248
|
+ @Override
|
|
249
|
+ public void handle(ActionEvent actionEvent) {
|
|
250
|
+ currentValue.set(calcEngine.square(currentValue.get()));
|
|
251
|
+ stackOp = Op.SQUARE;
|
|
252
|
+ }
|
|
253
|
+ });
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ //CREATE THE SQUARED ROOT FUNCTION FOR THE RT BUTTON
|
|
257
|
+ private void makeSquareRtBtn(Button button) {
|
|
258
|
+ button.setStyle("-fx-base: lightgray;");
|
|
259
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
260
|
+ @Override
|
|
261
|
+ public void handle(ActionEvent actionEvent) {
|
|
262
|
+ currentValue.set(calcEngine.root(currentValue.get()));
|
|
263
|
+ stackOp = Op.ROOT;
|
|
264
|
+ }
|
|
265
|
+ });
|
|
266
|
+ }
|
|
267
|
+
|
|
268
|
+ //CREATE THE INVERSE FUNCTION FOR THE INV BUTTON
|
|
269
|
+ private void makeInverseBtn(Button button) {
|
|
270
|
+ button.setStyle("-fx-base: lightgray;");
|
|
271
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
272
|
+ @Override
|
|
273
|
+ public void handle(ActionEvent actionEvent) {
|
|
274
|
+ currentValue.set(calcEngine.inverse(currentValue.get()));
|
|
275
|
+ stackOp = Op.INVERSE;
|
|
276
|
+ }
|
|
277
|
+ });
|
|
278
|
+ }
|
194
|
279
|
|
|
280
|
+ //CREATE THE COS FUNCTION FOR THE COS BUTTON
|
|
281
|
+ private void makeCOSBtn(Button button) {
|
|
282
|
+ button.setStyle("-fx-base: lightgray;");
|
|
283
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
284
|
+ @Override
|
|
285
|
+ public void handle(ActionEvent actionEvent) {
|
|
286
|
+ currentValue.set(calcEngine.cos(currentValue.get()));
|
|
287
|
+ stackOp = Op.COS;
|
|
288
|
+ }
|
|
289
|
+ });
|
|
290
|
+ }
|
|
291
|
+ //CREATE THE SIN FUNCTION FOR THE SIN BUTTON
|
|
292
|
+ private void makeSINBtn(Button button) {
|
|
293
|
+ button.setStyle("-fx-base: lightgray;");
|
|
294
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
295
|
+ @Override
|
|
296
|
+ public void handle(ActionEvent actionEvent) {
|
|
297
|
+ currentValue.set(calcEngine.sin(currentValue.get()));
|
|
298
|
+ stackOp = Op.SIN;
|
|
299
|
+ }
|
|
300
|
+ });
|
|
301
|
+ }
|
|
302
|
+ //CREATE THE TAN FUNCTION FOR THE TAN BUTTON
|
|
303
|
+ private void makeTANBtn(Button button) {
|
|
304
|
+ button.setStyle("-fx-base: lightgray;");
|
|
305
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
306
|
+ @Override
|
|
307
|
+ public void handle(ActionEvent actionEvent) {
|
|
308
|
+ currentValue.set(calcEngine.tan(currentValue.get()));
|
|
309
|
+ stackOp = Op.TAN;
|
|
310
|
+ }
|
|
311
|
+ });
|
|
312
|
+ }
|
|
313
|
+ //CREATE THE ACOS FUNCTION FOR THE ACOS BUTTON
|
|
314
|
+ private void makeACOSBtn(Button button) {
|
|
315
|
+ button.setStyle("-fx-base: lightgray;");
|
|
316
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
317
|
+ @Override
|
|
318
|
+ public void handle(ActionEvent actionEvent) {
|
|
319
|
+ currentValue.set(calcEngine.acos(currentValue.get()));
|
|
320
|
+ stackOp = Op.ACOS;
|
|
321
|
+ }
|
|
322
|
+ });
|
|
323
|
+ }
|
|
324
|
+ //CREATE THE ASIN FUNCTION FOR THE ASIN BUTTON
|
|
325
|
+ private void makeASINBtn(Button button) {
|
|
326
|
+ button.setStyle("-fx-base: lightgray;");
|
|
327
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
328
|
+ @Override
|
|
329
|
+ public void handle(ActionEvent actionEvent) {
|
|
330
|
+ currentValue.set(calcEngine.asin(currentValue.get()));
|
|
331
|
+ stackOp = Op.ASIN;
|
|
332
|
+ }
|
|
333
|
+ });
|
|
334
|
+ }
|
|
335
|
+ //CREATE THE ATAN FUNCTION FOR THE ATAN BUTTON
|
|
336
|
+ private void makeATANBtn(Button button) {
|
|
337
|
+ button.setStyle("-fx-base: lightgray;");
|
|
338
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
339
|
+ @Override
|
|
340
|
+ public void handle(ActionEvent actionEvent) {
|
|
341
|
+ currentValue.set(calcEngine.atan(currentValue.get()));
|
|
342
|
+ stackOp = Op.ATAN;
|
|
343
|
+ }
|
|
344
|
+ });
|
|
345
|
+ }
|
|
346
|
+ //CREATE THE COS FUNCTION FOR THE COS BUTTON
|
|
347
|
+ private void makeLOGBtn(Button button) {
|
|
348
|
+ button.setStyle("-fx-base: lightgray;");
|
|
349
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
350
|
+ @Override
|
|
351
|
+ public void handle(ActionEvent actionEvent) {
|
|
352
|
+ currentValue.set(calcEngine.log(currentValue.get()));
|
|
353
|
+ stackOp = Op.LOG;
|
|
354
|
+ }
|
|
355
|
+ });
|
|
356
|
+
|
|
357
|
+ }
|
|
358
|
+ //CREATE THE STORE FUNCTION FOR THE STORE BUTTON
|
|
359
|
+ private void makeStoreBtn(Button button) {
|
|
360
|
+ button.setStyle("-fx-base: lightgray;");
|
|
361
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
362
|
+ @Override
|
|
363
|
+ public void handle(ActionEvent actionEvent) {
|
|
364
|
+ memoryValue.set(currentValue.get());
|
|
365
|
+ stackOp = Op.STORE;
|
|
366
|
+ }
|
|
367
|
+ });
|
|
368
|
+ }
|
|
369
|
+ //CREATE THE USE FUNCTION FOR THE USE BUTTON
|
|
370
|
+ private void makeUseBtn(Button button) {
|
|
371
|
+ button.setStyle("-fx-base: lightgray;");
|
|
372
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
373
|
+ @Override
|
|
374
|
+ public void handle(ActionEvent actionEvent) {
|
|
375
|
+ if(curOp == Op.NOOP){
|
|
376
|
+ currentValue.set(currentValue.get() * 10 + memoryValue.get());
|
|
377
|
+ } else {
|
|
378
|
+ previousValue.set(currentValue.get());
|
|
379
|
+ currentValue.set(memoryValue.get());
|
|
380
|
+ stackOp = Op.USE;
|
|
381
|
+ stackOp = curOp;
|
|
382
|
+ curOp = Op.NOOP;
|
|
383
|
+ }
|
|
384
|
+ }
|
|
385
|
+ });
|
|
386
|
+ }
|
|
387
|
+ //CREATE THE COS FUNCTION FOR THE COS BUTTON
|
|
388
|
+ private void makeResetBtn(Button button) {
|
|
389
|
+ button.setStyle("-fx-base: lightgray;");
|
|
390
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
391
|
+ @Override
|
|
392
|
+ public void handle(ActionEvent actionEvent) {
|
|
393
|
+ memoryValue.set(0);
|
|
394
|
+ stackOp = Op.RESET;
|
|
395
|
+ }
|
|
396
|
+ });
|
|
397
|
+ }
|
|
398
|
+}
|