|
@@ -24,10 +24,10 @@ public class CalcSkin extends Application {
|
24
|
24
|
launch(args);
|
25
|
25
|
}
|
26
|
26
|
private static final String[][] template = {
|
27
|
|
- { "7", "8", "9", "/" },
|
28
|
|
- { "4", "5", "6", "*" },
|
29
|
|
- { "1", "2", "3", "-" },
|
30
|
|
- { "0", "c", "=", "+" }
|
|
27
|
+ { "7", "8", "9", "/", "x^2", "MC", "sin" },
|
|
28
|
+ { "4", "5", "6", "*", "SQRT", "MR", "cos" },
|
|
29
|
+ { "1", "2", "3", "-", "1/x", "M+", "tan" },
|
|
30
|
+ { "0", "c", "=", "+", "x^y" }
|
31
|
31
|
};
|
32
|
32
|
|
33
|
33
|
private final Map<String, Button> accelerators = new HashMap<>();
|
|
@@ -35,8 +35,9 @@ public class CalcSkin extends Application {
|
35
|
35
|
private DoubleProperty previousValue = new SimpleDoubleProperty();
|
36
|
36
|
private DoubleProperty currentValue = new SimpleDoubleProperty();
|
37
|
37
|
private CalcEngine calcEngine = new CalcEngine();
|
|
38
|
+ private DoubleProperty memoryValue = new SimpleDoubleProperty();
|
38
|
39
|
|
39
|
|
- private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
|
|
40
|
+ private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUARED, SQUAREROOT, INVERSE, EXPONENT }
|
40
|
41
|
|
41
|
42
|
private Op curOp = Op.NOOP;
|
42
|
43
|
private Op stackOp = Op.NOOP;
|
|
@@ -82,9 +83,9 @@ public class CalcSkin extends Application {
|
82
|
83
|
private TextField createScreen() {
|
83
|
84
|
final TextField screen = new TextField();
|
84
|
85
|
screen.setStyle("-fx-background-color: aquamarine;");
|
85
|
|
- screen.setAlignment(Pos.CENTER_RIGHT);
|
|
86
|
+ screen.setAlignment(Pos.CENTER_LEFT);
|
86
|
87
|
screen.setEditable(false);
|
87
|
|
- screen.textProperty().bind(Bindings.format("%.0f", currentValue));
|
|
88
|
+ screen.textProperty().bind(Bindings.format("%.4f", currentValue));
|
88
|
89
|
return screen;
|
89
|
90
|
}
|
90
|
91
|
|
|
@@ -114,7 +115,27 @@ public class CalcSkin extends Application {
|
114
|
115
|
makeClearButton(button);
|
115
|
116
|
} else if ("=".equals(s)) {
|
116
|
117
|
makeEqualsButton(button);
|
|
118
|
+ } else if ("x^2".equals(s)) {
|
|
119
|
+ makeSquaredButton(button);
|
|
120
|
+ } else if ("SQRT".equals(s)){
|
|
121
|
+ makeSqrtButton(button);
|
|
122
|
+ } else if ("1/x".equals(s)){
|
|
123
|
+ makeInverseButton(button);
|
|
124
|
+ } else if ("M+".equals(s)){
|
|
125
|
+ makeMemoryAddButton(button);
|
|
126
|
+ } else if ("MR".equals(s)){
|
|
127
|
+ makeMemoryRecallButton(button);
|
|
128
|
+ } else if ("MC".equals(s)){
|
|
129
|
+ makeMemoryClearButton(button);
|
|
130
|
+ } else if ("sin".equals(s)){
|
|
131
|
+ makeSinButton(button);
|
|
132
|
+ } else if ("cos".equals(s)) {
|
|
133
|
+ makeCosButton(button);
|
|
134
|
+ } else if ("tan".equals(s)) {
|
|
135
|
+ makeTanButton(button);
|
117
|
136
|
}
|
|
137
|
+ //make else ifs for the single numeric functions
|
|
138
|
+ //call the new method for those functions
|
118
|
139
|
}
|
119
|
140
|
|
120
|
141
|
return button;
|
|
@@ -127,6 +148,10 @@ public class CalcSkin extends Application {
|
127
|
148
|
case "-": triggerOp.set(Op.SUBTRACT); break;
|
128
|
149
|
case "*": triggerOp.set(Op.MULTIPLY); break;
|
129
|
150
|
case "/": triggerOp.set(Op.DIVIDE); break;
|
|
151
|
+ //case "x^2": triggerOp.set(Op.SQUARED); break;
|
|
152
|
+ //case "SQRT": triggerOp.set(Op.SQUAREROOT); break;
|
|
153
|
+ //case "1/x": triggerOp.set(Op.INVERSE); break;
|
|
154
|
+ case "x^y": triggerOp.set(Op.EXPONENT); break;
|
130
|
155
|
}
|
131
|
156
|
return triggerOp;
|
132
|
157
|
}
|
|
@@ -175,6 +200,96 @@ public class CalcSkin extends Application {
|
175
|
200
|
});
|
176
|
201
|
}
|
177
|
202
|
|
|
203
|
+ private void makeSquaredButton(Button button) {
|
|
204
|
+ button.setStyle("-fx-base: darkgray");
|
|
205
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
206
|
+ @Override
|
|
207
|
+ public void handle(ActionEvent actionEvent) {
|
|
208
|
+ currentValue.set(calcEngine.squared(currentValue.get()));
|
|
209
|
+ }
|
|
210
|
+ });
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ private void makeSqrtButton(Button button) {
|
|
214
|
+ button.setStyle("-fx-base: darkgray;");
|
|
215
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
216
|
+ @Override
|
|
217
|
+ public void handle(ActionEvent actionEvent) {
|
|
218
|
+ currentValue.set(calcEngine.sqrt(currentValue.get()));
|
|
219
|
+ }
|
|
220
|
+ });
|
|
221
|
+ }
|
|
222
|
+
|
|
223
|
+ private void makeInverseButton(Button button) {
|
|
224
|
+ button.setStyle("-fx-base: darkgray;");
|
|
225
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
226
|
+ @Override
|
|
227
|
+ public void handle(ActionEvent actionEvent) {
|
|
228
|
+ currentValue.set(calcEngine.inverse(currentValue.get()));
|
|
229
|
+ }
|
|
230
|
+ });
|
|
231
|
+ }
|
|
232
|
+
|
|
233
|
+ private void makeMemoryAddButton(Button button) {
|
|
234
|
+ button.setStyle("-fx-base: gray");
|
|
235
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
236
|
+ @Override
|
|
237
|
+ public void handle(ActionEvent actionEvent) {
|
|
238
|
+ memoryValue.set(currentValue.get());
|
|
239
|
+ }
|
|
240
|
+ });
|
|
241
|
+ }
|
|
242
|
+
|
|
243
|
+ private void makeMemoryRecallButton(Button button) {
|
|
244
|
+ button.setStyle("-fx-base: gray");
|
|
245
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
246
|
+ @Override
|
|
247
|
+ public void handle(ActionEvent actionEvent) {
|
|
248
|
+ currentValue.set(memoryValue.get());
|
|
249
|
+ }
|
|
250
|
+ });
|
|
251
|
+ }
|
|
252
|
+
|
|
253
|
+ private void makeMemoryClearButton(Button button) {
|
|
254
|
+ button.setStyle("-fx-base: gray");
|
|
255
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
256
|
+ @Override
|
|
257
|
+ public void handle(ActionEvent actionEvent) {
|
|
258
|
+ memoryValue.set(0);
|
|
259
|
+ }
|
|
260
|
+ });
|
|
261
|
+ }
|
|
262
|
+
|
|
263
|
+ private void makeSinButton(Button button) {
|
|
264
|
+ button.setStyle("-fx-base: black");
|
|
265
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
266
|
+ @Override
|
|
267
|
+ public void handle(ActionEvent actionEvent) {
|
|
268
|
+ currentValue.set(Math.sin(currentValue.get()));
|
|
269
|
+ }
|
|
270
|
+ });
|
|
271
|
+ }
|
|
272
|
+
|
|
273
|
+ private void makeCosButton(Button button) {
|
|
274
|
+ button.setStyle("-fx-base: black");
|
|
275
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
276
|
+ @Override
|
|
277
|
+ public void handle(ActionEvent actionEvent) {
|
|
278
|
+ currentValue.set(Math.cos(currentValue.get()));
|
|
279
|
+ }
|
|
280
|
+ });
|
|
281
|
+ }
|
|
282
|
+
|
|
283
|
+ private void makeTanButton(Button button) {
|
|
284
|
+ button.setStyle("-fx-base: black");
|
|
285
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
286
|
+ @Override
|
|
287
|
+ public void handle(ActionEvent actionEvent) {
|
|
288
|
+ currentValue.set(Math.tan(currentValue.get()));
|
|
289
|
+ }
|
|
290
|
+ });
|
|
291
|
+ }
|
|
292
|
+
|
178
|
293
|
private void makeEqualsButton(Button button) {
|
179
|
294
|
button.setStyle("-fx-base: ghostwhite;");
|
180
|
295
|
button.setOnAction(new EventHandler<ActionEvent>() {
|
|
@@ -185,10 +300,19 @@ public class CalcSkin extends Application {
|
185
|
300
|
case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
|
186
|
301
|
case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
|
187
|
302
|
case DIVIDE: currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
|
|
303
|
+ //case SQUARED: currentValue.set(calcEngine.squared(currentValue.get())); break;
|
|
304
|
+ //case SQUAREROOT: currentValue.set(calcEngine.sqrt(currentValue.get())); break;
|
|
305
|
+ //case INVERSE: currentValue.set(calcEngine.inverse(currentValue.get())); break;
|
|
306
|
+ case EXPONENT: currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
188
|
310
|
}
|
189
|
311
|
}
|
190
|
312
|
});
|
191
|
313
|
}
|
|
314
|
+
|
|
315
|
+
|
192
|
316
|
}
|
193
|
317
|
|
194
|
318
|
|