Quellcode durchsuchen

added extra functions

Jennifer Chao vor 6 Jahren
Ursprung
Commit
668e244c41

+ 52
- 8
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Datei anzeigen

@@ -5,19 +5,63 @@ public class CalcEngine {
5 5
     CalcEngine() {
6 6
     }
7 7
 
8
-    public double add(double v, double v1) {
9
-        return Double.NaN;
8
+    public double add(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 double subtract(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 double multiply(double x, double y) {
17
+        return x * y;
18 18
     }
19 19
 
20
-    public double divide(double v, double v1) {
21
-        return Double.NaN;
20
+    public double divide(double x, double y) {
21
+        return x / y;
22
+    }
23
+
24
+    public double square(double x) {
25
+        return Math.pow(x, 2.0D);
26
+    }
27
+
28
+    public double squareroot(double x) {
29
+        return Math.sqrt(x);
30
+    }
31
+
32
+    public double power(double x, double y) {
33
+        return Math.pow(x, y);
34
+    }
35
+
36
+    public double inverse(double x) {
37
+        return 1.0D / x;
38
+    }
39
+
40
+    public static double invert(double x) {
41
+        return -x;
42
+    }
43
+
44
+    public static double findSin(double x) {
45
+        return Math.sin(x);
46
+    }
47
+
48
+    public static double findCos(double x) {
49
+        return Math.cos(x);
50
+    }
51
+
52
+    public static double findTan(double x) {
53
+        return Math.tan(x);
54
+    }
55
+
56
+    public static double findAsin(double x) {
57
+        return Math.asin(x);
58
+    }
59
+
60
+    public static double findAcos(double x) {
61
+        return Math.acos(x);
62
+    }
63
+
64
+    public static double findAtan(double x) {
65
+        return Math.atan(x);
22 66
     }
23 67
 }

+ 16
- 8
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Datei anzeigen

@@ -24,10 +24,10 @@ 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", "/", "x²" },
28
+            { "4", "5", "6", "*", "√x" },
29
+            { "1", "2", "3", "-", "x^" },
30
+            { "0", "c", "=", "+", "1/x" }
31 31
     };
32 32
 
33 33
     private final Map<String, Button> accelerators = new HashMap<>();
@@ -36,7 +36,7 @@ public class CalcSkin extends Application {
36 36
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 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, POWER, INVERSE }
40 40
 
41 41
     private Op curOp   = Op.NOOP;
42 42
     private Op stackOp = Op.NOOP;
@@ -50,7 +50,7 @@ public class CalcSkin extends Application {
50 50
         final TextField screen  = createScreen();
51 51
         final TilePane  buttons = createButtons();
52 52
 
53
-        stage.setTitle("Calc");
53
+        stage.setTitle("Calculator");
54 54
         stage.initStyle(StageStyle.UTILITY);
55 55
         stage.setResizable(false);
56 56
         stage.setScene(new Scene(createLayout(screen, buttons)));
@@ -60,7 +60,7 @@ public class CalcSkin extends Application {
60 60
     private VBox createLayout(TextField screen, TilePane buttons) {
61 61
         final VBox layout = new VBox(20);
62 62
         layout.setAlignment(Pos.CENTER);
63
-        layout.setStyle("-fx-background-color: silver; -fx-padding: 20; -fx-font-size: 20;");
63
+        layout.setStyle("-fx-background-color: AQUAMARINE; -fx-padding: 20; -fx-font-size: 20;");
64 64
         layout.getChildren().setAll(screen, buttons);
65 65
         handleAccelerators(layout);
66 66
         screen.prefWidthProperty().bind(buttons.widthProperty());
@@ -81,7 +81,7 @@ public class CalcSkin extends Application {
81 81
 
82 82
     private TextField createScreen() {
83 83
         final TextField screen = new TextField();
84
-        screen.setStyle("-fx-background-color: aquamarine;");
84
+        screen.setStyle("-fx-background-color: HONEYDEW;");
85 85
         screen.setAlignment(Pos.CENTER_RIGHT);
86 86
         screen.setEditable(false);
87 87
         screen.textProperty().bind(Bindings.format("%.0f", currentValue));
@@ -127,6 +127,10 @@ public class CalcSkin extends Application {
127 127
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 128
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 129
             case "/": triggerOp.set(Op.DIVIDE);   break;
130
+            case "x²": triggerOp.set(Op.SQUARE);      break;
131
+            case "√x": triggerOp.set(Op.SQUAREROOT); break;
132
+            case "x^": triggerOp.set(Op.POWER); break;
133
+            case "1/x": triggerOp.set(Op.INVERSE);   break;
130 134
         }
131 135
         return triggerOp;
132 136
     }
@@ -185,6 +189,10 @@ public class CalcSkin extends Application {
185 189
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 190
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 191
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
192
+                    case SQUARE:    currentValue.set(calcEngine.square(currentValue.get())); break;
193
+                    case SQUAREROOT:    currentValue.set(calcEngine.squareroot(currentValue.get())); break;
194
+                    case POWER:     currentValue.set(calcEngine.power(previousValue.get(), currentValue.get())); break;
195
+                    case INVERSE:      currentValue.set(calcEngine.inverse(currentValue.get())); break;
188 196
                 }
189 197
             }
190 198
         });