Brandon Defrancis 6 years ago
parent
commit
91b133d26e

+ 41
- 4
src/main/java/rocks/zipcode/calcskin/CalcEngine.java View File

@@ -6,18 +6,55 @@ public class CalcEngine {
6 6
     }
7 7
 
8 8
     public double add(double v, double v1) {
9
-        return Double.NaN;
9
+        return v + v1;
10 10
     }
11 11
 
12 12
     public double subtract(double v, double v1) {
13
-        return Double.NaN;
13
+        return v - v1;
14 14
     }
15 15
 
16 16
     public double multiply(double v, double v1) {
17
-        return Double.NaN;
17
+        return v * v1;
18 18
     }
19 19
 
20 20
     public double divide(double v, double v1) {
21
-        return Double.NaN;
21
+        return v / v1;
22
+    }
23
+
24
+    public double exponent(double v, double v1 ) {
25
+        return Math.pow(v, v1);
26
+    }
27
+
28
+
29
+    public double squareRoot(double v) {
30
+        double answer = Math.sqrt(v);
31
+        return Math.toRadians(answer);
32
+    }
33
+
34
+
35
+    public double squared(double v) {
36
+        return Math.pow(v, 2);
37
+    }
38
+
39
+    public double inverse(double v) {
40
+        return (1 / v);
41
+    }
42
+
43
+    public double sin(double v) {
44
+        double answer = Math.toDegrees(Math.sin(v));
45
+
46
+        return Math.toRadians(answer);
47
+    }
48
+
49
+    public double cos(double v) {
50
+
51
+        double answer = Math.toDegrees(Math.cos(v));
52
+        return Math.toRadians(answer);
53
+    }
54
+
55
+    public double tan(double v) {
56
+
57
+        double answer = Math.toDegrees(Math.sin(v));
58
+        return Math.toRadians(answer);
22 59
     }
23 60
 }

+ 98
- 9
src/main/java/rocks/zipcode/calcskin/CalcSkin.java View File

@@ -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", "/", "^", "sin"},
28
+            { "4", "5", "6", "*", "√", "cos" },
29
+            { "1", "2", "3", "-", "x²", "tan" },
30
+            { "0", "c", "=", "+", "1/x" }
31 31
     };
32 32
 
33 33
     private final Map<String, Button> accelerators = new HashMap<>();
@@ -36,7 +36,7 @@ public class CalcSkin extends Application {
36 36
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 37
     private CalcEngine calcEngine = new CalcEngine();
38 38
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
39
+    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, EXPONENT, SQUAREROOT, SQUARED, INVERSE, SINE, COSINE, TANGENT }
40 40
 
41 41
     private Op curOp   = Op.NOOP;
42 42
     private Op stackOp = Op.NOOP;
@@ -60,7 +60,7 @@ public class CalcSkin extends Application {
60 60
     private VBox createLayout(TextField screen, TilePane buttons) {
61 61
         final VBox layout = new VBox(20);
62 62
         layout.setAlignment(Pos.CENTER);
63
-        layout.setStyle("-fx-background-color: silver; -fx-padding: 20; -fx-font-size: 20;");
63
+        layout.setStyle("-fx-background-color: #4e4e4e; -fx-padding: 20; -fx-font-size: 20;");
64 64
         layout.getChildren().setAll(screen, buttons);
65 65
         handleAccelerators(layout);
66 66
         screen.prefWidthProperty().bind(buttons.widthProperty());
@@ -81,10 +81,10 @@ public class CalcSkin extends Application {
81 81
 
82 82
     private TextField createScreen() {
83 83
         final TextField screen = new TextField();
84
-        screen.setStyle("-fx-background-color: aquamarine;");
84
+        screen.setStyle("-fx-background-color: #faffd3;");
85 85
         screen.setAlignment(Pos.CENTER_RIGHT);
86 86
         screen.setEditable(false);
87
-        screen.textProperty().bind(Bindings.format("%.0f", currentValue));
87
+        screen.textProperty().bind(Bindings.format("%.2f", currentValue));
88 88
         return screen;
89 89
     }
90 90
 
@@ -114,7 +114,19 @@ public class CalcSkin extends Application {
114 114
                 makeClearButton(button);
115 115
             } else if ("=".equals(s)) {
116 116
                 makeEqualsButton(button);
117
-            }
117
+            } else if ("√".equals(s)) {
118
+               squareRootButton(button);
119
+            } else if ("x²".equals(s)) {
120
+                squaredButton(button);
121
+            } else if ("1/x".equals(s)) {
122
+                inverseButton(button);
123
+            } else if ("sin".equals(s)) {
124
+                sinButton(button);
125
+            } else if ("cos".equals(s)) {
126
+                cosButton(button);
127
+            } else if ("tan".equals(s)) {
128
+                tanButton(button);
129
+        }
118 130
         }
119 131
 
120 132
         return button;
@@ -127,6 +139,13 @@ public class CalcSkin extends Application {
127 139
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 140
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 141
             case "/": triggerOp.set(Op.DIVIDE);   break;
142
+            case "^": triggerOp.set(Op.EXPONENT); break;
143
+//            case "√": triggerOp.set(Op.SQUAREROOT); break;
144
+//            case "x²": triggerOp.set(Op.SQUARED);   break;
145
+//            case "1/x": triggerOp.set(Op.INVERSE);  break;
146
+//            case "sin": triggerOp.set(Op.SINE); break;
147
+//            case "cos": triggerOp.set(Op.COSINE);   break;
148
+//            case "tan": triggerOp.set(Op.TANGENT);  break;
130 149
         }
131 150
         return triggerOp;
132 151
     }
@@ -185,10 +204,80 @@ public class CalcSkin extends Application {
185 204
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 205
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 206
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
207
+                    case EXPONENT:   currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
188 208
                 }
189 209
             }
190 210
         });
191 211
     }
212
+
213
+        private void squaredButton(Button button) {
214
+            button.setStyle("-fx-base: ghostwhite;");
215
+            button.setOnAction(new EventHandler<ActionEvent>() {
216
+                @Override
217
+                public void handle(ActionEvent actionEvent) {
218
+                    SQUARED:
219
+                    currentValue.set(calcEngine.squared(currentValue.get()));
220
+
221
+                }
222
+            });
223
+        }
224
+
225
+        private void squareRootButton(Button button) {
226
+            button.setStyle("-fx-base: ghostwhite;");
227
+            button.setOnAction(new EventHandler<ActionEvent>() {
228
+                @Override
229
+                public void handle(ActionEvent actionEvent) {
230
+                    SQUAREROOT:
231
+                    currentValue.set(calcEngine.squareRoot(currentValue.get()));
232
+
233
+                }
234
+            });
235
+        }
236
+
237
+            private void inverseButton(Button button) {
238
+                button.setStyle("-fx-base: ghostwhite;");
239
+                button.setOnAction(new EventHandler<ActionEvent>() {
240
+                    @Override
241
+                    public void handle(ActionEvent actionEvent) {
242
+                        INVERSE: currentValue.set(calcEngine.inverse(currentValue.get()));
243
+
244
+                    }
245
+                });
246
+            }
247
+
248
+            private void sinButton(Button button) {
249
+        button.setStyle("-fx-base: ghostwhite;");
250
+        button.setOnAction(new EventHandler<ActionEvent>() {
251
+            @Override
252
+            public void handle(ActionEvent actionEvent) {
253
+                SINE: currentValue.set(calcEngine.sin(currentValue.get()));
254
+
255
+            }
256
+        });
257
+    }
258
+
259
+        private void cosButton(Button button) {
260
+            button.setStyle("-fx-base: ghostwhite;");
261
+            button.setOnAction(new EventHandler<ActionEvent>() {
262
+                @Override
263
+                public void handle(ActionEvent actionEvent) {
264
+                    COSINE: currentValue.set(calcEngine.cos(currentValue.get()));
265
+
266
+                }
267
+            });
268
+        }
269
+
270
+        private void tanButton(Button button) {
271
+            button.setStyle("-fx-base: ghostwhite;");
272
+            button.setOnAction(new EventHandler<ActionEvent>() {
273
+                @Override
274
+                public void handle(ActionEvent actionEvent) {
275
+                    TANGENT: currentValue.set(calcEngine.tan(currentValue.get()));
276
+
277
+                }
278
+            });
279
+        }
280
+
192 281
 }
193 282
 
194 283
 

+ 19
- 0
src/test/java/rocks/zipcode/calcskin/CalcEngineTest.java View File

@@ -38,4 +38,23 @@ public class CalcEngineTest {
38 38
     public void divide() {
39 39
         Assert.assertTrue("", (testCalc.divide(10.0, 2.0) == 5.0));
40 40
     }
41
+
42
+    @Test
43
+    public void exponent() { Assert.assertTrue("", (testCalc.exponent(3.0, 3.0) == 27.0)); }
44
+
45
+
46
+    @Test
47
+    public void squared() { Assert.assertTrue("", (testCalc.squared(10.0) == 100.0));
48
+    }
49
+
50
+    @Test
51
+    public void squareRoot() { Assert.assertTrue("", (testCalc.squareRoot(4.0) == 2.0));
52
+    }
53
+
54
+    @Test
55
+    public void inverse() { Assert.assertTrue("", (testCalc.inverse(2.0) == .5));
56
+    }
57
+
58
+    @Test
59
+    public void sin() { Assert.assertTrue("", (testCalc.sin(2.0) == .03)); }
41 60
 }