Soujanya Buragapu 6 gadus atpakaļ
vecāks
revīzija
097cd14a3f

Binārs
.DS_Store Parādīt failu


Binārs
src/.DS_Store Parādīt failu


Binārs
src/main/.DS_Store Parādīt failu


Binārs
src/main/java/.DS_Store Parādīt failu


Binārs
src/main/java/rocks/.DS_Store Parādīt failu


Binārs
src/main/java/rocks/zipcode/.DS_Store Parādīt failu


+ 36
- 13
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Parādīt failu

@@ -3,21 +3,44 @@ package rocks.zipcode.calcskin;
3 3
 public class CalcEngine {
4 4
 
5 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 Parādīt failu

@@ -1,5 +1,4 @@
1 1
 package rocks.zipcode.calcskin;
2
-
3 2
 import javafx.application.Application;
4 3
 import javafx.beans.binding.Bindings;
5 4
 import javafx.beans.property.*;
@@ -12,12 +11,9 @@ import javafx.scene.input.KeyEvent;
12 11
 import javafx.scene.layout.*;
13 12
 import javafx.stage.Stage;
14 13
 import javafx.stage.StageStyle;
15
-import rocks.zipcode.calcskin.CalcEngine;
16
-
17 14
 import java.util.HashMap;
18 15
 import java.util.Map;
19 16
 
20
-// a simple JavaFX calculator.
21 17
 public class CalcSkin extends Application {
22 18
 
23 19
     public static void main(String[] args){
@@ -27,19 +23,19 @@ public class CalcSkin extends Application {
27 23
             { "7", "8", "9", "/" },
28 24
             { "4", "5", "6", "*" },
29 25
             { "1", "2", "3", "-" },
30
-            { "0", "c", "=", "+" }
26
+            { "0", "c", "=", "+" },
27
+            { "√", "^", "~", "²"}
31 28
     };
32 29
 
33 30
     private final Map<String, Button> accelerators = new HashMap<>();
34
-
35 31
     private DoubleProperty previousValue = new SimpleDoubleProperty();
36 32
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 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 40
     public static void launchCalc(String... args) {
45 41
         launch(args);
@@ -107,9 +103,9 @@ public class CalcSkin extends Application {
107 103
         if (s.matches("[0-9]")) {
108 104
             makeNumericButton(s, button);
109 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 109
             } else if ("c".equals(s)) {
114 110
                 makeClearButton(button);
115 111
             } else if ("=".equals(s)) {
@@ -120,30 +116,34 @@ public class CalcSkin extends Application {
120 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 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 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 136
         button.setOnAction(new EventHandler<ActionEvent>() {
137 137
             @Override
138 138
             public void handle(ActionEvent actionEvent) {
139
-                curOp = triggerOp.get();
139
+                first = triggerCal.get();
140 140
             }
141 141
         });
142 142
     }
143 143
 
144 144
     private Button makeStandardButton(String s) {
145 145
         Button button = new Button(s);
146
-        button.setStyle("-fx-base: beige;");
146
+        button.setStyle("-fx-base: pink;");
147 147
         accelerators.put(s, button);
148 148
         button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
149 149
         return button;
@@ -153,20 +153,20 @@ public class CalcSkin extends Application {
153 153
         button.setOnAction(new EventHandler<ActionEvent>() {
154 154
             @Override
155 155
             public void handle(ActionEvent actionEvent) {
156
-                if (curOp == Op.NOOP) {
156
+                if (first == Cal.operation) {
157 157
                     currentValue.set(currentValue.get() * 10 + Integer.parseInt(s));
158 158
                 } else {
159 159
                     previousValue.set(currentValue.get());
160 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 168
     private void makeClearButton(Button button) {
169
-        button.setStyle("-fx-base: mistyrose;");
169
+        button.setStyle("-fx-base: grey;");
170 170
         button.setOnAction(new EventHandler<ActionEvent>() {
171 171
             @Override
172 172
             public void handle(ActionEvent actionEvent) {
@@ -176,19 +176,23 @@ public class CalcSkin extends Application {
176 176
     }
177 177
 
178 178
     private void makeEqualsButton(Button button) {
179
-        button.setStyle("-fx-base: ghostwhite;");
179
+        button.setStyle("-fx-base: green;");
180 180
         button.setOnAction(new EventHandler<ActionEvent>() {
181 181
             @Override
182 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 Parādīt failu

@@ -1,8 +1,4 @@
1 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 2
 public class MainApplication {
7 3
     public static void main(String[] args){
8 4
 

+ 66
- 10
src/test/java/rocks/zipcode/calcskin/CalcEngineTest.java Parādīt failu

@@ -1,10 +1,8 @@
1 1
 package rocks.zipcode.calcskin;
2 2
 
3 3
 import org.junit.After;
4
-import org.junit.Assert;
5 4
 import org.junit.Before;
6 5
 import org.junit.Test;
7
-
8 6
 import static org.junit.Assert.*;
9 7
 
10 8
 public class CalcEngineTest {
@@ -20,22 +18,80 @@ public class CalcEngineTest {
20 18
     }
21 19
 
22 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 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 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 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
 }