Yesoda Sanka 6 år sedan
förälder
incheckning
87374512eb

+ 42
- 5
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Visa fil

@@ -6,18 +6,55 @@ public class CalcEngine {
6 6
     }
7 7
 
8 8
     public double add(double v, double v1) {
9
-        return Double.NaN;
9
+
10
+        return v + v1;
10 11
     }
11 12
 
12 13
     public double subtract(double v, double v1) {
13
-        return Double.NaN;
14
+
15
+        return v - v1;
14 16
     }
15 17
 
16 18
     public double multiply(double v, double v1) {
17
-        return Double.NaN;
19
+
20
+        return v * v1;
18 21
     }
19 22
 
20 23
     public double divide(double v, double v1) {
21
-        return Double.NaN;
24
+
25
+        return v / v1;
26
+    }
27
+
28
+    public double sin(double v) {
29
+        return Math.sin(v);
30
+    }
31
+
32
+    public double cos(double v) {
33
+        return Math.cos(v);
34
+    }
35
+
36
+    public double tan(double v) {
37
+        return Math.tan(v);
38
+    }
39
+
40
+    public double inverse(double x) {
41
+        double v=(1/x);
42
+        return v;
43
+    }
44
+    public double sroot(double x)
45
+    {
46
+        return Math.sqrt(x) ;
47
+    }
48
+    public double xsqu(double x)
49
+    {
50
+        return x*x;
51
+    }
52
+    public double power(double x,double y)
53
+    {
54
+        return Math.pow(x,y) ;
55
+    }
56
+    public double mod(double x,double y)
57
+    {
58
+        return x%y;
22 59
     }
23
-}
60
+}

+ 26
- 7
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Visa fil

@@ -24,10 +24,11 @@ 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", "sin" ,"sroot","/"},
28
+            { "4", "5", "6", "cos","xsqu","*" },
29
+            { "1", "2", "3", "tan"," power","-" },
30
+            { "0", "c", "=", "inverse","Mod","+" }
31
+
31 32
     };
32 33
 
33 34
     private final Map<String, Button> accelerators = new HashMap<>();
@@ -36,7 +37,7 @@ public class CalcSkin extends Application {
36 37
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 38
     private CalcEngine calcEngine = new CalcEngine();
38 39
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
40
+    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE,SIN,COS,TAN,INVERSE,SROOT,XSQU,POWER,MOD }
40 41
 
41 42
     private Op curOp   = Op.NOOP;
42 43
     private Op stackOp = Op.NOOP;
@@ -81,10 +82,10 @@ public class CalcSkin extends Application {
81 82
 
82 83
     private TextField createScreen() {
83 84
         final TextField screen = new TextField();
84
-        screen.setStyle("-fx-background-color: aquamarine;");
85
+        screen.setStyle("-fx-background-color: pink;");
85 86
         screen.setAlignment(Pos.CENTER_RIGHT);
86 87
         screen.setEditable(false);
87
-        screen.textProperty().bind(Bindings.format("%.0f", currentValue));
88
+            screen.textProperty().bind(Bindings.format("%.0f", currentValue));
88 89
         return screen;
89 90
     }
90 91
 
@@ -122,11 +123,21 @@ public class CalcSkin extends Application {
122 123
 
123 124
     private ObjectProperty<Op> determineOperand(String s) {
124 125
         final ObjectProperty<Op> triggerOp = new SimpleObjectProperty<>(Op.NOOP);
126
+        s = s.toLowerCase();
125 127
         switch (s) {
126 128
             case "+": triggerOp.set(Op.ADD);      break;
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 "sin": triggerOp.set(Op.SIN);    break;
133
+            case "cos": triggerOp.set(Op.COS);    break;
134
+            case "tan": triggerOp.set(Op.TAN);    break;
135
+            case "inverse": triggerOp.set(Op.INVERSE);   break;
136
+            case "sroot": triggerOp.set(Op.SROOT);       break;
137
+            case "xsqu": triggerOp.set(Op.XSQU );        break;
138
+            case "power": triggerOp.set(Op.POWER);       break;
139
+            case "mod": triggerOp.set(Op.MOD);           break;
140
+
130 141
         }
131 142
         return triggerOp;
132 143
     }
@@ -185,6 +196,14 @@ public class CalcSkin extends Application {
185 196
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 197
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 198
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
199
+                    case SIN:   currentValue.set(calcEngine.sin( currentValue.get())); break;
200
+                    case COS:  currentValue.set(calcEngine.cos( currentValue.get()) ); break;
201
+                    case TAN:   currentValue.set(calcEngine.tan( currentValue.get())); break;
202
+                    case INVERSE :   currentValue.set(calcEngine.inverse(currentValue.get())); break;
203
+                    case SROOT :   currentValue.set(calcEngine.sroot(currentValue.get())); break;
204
+                    case XSQU :  currentValue.set(calcEngine.xsqu(currentValue.get())); break;
205
+                    case POWER :  currentValue.set(calcEngine.power(previousValue.get(),currentValue.get())); break;
206
+                    case MOD:   currentValue.set(calcEngine.mod(previousValue.get(),currentValue.get())); break;
188 207
                 }
189 208
             }
190 209
         });