Soujanya Buragapu 5 年之前
父節點
當前提交
097cd14a3f

二進制
.DS_Store 查看文件


二進制
src/.DS_Store 查看文件


二進制
src/main/.DS_Store 查看文件


二進制
src/main/java/.DS_Store 查看文件


二進制
src/main/java/rocks/.DS_Store 查看文件


二進制
src/main/java/rocks/zipcode/.DS_Store 查看文件


+ 36
- 13
src/main/java/rocks/zipcode/calcskin/CalcEngine.java 查看文件

3
 public class CalcEngine {
3
 public class CalcEngine {
4
 
4
 
5
     CalcEngine() {
5
     CalcEngine() {
6
-    }
6
+        }
7
 
7
 
8
-    public double add(double v, double v1) {
9
-        return Double.NaN;
10
-    }
8
+        public static double addition(double x, double y) {
9
+            return (x + y);
10
+        }
11
 
11
 
12
-    public double subtract(double v, double v1) {
13
-        return Double.NaN;
14
-    }
12
+        public static double subtraction(double x, double y) {
13
+            return (x - y);
14
+        }
15
 
15
 
16
-    public double multiply(double v, double v1) {
17
-        return Double.NaN;
18
-    }
16
+        public static double multiplication(double x, double y) {
17
+
18
+            return (x * y);
19
+        }
20
+
21
+        public static double division(double x, double y) {
22
+            return (x / y);
23
+        }
24
+
25
+        public static double square(double x) {
26
+            return Math.pow(x, 2);
27
+        }
28
+
29
+        public static double squareroot(double x) {
30
+            return Math.sqrt(x);
31
+        }
32
+
33
+        public static double exponent(double x, double y) {
34
+
35
+            return Math.pow(x, y);
36
+        }
37
+
38
+        public static double inverse(double x) {
39
+            return (1 / x);
40
+        }
41
+
42
+        public static double invert(double x) {
43
+            return (-x);
44
+        }
19
 
45
 
20
-    public double divide(double v, double v1) {
21
-        return Double.NaN;
22
-    }
23
 }
46
 }

+ 37
- 33
src/main/java/rocks/zipcode/calcskin/CalcSkin.java 查看文件

1
 package rocks.zipcode.calcskin;
1
 package rocks.zipcode.calcskin;
2
-
3
 import javafx.application.Application;
2
 import javafx.application.Application;
4
 import javafx.beans.binding.Bindings;
3
 import javafx.beans.binding.Bindings;
5
 import javafx.beans.property.*;
4
 import javafx.beans.property.*;
12
 import javafx.scene.layout.*;
11
 import javafx.scene.layout.*;
13
 import javafx.stage.Stage;
12
 import javafx.stage.Stage;
14
 import javafx.stage.StageStyle;
13
 import javafx.stage.StageStyle;
15
-import rocks.zipcode.calcskin.CalcEngine;
16
-
17
 import java.util.HashMap;
14
 import java.util.HashMap;
18
 import java.util.Map;
15
 import java.util.Map;
19
 
16
 
20
-// a simple JavaFX calculator.
21
 public class CalcSkin extends Application {
17
 public class CalcSkin extends Application {
22
 
18
 
23
     public static void main(String[] args){
19
     public static void main(String[] args){
27
             { "7", "8", "9", "/" },
23
             { "7", "8", "9", "/" },
28
             { "4", "5", "6", "*" },
24
             { "4", "5", "6", "*" },
29
             { "1", "2", "3", "-" },
25
             { "1", "2", "3", "-" },
30
-            { "0", "c", "=", "+" }
26
+            { "0", "c", "=", "+" },
27
+            { "√", "^", "~", "²"}
31
     };
28
     };
32
 
29
 
33
     private final Map<String, Button> accelerators = new HashMap<>();
30
     private final Map<String, Button> accelerators = new HashMap<>();
34
-
35
     private DoubleProperty previousValue = new SimpleDoubleProperty();
31
     private DoubleProperty previousValue = new SimpleDoubleProperty();
36
     private DoubleProperty currentValue = new SimpleDoubleProperty();
32
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37
     private CalcEngine calcEngine = new CalcEngine();
33
     private CalcEngine calcEngine = new CalcEngine();
38
 
34
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
35
+    private enum Cal { operation, add, subtract, multiply, divide, squareroot, square, exponent, invert}
40
 
36
 
41
-    private Op curOp   = Op.NOOP;
42
-    private Op stackOp = Op.NOOP;
37
+    private Cal first   = Cal.operation;
38
+    private Cal second = Cal.operation;
43
 
39
 
44
     public static void launchCalc(String... args) {
40
     public static void launchCalc(String... args) {
45
         launch(args);
41
         launch(args);
107
         if (s.matches("[0-9]")) {
103
         if (s.matches("[0-9]")) {
108
             makeNumericButton(s, button);
104
             makeNumericButton(s, button);
109
         } else {
105
         } else {
110
-            final ObjectProperty<Op> triggerOp = determineOperand(s);
111
-            if (triggerOp.get() != Op.NOOP) {
112
-                makeOperandButton(button, triggerOp);
106
+            final ObjectProperty<Cal> triggerCal = determineOperand(s);
107
+            if (triggerCal.get() != Cal.operation) {
108
+                makeOperandButton(button, triggerCal);
113
             } else if ("c".equals(s)) {
109
             } else if ("c".equals(s)) {
114
                 makeClearButton(button);
110
                 makeClearButton(button);
115
             } else if ("=".equals(s)) {
111
             } else if ("=".equals(s)) {
120
         return button;
116
         return button;
121
     }
117
     }
122
 
118
 
123
-    private ObjectProperty<Op> determineOperand(String s) {
124
-        final ObjectProperty<Op> triggerOp = new SimpleObjectProperty<>(Op.NOOP);
119
+    private ObjectProperty<Cal> determineOperand(String s) {
120
+        final ObjectProperty<Cal> triggerOp = new SimpleObjectProperty<>(Cal.operation);
125
         switch (s) {
121
         switch (s) {
126
-            case "+": triggerOp.set(Op.ADD);      break;
127
-            case "-": triggerOp.set(Op.SUBTRACT); break;
128
-            case "*": triggerOp.set(Op.MULTIPLY); break;
129
-            case "/": triggerOp.set(Op.DIVIDE);   break;
122
+            case "+": triggerOp.set(Cal.add);      break;
123
+            case "-": triggerOp.set(Cal.subtract); break;
124
+            case "*": triggerOp.set(Cal.multiply); break;
125
+            case "/": triggerOp.set(Cal.divide);   break;
126
+            case "²": triggerOp.set(Cal.square);   break;
127
+            case "√": triggerOp.set(Cal.squareroot);  break;
128
+            case "^": triggerOp.set(Cal.exponent);   break;
129
+            case "~": triggerOp.set(Cal.invert);     break;
130
         }
130
         }
131
         return triggerOp;
131
         return triggerOp;
132
     }
132
     }
133
 
133
 
134
-    private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
135
-        button.setStyle("-fx-base: lightgray;");
134
+    private void makeOperandButton(Button button, final ObjectProperty<Cal> triggerCal) {
135
+        button.setStyle("-fx-base: black;");
136
         button.setOnAction(new EventHandler<ActionEvent>() {
136
         button.setOnAction(new EventHandler<ActionEvent>() {
137
             @Override
137
             @Override
138
             public void handle(ActionEvent actionEvent) {
138
             public void handle(ActionEvent actionEvent) {
139
-                curOp = triggerOp.get();
139
+                first = triggerCal.get();
140
             }
140
             }
141
         });
141
         });
142
     }
142
     }
143
 
143
 
144
     private Button makeStandardButton(String s) {
144
     private Button makeStandardButton(String s) {
145
         Button button = new Button(s);
145
         Button button = new Button(s);
146
-        button.setStyle("-fx-base: beige;");
146
+        button.setStyle("-fx-base: pink;");
147
         accelerators.put(s, button);
147
         accelerators.put(s, button);
148
         button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
148
         button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
149
         return button;
149
         return button;
153
         button.setOnAction(new EventHandler<ActionEvent>() {
153
         button.setOnAction(new EventHandler<ActionEvent>() {
154
             @Override
154
             @Override
155
             public void handle(ActionEvent actionEvent) {
155
             public void handle(ActionEvent actionEvent) {
156
-                if (curOp == Op.NOOP) {
156
+                if (first == Cal.operation) {
157
                     currentValue.set(currentValue.get() * 10 + Integer.parseInt(s));
157
                     currentValue.set(currentValue.get() * 10 + Integer.parseInt(s));
158
                 } else {
158
                 } else {
159
                     previousValue.set(currentValue.get());
159
                     previousValue.set(currentValue.get());
160
                     currentValue.set(Integer.parseInt(s));
160
                     currentValue.set(Integer.parseInt(s));
161
-                    stackOp = curOp;
162
-                    curOp = Op.NOOP;
161
+                    second = first;
162
+                    second = Cal.operation;
163
                 }
163
                 }
164
             }
164
             }
165
         });
165
         });
166
     }
166
     }
167
 
167
 
168
     private void makeClearButton(Button button) {
168
     private void makeClearButton(Button button) {
169
-        button.setStyle("-fx-base: mistyrose;");
169
+        button.setStyle("-fx-base: grey;");
170
         button.setOnAction(new EventHandler<ActionEvent>() {
170
         button.setOnAction(new EventHandler<ActionEvent>() {
171
             @Override
171
             @Override
172
             public void handle(ActionEvent actionEvent) {
172
             public void handle(ActionEvent actionEvent) {
176
     }
176
     }
177
 
177
 
178
     private void makeEqualsButton(Button button) {
178
     private void makeEqualsButton(Button button) {
179
-        button.setStyle("-fx-base: ghostwhite;");
179
+        button.setStyle("-fx-base: green;");
180
         button.setOnAction(new EventHandler<ActionEvent>() {
180
         button.setOnAction(new EventHandler<ActionEvent>() {
181
             @Override
181
             @Override
182
             public void handle(ActionEvent actionEvent) {
182
             public void handle(ActionEvent actionEvent) {
183
-                switch (stackOp) {
184
-                    case ADD:      currentValue.set(calcEngine.add(previousValue.get(), currentValue.get())); break;
185
-                    case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186
-                    case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187
-                    case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
183
+                switch (second) {
184
+                    case add:      currentValue.set(calcEngine.addition(previousValue.get(), currentValue.get())); break;
185
+                    case subtract: currentValue.set(calcEngine.subtraction(previousValue.get(), currentValue.get())); break;
186
+                    case multiply: currentValue.set(calcEngine.multiplication(previousValue.get(), currentValue.get())); break;
187
+                    case divide:   currentValue.set(calcEngine.division(previousValue.get(), currentValue.get())); break;
188
+                    case square:   currentValue.set(calcEngine.square(previousValue.get())); break;
189
+                    case squareroot: currentValue.set(calcEngine.squareroot(previousValue.get()));   break;
190
+                    case exponent:  currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
191
+                    case invert:    currentValue.set(calcEngine.invert(previousValue.get()));   break;
192
+
188
                 }
193
                 }
189
             }
194
             }
190
         });
195
         });
191
     }
196
     }
192
 }
197
 }
193
 
198
 
194
-

+ 0
- 4
src/main/java/rocks/zipcode/calcskin/MainApplication.java 查看文件

1
 package rocks.zipcode.calcskin;
1
 package rocks.zipcode.calcskin;
2
-
3
-import rocks.zipcode.calcskin.CalcSkin;
4
-
5
-// You probably should make NO changes here. Keep it simple.
6
 public class MainApplication {
2
 public class MainApplication {
7
     public static void main(String[] args){
3
     public static void main(String[] args){
8
 
4
 

+ 66
- 10
src/test/java/rocks/zipcode/calcskin/CalcEngineTest.java 查看文件

1
 package rocks.zipcode.calcskin;
1
 package rocks.zipcode.calcskin;
2
 
2
 
3
 import org.junit.After;
3
 import org.junit.After;
4
-import org.junit.Assert;
5
 import org.junit.Before;
4
 import org.junit.Before;
6
 import org.junit.Test;
5
 import org.junit.Test;
7
-
8
 import static org.junit.Assert.*;
6
 import static org.junit.Assert.*;
9
 
7
 
10
 public class CalcEngineTest {
8
 public class CalcEngineTest {
20
     }
18
     }
21
 
19
 
22
     @Test
20
     @Test
23
-    public void add() {
24
-        Assert.assertTrue("", (testCalc.add(1.0, 1.0) == 2.0));
21
+    public void testSquare(){
22
+        // set up data
23
+        double x = 3.0;
24
+
25
+        //invoke method under test
26
+        double actualResult = CalcEngine.square(x);
27
+
28
+        //verify result
29
+        double expectResult = 9.0;
30
+        assertEquals(expectResult, actualResult, 0.001);
31
+
32
+    }
33
+
34
+    @Test
35
+    public void testsquareroot(){
36
+        double x = 25.0;
37
+
38
+        double actualResult = CalcEngine.squareroot(x);
39
+
40
+        double expectResult = 5.0;
41
+        assertEquals(expectResult, actualResult, 0.001);
42
+    }
43
+
44
+    @Test
45
+    public void testinverse(){
46
+        double x = 2;
47
+
48
+        double actualResult = CalcEngine.inverse(x);
49
+
50
+        double expectResult = 0.5;
51
+        assertEquals(expectResult, actualResult, 0.001);
52
+    }
53
+
54
+    @Test
55
+    public void testinvert(){
56
+        double x = 12.0;
57
+
58
+        double actualResult = CalcEngine.invert(x);
59
+
60
+        double expectResult = -12.0;
61
+        assertEquals(expectResult, actualResult, 0.001);
25
     }
62
     }
26
 
63
 
64
+
27
     @Test
65
     @Test
28
-    public void subtract() {
29
-        Assert.assertTrue("", (testCalc.subtract(17.0, 13.0) == 4.0));
66
+    public void testAddition()
67
+    {
68
+        assertEquals(20.0, CalcEngine.addition(15, 5), 0.1);
30
     }
69
     }
31
 
70
 
71
+
32
     @Test
72
     @Test
33
-    public void multiply() {
34
-        Assert.assertTrue("", (testCalc.multiply(8.0, 3.0) == 24.0));
73
+    public void testSubstraction()
74
+    {
75
+        assertEquals(3.0, CalcEngine.subtraction(5, 2), 0.1);
76
+
77
+
35
     }
78
     }
36
 
79
 
37
     @Test
80
     @Test
38
-    public void divide() {
39
-        Assert.assertTrue("", (testCalc.divide(10.0, 2.0) == 5.0));
81
+    public void testMultiplication(){
82
+        assertEquals(6.0, CalcEngine.multiplication(3, 2), 0.1);
40
     }
83
     }
84
+
85
+    @Test
86
+    public void testDivision()
87
+    {
88
+        assertEquals(3.0, CalcEngine.division(6, 2), 0.1);
89
+    }
90
+
91
+    @Test
92
+    public void testExponent()
93
+    {
94
+        assertEquals(64.0, CalcEngine.exponent(4, 3), 0.1);
95
+    }
96
+
41
 }
97
 }