Connor Dunnigan 6 yıl önce
ebeveyn
işleme
6c0478cf65

+ 12
- 8
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Dosyayı Görüntüle

@@ -6,18 +6,22 @@ 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
-    public double subtract(double v, double v1) {
13
-        return Double.NaN;
14
-    }
12
+    public double subtract(double v, double v1) { return v - v1; }
15 13
 
16
-    public double multiply(double v, double v1) {
17
-        return Double.NaN;
18
-    }
14
+    public double multiply(double v, double v1) { return v * v1; }
19 15
 
20 16
     public double divide(double v, double v1) {
21
-        return Double.NaN;
17
+        return v / v1;
22 18
     }
19
+
20
+    public double square(double v){ return v*v;}
21
+
22
+    public double sqrt(double v){ return Math.sqrt(v);}
23
+
24
+    public double inverse(double v){
25
+        double inv = (double)1/v;
26
+        return inv; }
23 27
 }

+ 46
- 7
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Dosyayı Görüntüle

@@ -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"},
28
+            { "4", "5", "6", "*", "sqrt"},
29
+            { "1", "2", "3", "-", ""},
30
+            { "0", "c", "=", "+"}
31 31
     };
32 32
 
33 33
     private final Map<String, Button> accelerators = new HashMap<>();
@@ -84,14 +84,14 @@ public class CalcSkin extends Application {
84 84
         screen.setStyle("-fx-background-color: aquamarine;");
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("%.3f", currentValue));
88 88
         return screen;
89 89
     }
90 90
 
91 91
     private TilePane createButtons() {
92 92
         TilePane buttons = new TilePane();
93
-        buttons.setVgap(7);
94
-        buttons.setHgap(7);
93
+        buttons.setVgap(10);
94
+        buttons.setHgap(10);
95 95
         buttons.setPrefColumns(template[0].length);
96 96
         for (String[] r: template) {
97 97
             for (String s: r) {
@@ -114,6 +114,12 @@ public class CalcSkin extends Application {
114 114
                 makeClearButton(button);
115 115
             } else if ("=".equals(s)) {
116 116
                 makeEqualsButton(button);
117
+            } else if("x^2".equals(s)){
118
+                makeSquareButton(button);
119
+            } else if("sqrt".equals(s)){
120
+                makeSqrtButton(button);
121
+            } else if("1/x".equals(s)){
122
+                makeInverseButton(button);
117 123
             }
118 124
         }
119 125
 
@@ -175,6 +181,39 @@ public class CalcSkin extends Application {
175 181
         });
176 182
     }
177 183
 
184
+    private void makeSquareButton(Button button){
185
+        button.setStyle("-fx-base: ghostwhite;");
186
+        button.setOnAction(new EventHandler<ActionEvent>() {
187
+            @Override
188
+            public void handle(ActionEvent actionEvent) {
189
+                currentValue.set(calcEngine.square(currentValue.get()));
190
+            }
191
+        });
192
+
193
+    }
194
+
195
+    private void makeInverseButton(Button button){
196
+        button.setStyle("-fx-base: ghostwhite;");
197
+        button.setOnAction(new EventHandler<ActionEvent>() {
198
+            @Override
199
+            public void handle(ActionEvent actionEvent) {
200
+                currentValue.set(calcEngine.inverse(currentValue.get()));
201
+            }
202
+        });
203
+
204
+    }
205
+
206
+    private void makeSqrtButton(Button button){
207
+        button.setStyle("-fx-base: ghostwhite;");
208
+        button.setOnAction(new EventHandler<ActionEvent>() {
209
+            @Override
210
+            public void handle(ActionEvent actionEvent) {
211
+                currentValue.set(calcEngine.sqrt(currentValue.get()));
212
+            }
213
+        });
214
+
215
+    }
216
+
178 217
     private void makeEqualsButton(Button button) {
179 218
         button.setStyle("-fx-base: ghostwhite;");
180 219
         button.setOnAction(new EventHandler<ActionEvent>() {