瀏覽代碼

start of a calculator

Margaret Pierse 6 年之前
父節點
當前提交
ea0ea5e429
共有 4 個檔案被更改,包括 145 行新增33 行删除
  1. 32
    0
      Calculator.java
  2. 68
    5
      Console.java
  3. 14
    7
      MainApplication.java
  4. 31
    21
      package.bluej

+ 32
- 0
Calculator.java 查看文件

@@ -0,0 +1,32 @@
1
+
2
+/**
3
+ * Write a description of class Calculator here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class Calculator
9
+
10
+{
11
+    public static double add(double x, double y) {
12
+    double result = x+y;
13
+    return result;
14
+    }
15
+    
16
+    public static double subtract(double x, double y) {
17
+    double result = x-y;
18
+    return result;
19
+    }
20
+    
21
+    public static double multiply(double x, double y) {
22
+    double result = x*y;
23
+    return result;
24
+    }
25
+    
26
+    public static double divide(double x, double y) {
27
+    double result = x/y;
28
+    return result;
29
+    }
30
+
31
+  
32
+}

+ 68
- 5
Console.java 查看文件

@@ -1,11 +1,10 @@
1
- 
2 1
 
3 2
 import java.util.Scanner;
4
-
5 3
 /**
6 4
  * Created by leon on 2/9/18.
7 5
  */
8 6
 public class Console {
7
+    Calculator calc = new Calculator();
9 8
 
10 9
     public static void print(String output, Object... args) {
11 10
         System.out.printf(output, args);
@@ -15,7 +14,62 @@ public class Console {
15 14
         print(output + "\n", args);
16 15
     }
17 16
 
18
-    public static String getStringInput(String prompt) {
17
+    public static String getMethod() {
18
+        Scanner scanner = new Scanner(System.in);
19
+        String userInput = scanner.nextLine();
20
+        return userInput;
21
+    }
22
+
23
+    public static String switchStatment() {
24
+        Scanner scanner = new Scanner(System.in);
25
+        String ultimateAnswer = "";
26
+        Boolean turnOff = false;
27
+        switch (getMethod()) {
28
+            case "add" :
29
+                System.out.println("Enter your numbers:");
30
+                double userInput = scanner.nextDouble();
31
+                double nextInput = scanner.nextDouble();
32
+                
33
+                double answer = Calculator.add(userInput, nextInput);
34
+                ultimateAnswer = Double.toString(answer);
35
+                return ultimateAnswer;
36
+                
37
+            case "subtract" :
38
+                System.out.println("Enter your numbers:");
39
+                userInput = scanner.nextDouble();
40
+                nextInput = scanner.nextDouble();
41
+                
42
+                answer = Calculator.subtract(userInput, nextInput);
43
+                ultimateAnswer = Double.toString(answer);
44
+                return ultimateAnswer;
45
+                
46
+            case "multiply" :
47
+                System.out.println("Enter your numbers:");
48
+                userInput = scanner.nextDouble();
49
+                nextInput = scanner.nextDouble();
50
+                
51
+                answer = Calculator.multiply(userInput, nextInput);
52
+                ultimateAnswer = Double.toString(answer);
53
+                return ultimateAnswer;
54
+                
55
+            case "divide" :
56
+                System.out.println("Enter your numbers:");
57
+                userInput = scanner.nextDouble();
58
+                nextInput = scanner.nextDouble();
59
+                
60
+                answer = Calculator.divide(userInput, nextInput);
61
+                ultimateAnswer = Double.toString(answer);
62
+                return ultimateAnswer;
63
+                
64
+            case "quit" :
65
+                    return "quit";
66
+           
67
+
68
+        }
69
+        return ultimateAnswer;
70
+    }
71
+
72
+    /*public static String getStringInput(String prompt) {
19 73
         Scanner scanner = new Scanner(System.in);
20 74
         println(prompt);
21 75
         String userInput = scanner.nextLine();
@@ -23,10 +77,19 @@ public class Console {
23 77
     }
24 78
 
25 79
     public static Integer getIntegerInput(String prompt) {
80
+        Scanner scanner = new Scanner(System.in);
81
+        println(prompt);
82
+        int userInput = scanner.nextInt();
26 83
         return null;
27 84
     }
28 85
 
29 86
     public static Double getDoubleInput(String prompt) {
30
-        return null;
31
-    }
87
+        Scanner scanner = new Scanner(System.in);
88
+        println(prompt);
89
+        double userInput = scanner.nextDouble();
90
+        double nextInput = scanner.nextDouble();
91
+        double answer = Calculator.add(userInput, nextInput);
92
+        return answer;
93
+    }*/
94
+
32 95
 }

+ 14
- 7
MainApplication.java 查看文件

@@ -4,14 +4,21 @@
4 4
  * Created by leon on 2/9/18.
5 5
  */
6 6
 public class MainApplication {
7
+    
7 8
     public static void main(String[] args) {
8
-        Console.println("Welcome to my calculator!");
9
-        String s = Console.getStringInput("Enter a string");
10
-        Integer i = Console.getIntegerInput("Enter an integer");
11
-        Double d = Console.getDoubleInput("Enter a double.");
9
+        String d = "";
10
+        while (d != "quit") {
11
+        Console.println("Welcome to my calculator! What would you like to do?");
12
+        d = Console.switchStatment();
13
+        //String s = Console.getStringInput("Enter a string");
14
+        //Integer i = Console.getIntegerInput("Enter an integer");
15
+        //Double d = Console.getDoubleInput("Enter a double.");
12 16
 
13
-        Console.println("The user input %s as a string", s);
14
-        Console.println("The user input %s as a integer", i);
15
-        Console.println("The user input %s as a d", d);
17
+        //Console.println("The user input %s as a string", s);
18
+        //Console.println("The user input %s as a integer", i);
19
+        Console.println("Your answer is %s", d);
16 20
     }
17 21
 }
22
+    
23
+    
24
+}

+ 31
- 21
package.bluej 查看文件

@@ -2,22 +2,25 @@
2 2
 dependency1.from=MainApplication
3 3
 dependency1.to=Console
4 4
 dependency1.type=UsesDependency
5
-editor.fx.0.height=722
6
-editor.fx.0.width=800
7
-editor.fx.0.x=640
8
-editor.fx.0.y=23
9
-objectbench.height=214
10
-objectbench.width=595
11
-package.divider.horizontal=0.6
12
-package.divider.vertical=0.6847360912981455
5
+dependency2.from=Console
6
+dependency2.to=Calculator
7
+dependency2.type=UsesDependency
8
+editor.fx.0.height=1020
9
+editor.fx.0.width=1247
10
+editor.fx.0.x=2093
11
+editor.fx.0.y=165
12
+objectbench.height=80
13
+objectbench.width=352
14
+package.divider.horizontal=0.5993322203672788
15
+package.divider.vertical=0.8465608465608465
13 16
 package.editor.height=473
14 17
 package.editor.width=493
15
-package.editor.x=35
16
-package.editor.y=60
17
-package.frame.height=759
18
+package.editor.x=1446
19
+package.editor.y=445
20
+package.frame.height=625
18 21
 package.frame.width=619
19
-package.numDependencies=1
20
-package.numTargets=2
22
+package.numDependencies=2
23
+package.numTargets=3
21 24
 package.showExtends=true
22 25
 package.showUses=true
23 26
 project.charset=UTF-8
@@ -27,16 +30,23 @@ readme.width=47
27 30
 readme.x=10
28 31
 readme.y=10
29 32
 target1.height=50
30
-target1.name=Console
33
+target1.name=Calculator
31 34
 target1.showInterface=false
32 35
 target1.type=ClassTarget
33
-target1.width=80
34
-target1.x=80
35
-target1.y=200
36
+target1.width=90
37
+target1.x=200
38
+target1.y=310
36 39
 target2.height=50
37
-target2.name=MainApplication
40
+target2.name=Console
38 41
 target2.showInterface=false
39 42
 target2.type=ClassTarget
40
-target2.width=120
41
-target2.x=70
42
-target2.y=70
43
+target2.width=80
44
+target2.x=80
45
+target2.y=200
46
+target3.height=50
47
+target3.name=MainApplication
48
+target3.showInterface=false
49
+target3.type=ClassTarget
50
+target3.width=120
51
+target3.x=70
52
+target3.y=70