Browse Source

Added few functions to the CalcSkin Project. UI is running good.

thulasi 6 years ago
parent
commit
d128fe197d

BIN
.DS_Store View File


BIN
src/.DS_Store View File


BIN
src/main/.DS_Store View File


BIN
src/main/java/.DS_Store View File


BIN
src/main/java/rocks/.DS_Store View File


+ 57
- 8
src/main/java/rocks/zipcode/calcskin/CalcEngine.java View File

@@ -5,19 +5,68 @@ public class CalcEngine {
5 5
     CalcEngine() {
6 6
     }
7 7
 
8
-    public double add(double v, double v1) {
9
-        return Double.NaN;
8
+    public static double addition(double x, double y) {
9
+        return (x + y);
10 10
     }
11 11
 
12
-    public double subtract(double v, double v1) {
13
-        return Double.NaN;
12
+    public static double subtraction(double x, double y) {
13
+        return (x - y);
14 14
     }
15 15
 
16
-    public double multiply(double v, double v1) {
17
-        return Double.NaN;
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);
18 23
     }
19 24
 
20
-    public double divide(double v, double v1) {
21
-        return Double.NaN;
25
+    public static double square(double x) {
26
+        return Math.pow(x, 2);
22 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
+    }
45
+
46
+/*
47
+    public static double findSin(double x) {
48
+        return Math.sin(x);
49
+    }
50
+
51
+    public static double findCos(double x) {
52
+        return Math.cos(x);
53
+    }
54
+
55
+    public static double findTan(double x) {
56
+        return Math.tan(x);
57
+    }
58
+
59
+    public static double findAsin(double x) {
60
+        return Math.asin(x);
61
+    }
62
+
63
+    public static double findAcos(double x) {
64
+        return Math.acos(x);
65
+    }
66
+
67
+    public static double findAtan(double x) {
68
+        return Math.atan(x);
69
+    }
70
+*/
71
+
23 72
 }

+ 17
- 6
src/main/java/rocks/zipcode/calcskin/CalcSkin.java View File

@@ -13,6 +13,7 @@ import javafx.scene.layout.*;
13 13
 import javafx.stage.Stage;
14 14
 import javafx.stage.StageStyle;
15 15
 import rocks.zipcode.calcskin.CalcEngine;
16
+import sun.print.CUPSPrinter;
16 17
 
17 18
 import java.util.HashMap;
18 19
 import java.util.Map;
@@ -27,7 +28,8 @@ public class CalcSkin extends Application {
27 28
             { "7", "8", "9", "/" },
28 29
             { "4", "5", "6", "*" },
29 30
             { "1", "2", "3", "-" },
30
-            { "0", "c", "=", "+" }
31
+            { "0", "c", "=", "+" },
32
+            { "√", "^", "~", "²"}
31 33
     };
32 34
 
33 35
     private final Map<String, Button> accelerators = new HashMap<>();
@@ -36,7 +38,7 @@ public class CalcSkin extends Application {
36 38
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 39
     private CalcEngine calcEngine = new CalcEngine();
38 40
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
41
+    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUAREROT, SQUARE, EXPONENT, INVERT}
40 42
 
41 43
     private Op curOp   = Op.NOOP;
42 44
     private Op stackOp = Op.NOOP;
@@ -127,6 +129,10 @@ public class CalcSkin extends Application {
127 129
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 130
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 131
             case "/": triggerOp.set(Op.DIVIDE);   break;
132
+            case "²": triggerOp.set(Op.SQUARE);   break;
133
+            case "√": triggerOp.set(Op.SQUAREROT);  break;
134
+            case "^": triggerOp.set(Op.EXPONENT);   break;
135
+            case "~": triggerOp.set(Op.INVERT);     break;
130 136
         }
131 137
         return triggerOp;
132 138
     }
@@ -181,10 +187,15 @@ public class CalcSkin extends Application {
181 187
             @Override
182 188
             public void handle(ActionEvent actionEvent) {
183 189
                 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;
190
+                    case ADD:      currentValue.set(calcEngine.addition(previousValue.get(), currentValue.get())); break;
191
+                    case SUBTRACT: currentValue.set(calcEngine.subtraction(previousValue.get(), currentValue.get())); break;
192
+                    case MULTIPLY: currentValue.set(calcEngine.multiplication(previousValue.get(), currentValue.get())); break;
193
+                    case DIVIDE:   currentValue.set(calcEngine.division(previousValue.get(), currentValue.get())); break;
194
+                    case SQUARE:   currentValue.set(calcEngine.square(previousValue.get())); break;
195
+                    case SQUAREROT: currentValue.set(calcEngine.squareroot(previousValue.get()));   break;
196
+                    case EXPONENT:  currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
197
+                    case INVERT:    currentValue.set(calcEngine.invert(previousValue.get()));   break;
198
+
188 199
                 }
189 200
             }
190 201
         });

+ 126
- 8
src/test/java/rocks/zipcode/calcskin/CalcEngineTest.java View File

@@ -20,22 +20,140 @@ public class CalcEngineTest {
20 20
     }
21 21
 
22 22
     @Test
23
-    public void add() {
24
-        Assert.assertTrue("", (testCalc.add(1.0, 1.0) == 2.0));
23
+    public void testSquare(){
24
+        // set up data
25
+        double x = 3.0;
26
+
27
+        //invoke method under test
28
+        double actualResult = CalcEngine.square(x);
29
+
30
+        //verify result
31
+        double expectResult = 9.0;
32
+        assertEquals(expectResult, actualResult, 0.001);
33
+
25 34
     }
26 35
 
27 36
     @Test
28
-    public void subtract() {
29
-        Assert.assertTrue("", (testCalc.subtract(17.0, 13.0) == 4.0));
37
+    public void testsquareroot(){
38
+        double x = 25.0;
39
+
40
+        double actualResult = CalcEngine.squareroot(x);
41
+
42
+        double expectResult = 5.0;
43
+        assertEquals(expectResult, actualResult, 0.001);
30 44
     }
31 45
 
32 46
     @Test
33
-    public void multiply() {
34
-        Assert.assertTrue("", (testCalc.multiply(8.0, 3.0) == 24.0));
47
+    public void testinverse(){
48
+        double x = 2;
49
+
50
+        double actualResult = CalcEngine.inverse(x);
51
+
52
+        double expectResult = 0.5;
53
+        assertEquals(expectResult, actualResult, 0.001);
35 54
     }
36 55
 
37 56
     @Test
38
-    public void divide() {
39
-        Assert.assertTrue("", (testCalc.divide(10.0, 2.0) == 5.0));
57
+    public void testinvert(){
58
+        double x = 12.0;
59
+
60
+        double actualResult = CalcEngine.invert(x);
61
+
62
+        double expectResult = -12.0;
63
+        assertEquals(expectResult, actualResult, 0.001);
40 64
     }
65
+
66
+/*    @Test
67
+    public void testfindSin(){
68
+        double x = 90.0;
69
+
70
+        double actualResult = CalcEngine.findSin(x);
71
+
72
+        double expectResult = 0.8939966636;
73
+        assertEquals(expectResult, actualResult, 0.001);
74
+    }
75
+
76
+    @Test
77
+    public void testfindCos(){
78
+        double x = 45.0;
79
+
80
+        double actualResult = CalcEngine.findCos(x);
81
+
82
+        double expectResult = 0.52532198881;
83
+        assertEquals(expectResult, actualResult, 0.001);
84
+    }
85
+
86
+    @Test
87
+    public void testfindTan(){
88
+        double x = 100.0;
89
+
90
+        double actualResult = CalcEngine.findTan(x);
91
+
92
+        double expectResult = -0.58721391515;
93
+        assertEquals(expectResult, actualResult, 0.001);
94
+    }
95
+
96
+    @Test
97
+    public void testfindAsin(){
98
+        double x = 0.5;
99
+
100
+        double actualResult = CalcEngine.findAsin(x);
101
+
102
+        double expectResult = 0.52359877559;
103
+        assertEquals(expectResult, actualResult, 0.001);
104
+    }
105
+
106
+    @Test
107
+    public void testfindAcos(){
108
+        double x = 1.0;
109
+
110
+        double actualResult = CalcEngine.findAcos(x);
111
+
112
+        double expectResult = 0;
113
+        assertEquals(expectResult, actualResult, 0.001);
114
+    }
115
+
116
+    @Test
117
+    public void testfindAtan(){
118
+        double x = 1.0;
119
+
120
+        double actualResult = CalcEngine.findAtan(x);
121
+
122
+        double expectResult = 0.78539816339;
123
+        assertEquals(expectResult, actualResult, 0.001);
124
+
125
+    }*/
126
+
127
+    @Test
128
+    public void testAddition()
129
+    {
130
+        assertEquals(20.0, CalcEngine.addition(15, 5), 0.1);
131
+    }
132
+
133
+
134
+    @Test
135
+    public void testSubstraction()
136
+    {
137
+        assertEquals(3.0, CalcEngine.subtraction(5, 2), 0.1);
138
+
139
+
140
+    }
141
+
142
+    @Test
143
+    public void testMultiplication(){
144
+        assertEquals(6.0, CalcEngine.multiplication(3, 2), 0.1);
145
+    }
146
+
147
+    @Test
148
+    public void testDivision()
149
+    {
150
+        assertEquals(3.0, CalcEngine.division(6, 2), 0.1);
151
+    }
152
+
153
+    @Test
154
+    public void testExponent()
155
+    {
156
+        assertEquals(64.0, CalcEngine.exponent(4, 3), 0.1);
157
+    }
158
+
41 159
 }