Elliott Stansbury преди 6 години
родител
ревизия
e6e3a3981f

BIN
.DS_Store Целия файл


BIN
src/.DS_Store Целия файл


BIN
src/main/.DS_Store Целия файл


BIN
src/main/java/.DS_Store Целия файл


BIN
src/main/java/rocks/.DS_Store Целия файл


BIN
src/main/java/rocks/zipcode/.DS_Store Целия файл


+ 31
- 4
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Целия файл

6
     }
6
     }
7
 
7
 
8
     public double add(double v, double v1) {
8
     public double add(double v, double v1) {
9
-        return Double.NaN;
9
+
10
+        return v + v1;
10
     }
11
     }
11
 
12
 
12
     public double subtract(double v, double v1) {
13
     public double subtract(double v, double v1) {
13
-        return Double.NaN;
14
+
15
+        return v - v1;
14
     }
16
     }
15
 
17
 
16
     public double multiply(double v, double v1) {
18
     public double multiply(double v, double v1) {
17
-        return Double.NaN;
19
+
20
+        return v * v1;
18
     }
21
     }
19
 
22
 
20
     public double divide(double v, double v1) {
23
     public double divide(double v, double v1) {
21
-        return Double.NaN;
24
+
25
+        return v / v1;
26
+    }
27
+
28
+    public double square(double v){
29
+
30
+
31
+
32
+        return v * v;
33
+    }
34
+
35
+    public double squareRoot(double v){
36
+
37
+        return Math.sqrt(v);
38
+    }
39
+
40
+    public double invertNumber(double v){
41
+
42
+        return 1/v;
43
+    }
44
+
45
+    public double exponent(double v, double v1){
46
+
47
+        return Math.pow(v,v1);
48
+
22
     }
49
     }
23
 }
50
 }

+ 34
- 6
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Целия файл

24
         launch(args);
24
         launch(args);
25
     }
25
     }
26
     private static final String[][] template = {
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", "*", "1/x" },
29
+            { "1", "2", "3", "-", "SqRt" },
30
+            { "0", "c", "=", "+", "x^y" }
31
     };
31
     };
32
 
32
 
33
     private final Map<String, Button> accelerators = new HashMap<>();
33
     private final Map<String, Button> accelerators = new HashMap<>();
36
     private DoubleProperty currentValue = new SimpleDoubleProperty();
36
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37
     private CalcEngine calcEngine = new CalcEngine();
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, SQUARE, SQUAREROOT, INVERT, EXPONENT}
40
 
40
 
41
     private Op curOp   = Op.NOOP;
41
     private Op curOp   = Op.NOOP;
42
     private Op stackOp = Op.NOOP;
42
     private Op stackOp = Op.NOOP;
84
         screen.setStyle("-fx-background-color: aquamarine;");
84
         screen.setStyle("-fx-background-color: aquamarine;");
85
         screen.setAlignment(Pos.CENTER_RIGHT);
85
         screen.setAlignment(Pos.CENTER_RIGHT);
86
         screen.setEditable(false);
86
         screen.setEditable(false);
87
-        screen.textProperty().bind(Bindings.format("%.0f", currentValue));
87
+        screen.textProperty().bind(Bindings.format("%.3f", currentValue));
88
         return screen;
88
         return screen;
89
     }
89
     }
90
 
90
 
127
             case "-": triggerOp.set(Op.SUBTRACT); break;
127
             case "-": triggerOp.set(Op.SUBTRACT); break;
128
             case "*": triggerOp.set(Op.MULTIPLY); break;
128
             case "*": triggerOp.set(Op.MULTIPLY); break;
129
             case "/": triggerOp.set(Op.DIVIDE);   break;
129
             case "/": triggerOp.set(Op.DIVIDE);   break;
130
+            case "x^2": triggerOp.set(Op.SQUARE); break;
131
+            case "SqRt": triggerOp.set(Op.SQUAREROOT); break;
132
+            case "1/x": triggerOp.set(Op.INVERT); break;
133
+            case "x^y": triggerOp.set(Op.EXPONENT); break;
134
+
130
         }
135
         }
131
         return triggerOp;
136
         return triggerOp;
132
     }
137
     }
133
 
138
 
139
+    //private void squareRoot(Button button)
140
+
134
     private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
141
     private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
135
         button.setStyle("-fx-base: lightgray;");
142
         button.setStyle("-fx-base: lightgray;");
136
         button.setOnAction(new EventHandler<ActionEvent>() {
143
         button.setOnAction(new EventHandler<ActionEvent>() {
185
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
192
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
193
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
194
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
195
+                    case SQUARE:   currentValue.set(calcEngine.square(currentValue.get())); break;
196
+                    case SQUAREROOT:   currentValue.set(calcEngine.squareRoot(currentValue.get())); break;
197
+                    case INVERT:   currentValue.set(calcEngine.invertNumber(currentValue.get())); break;
198
+                    case EXPONENT:   currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
199
+
188
                 }
200
                 }
189
             }
201
             }
190
         });
202
         });
191
     }
203
     }
204
+
205
+//    private void makeSquareButton(Button button) {
206
+//        button.setStyle("-fx-base: mistyrose;");
207
+//        button.setOnAction(new EventHandler<ActionEvent>() {
208
+//            @Override
209
+//            public void handle(ActionEvent actionEvent) {
210
+//                switch (stackOp) {
211
+//
212
+//                    case SQUARE:
213
+//                        currentValue.set(calcEngine.square(currentValue.get()));
214
+//                }
215
+//            }
216
+//        });
217
+//    }
218
+
219
+
192
 }
220
 }
193
 
221
 
194
 
222
 

BIN
src/test/.DS_Store Целия файл


BIN
src/test/java/.DS_Store Целия файл


BIN
src/test/java/rocks/.DS_Store Целия файл


BIN
src/test/java/rocks/zipcode/.DS_Store Целия файл


+ 8
- 0
src/test/java/rocks/zipcode/calcskin/CalcEngineTest.java Целия файл

38
     public void divide() {
38
     public void divide() {
39
         Assert.assertTrue("", (testCalc.divide(10.0, 2.0) == 5.0));
39
         Assert.assertTrue("", (testCalc.divide(10.0, 2.0) == 5.0));
40
     }
40
     }
41
+
42
+    @Test
43
+    public void square() {
44
+
45
+
46
+        Assert.assertEquals(25.0, testCalc.square(5.0));
47
+
48
+    }
41
 }
49
 }