Seth 6 年之前
父節點
當前提交
737b8ffc44
共有 6 個文件被更改,包括 360 次插入205 次删除
  1. 217
    39
      Calculator.java
  2. 0
    122
      Console.java
  3. 0
    24
      MainApplication.java
  4. 136
    0
      MathMethods.java
  5. 0
    20
      Memory.java
  6. 7
    0
      package.bluej

+ 217
- 39
Calculator.java 查看文件

@@ -1,3 +1,4 @@
1
+import java.util.*;
1 2
 
2 3
 /**
3 4
  * Write a description of class Calculator here.
@@ -5,46 +6,223 @@
5 6
  * @author (your name)
6 7
  * @version (a version number or a date)
7 8
  */
8
-public class Calculator
9 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
-    public static double square(double x) {
32
-    double result = Math.pow(x, 2);
33
-    return result;
34
-    }
35
-    
36
-    public static double inverse(double x) {
37
-    double result = x;
38
-    return result;
39
-    }
40
-    
41
-    public static double tangent(double x) {
42
-    double result = Math.tan(x);
43
-    return result;
44
-    }
10
+class Calculator{
45 11
     
46
-    public static double itan(double x) {
47
-    double result = Math.atan(x);
48
-    return result;
12
+    public static void main(String args[]){
13
+
14
+        String toDo;                //what the user wants to do
15
+        double num1 = 0;                //The user's first input
16
+        double num2 = 0;            //The user's second input
17
+        double result = 0;          //The user's answer
18
+        boolean off = false;        //The user typed quit, becomes true
19
+        double memory;
20
+        boolean error = false;
21
+        MathMethods calc = new MathMethods();
22
+
23
+        while (off == false){
24
+            Scanner input1 = new Scanner(System.in);
25
+            System.out.println("What do you want to do?");
26
+            toDo = input1.nextLine();
27
+
28
+            if (toDo.equals("quit")){
29
+                off = true;
30
+                System.out.println("Goodbye!!");
31
+
32
+            } else {
33
+
34
+                //System.out.println("toDO is: " + toDo);
35
+
36
+                System.out.println(toDo + " -- Enter first number:");
37
+                num1 = input1.nextDouble();
38
+                //System.out.println("num1 is: " + num1);
39
+
40
+                switch (toDo){
41
+                    case "add":
42
+                    case "subtract":
43
+                    case "multiply":
44
+                    case "divide":
45
+                    case "exponent":
46
+                    System.out.println(toDo + " -- Enter second number:");
47
+                    num2 = input1.nextDouble();
48
+                    //System.out.println("num2 is: " + num2);
49
+
50
+                }
51
+
52
+                switch (toDo) {        
53
+                    case "add":
54
+                    result = calc.add(num1, num2);
55
+                    memory = result;
56
+                    calc.printAns(String.valueOf(result));
57
+                    break;
58
+
59
+                    case "subtract":
60
+                    result = calc.subtract(num1, num2);
61
+                    memory = result;
62
+                    calc.printAns(String.valueOf(result));
63
+                    break;
64
+
65
+                    case "multiply":
66
+                    result = calc.multiply(num1, num2);
67
+                    memory = result;
68
+                    calc.printAns(String.valueOf(result));
69
+                    break;
70
+
71
+                    case "divide":
72
+                    result = calc.divide(num1, num2);
73
+                    memory = result;
74
+                    calc.printAns(String.valueOf(result));
75
+                    break;
76
+
77
+                    case "exponent":
78
+                    result = calc.exponent(num1, num2);
79
+                    memory = result;
80
+                    calc.printAns(String.valueOf(result));
81
+                    break;
82
+
83
+                    case "cosine":
84
+                    result = calc.cosine(num1);
85
+                    memory = result;
86
+                    calc.printAns(String.valueOf(result));
87
+                    break;
88
+
89
+                    case "inverse cosine":
90
+                    result = calc.invCosine(num1);
91
+                    memory = result;
92
+                    calc.printAns(String.valueOf(result));
93
+                    break;
94
+
95
+                    case "square root":
96
+                    result = calc.squareRoot(num1);
97
+                    memory = result;
98
+                    calc.printAns(String.valueOf(result));
99
+                    break;
100
+                    
101
+                    case "invert":
102
+                    result = calc.invert(num1);
103
+                    memory = result;
104
+                    calc.printAns(String.valueOf(result));
105
+                    break;
106
+                    
107
+                    case "sine":
108
+                    result = calc.sin(num1);
109
+                    memory = result;
110
+                    calc.printAns(String.valueOf(result));
111
+                    break;
112
+                    
113
+                    case "inverse sine":
114
+                    result = calc.iSin(num1);
115
+                    memory = result;
116
+                    calc.printAns(String.valueOf(result));
117
+                    break;
118
+                    
119
+                    case "degree to radian":
120
+                    result = calc.degToRad(num1);
121
+                    memory = result;
122
+                    calc.printAns(String.valueOf(result));
123
+                    break;
124
+                    
125
+                    case "radian to degree":
126
+                    result = calc.radToDeg(num1);
127
+                    memory = result;
128
+                    calc.printAns(String.valueOf(result));
129
+                    break;
130
+                }
131
+            }
132
+        }
49 133
     }
50 134
 }
135
+
136
+        // /**
137
+        // * Created by leon on 2/9/18.
138
+        // */
139
+        // public class Console {
140
+        // Calculator calc = new Calculator();
141
+
142
+        // public static void print(String output, Object... args) {
143
+        // System.out.printf(output, args);
144
+        // }
145
+
146
+        // public static void println(String output, Object... args) {
147
+        // print(output + "\n", args);
148
+        // }
149
+
150
+        // public static String getMethod() {
151
+        // Scanner scanner = new Scanner(System.in);
152
+        // String userInput = scanner.nextLine();
153
+        // return userInput;
154
+        // }
155
+
156
+        // public static String switchStatment() {
157
+        // Scanner scanner = new Scanner(System.in);
158
+        // String ultimateAnswer = "";
159
+        // Boolean turnOff = false;
160
+
161
+        // switch (getMethod()) {
162
+        // case "add" :
163
+        // System.out.println("Enter your numbers:");
164
+        // double userInput = scanner.nextDouble();
165
+        // double nextInput = scanner.nextDouble();
166
+
167
+        // double answer = Calculator.add(userInput, nextInput);
168
+        // ultimateAnswer = Double.toString(answer);
169
+        // return Double.toString(answer);
170
+
171
+        // case "subtract" :
172
+        // System.out.println("Enter your numbers:");
173
+        // userInput = scanner.nextDouble();
174
+        // nextInput = scanner.nextDouble();
175
+
176
+        // answer = Calculator.subtract(userInput, nextInput);
177
+        // ultimateAnswer = Double.toString(answer);
178
+        // return ultimateAnswer;
179
+
180
+        // case "multiply" :
181
+        // System.out.println("Enter your numbers:");
182
+        // userInput = scanner.nextDouble();
183
+        // nextInput = scanner.nextDouble();
184
+
185
+        // answer = Calculator.multiply(userInput, nextInput);
186
+        // ultimateAnswer = Double.toString(answer);
187
+        // return ultimateAnswer;
188
+
189
+        // case "divide" :
190
+        // System.out.println("Enter your numbers:");
191
+        // userInput = scanner.nextDouble();
192
+        // nextInput = scanner.nextDouble();
193
+
194
+        // answer = Calculator.divide(userInput, nextInput);
195
+        // ultimateAnswer = Double.toString(answer);
196
+        // return ultimateAnswer;
197
+
198
+        // case "Square Root" :
199
+
200
+           
201
+        // }
202
+        // return ultimateAnswer;
203
+        // }
204
+
205
+        // /*public static String getStringInput(String prompt) {
206
+        // Scanner scanner = new Scanner(System.in);
207
+        // println(prompt);
208
+        // String userInput = scanner.nextLine();
209
+        // return userInput;
210
+        // }
211
+
212
+        // public static Integer getIntegerInput(String prompt) {
213
+        // Scanner scanner = new Scanner(System.in);
214
+        // println(prompt);
215
+        // int userInput = scanner.nextInt();
216
+        // return null;
217
+        // }
218
+
219
+        // public static Double getDoubleInput(String prompt) {
220
+        // Scanner scanner = new Scanner(System.in);
221
+        // println(prompt);
222
+        // double userInput = scanner.nextDouble();
223
+        // double nextInput = scanner.nextDouble();
224
+        // double answer = Calculator.add(userInput, nextInput);
225
+        // return answer;
226
+        // }*/
227
+
228
+        // }

+ 0
- 122
Console.java 查看文件

@@ -1,122 +0,0 @@
1
-
2
-import java.util.Scanner;
3
-/**
4
- * Created by leon on 2/9/18.
5
- */
6
-public class Console {
7
-    Calculator calc = new Calculator();
8
-
9
-    public static void print(String output, Object... args) {
10
-        System.out.printf(output, args);
11
-    }
12
-
13
-    public static void println(String output, Object... args) {
14
-        print(output + "\n", args);
15
-    }
16
-
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 "square" :
65
-                System.out.println("Enter your number:");
66
-                userInput = scanner.nextDouble();
67
-                answer = Calculator.square(userInput);
68
-                ultimateAnswer = Double.toString(answer);
69
-                return ultimateAnswer;
70
-                
71
-            case "inverse" :
72
-                System.out.println("Enter your number:");
73
-                userInput = scanner.nextDouble();
74
-                answer = Calculator.inverse(userInput);
75
-                ultimateAnswer = String.format("1/%.0f", answer);
76
-                return ultimateAnswer;   
77
-                
78
-            case "tangent" :
79
-                System.out.println("Enter your number:");
80
-                userInput = scanner.nextDouble();
81
-                answer = Calculator.tangent(userInput);
82
-                ultimateAnswer = Double.toString(answer);
83
-                return ultimateAnswer;
84
-                
85
-            case "inverse tangent" :
86
-                System.out.println("Enter your number:");
87
-                userInput = scanner.nextDouble();
88
-                answer = Calculator.itan(userInput);
89
-                ultimateAnswer = Double.toString(answer);
90
-                return ultimateAnswer;
91
-            case "quit" :
92
-                    return "quit";
93
-           
94
-
95
-        }
96
-        return ultimateAnswer;
97
-    }
98
-
99
-    /*public static String getStringInput(String prompt) {
100
-        Scanner scanner = new Scanner(System.in);
101
-        println(prompt);
102
-        String userInput = scanner.nextLine();
103
-        return userInput;
104
-    }
105
-
106
-    public static Integer getIntegerInput(String prompt) {
107
-        Scanner scanner = new Scanner(System.in);
108
-        println(prompt);
109
-        int userInput = scanner.nextInt();
110
-        return null;
111
-    }
112
-
113
-    public static Double getDoubleInput(String prompt) {
114
-        Scanner scanner = new Scanner(System.in);
115
-        println(prompt);
116
-        double userInput = scanner.nextDouble();
117
-        double nextInput = scanner.nextDouble();
118
-        double answer = Calculator.add(userInput, nextInput);
119
-        return answer;
120
-    }*/
121
-
122
-}

+ 0
- 24
MainApplication.java 查看文件

@@ -1,24 +0,0 @@
1
- 
2
-
3
-/**
4
- * Created by leon on 2/9/18.
5
- */
6
-public class MainApplication {
7
-    
8
-    public static void main(String[] args) {
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.");
16
-
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);
20
-    }
21
-}
22
-    
23
-    
24
-}

+ 136
- 0
MathMethods.java 查看文件

@@ -0,0 +1,136 @@
1
+import java.util.*;
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 MathMethods
9
+
10
+{
11
+
12
+    public static Double add(double n1, double n2){
13
+        return (n1 + n2);
14
+    }
15
+
16
+    public static Double subtract(double n1, double n2){
17
+        return (n1 - n2);
18
+    }
19
+
20
+    public static Double multiply(double n1, double n2){
21
+        return (n1 * n2);
22
+    }
23
+
24
+    public static Double divide(double n1, double n2){
25
+        return (n1 / n2);
26
+    }
27
+
28
+    public static Double exponent(double n1, double n2){
29
+        return (Math.pow(n1, n2));
30
+    }
31
+
32
+    public static Double cosine(double n1){
33
+        return (Math.cos(n1));
34
+    }
35
+
36
+    public static Double invCosine(double n1){
37
+        return (Math.acos(n1));
38
+    }
39
+
40
+    public static double squareRoot(double n1) {
41
+        return (Math.sqrt(n1));
42
+
43
+    }
44
+
45
+    public static double invert(double n1) {
46
+        return (n1*-1);
47
+
48
+    }
49
+
50
+    public static double sin(double n1){
51
+        return (Math.sin(n1));
52
+
53
+    }
54
+
55
+    public static double iSin(double n1){
56
+        return (Math.asin(n1));
57
+
58
+    }
59
+
60
+    public static double degToRad(double n1) {
61
+        return (Math.toRadians(n1));
62
+
63
+    }
64
+
65
+    public static double radToDeg(double n1) {
66
+        return (Math.toDegrees(n1));
67
+
68
+    }
69
+
70
+    public static double square(double n1) {
71
+        return Math.pow(n1, 2);
72
+
73
+    }
74
+
75
+    public static double inverse(double n1) {
76
+        return n1;
77
+    }
78
+
79
+    public static double tangent(double n1) {
80
+        return Math.tan(n1);
81
+    }
82
+
83
+    public static double itan(double n1) {
84
+        return Math.atan(n1);
85
+    }
86
+
87
+    public static void printAns(String printAnswer){
88
+        System.out.println("ANSWER: " + printAnswer);
89
+    }
90
+
91
+}
92
+
93
+    
94
+    
95
+// public static double squareRoot(double n1) {
96
+// double result = Math.sqrt(x);
97
+// return result;
98
+// }
99
+
100
+// public static double invert(double n1) {
101
+// double result = x*-1;
102
+// return result;
103
+// }
104
+
105
+// public static double sin(double n1){
106
+// double result = Math.sin(x);
107
+// return result;
108
+// }
109
+
110
+// public static double iSin(double n1){
111
+// double result = Math.asin(x);
112
+// return result;
113
+// }
114
+
115
+// public static double degToRad(double n1) {
116
+// double result = Math.toRadians(x);
117
+// return result;
118
+// }
119
+
120
+// public static double radToDeg(double n1) {
121
+// double result = Math.toDegrees(x);
122
+// return result;
123
+// }
124
+
125
+// public static Double exponent(double n1, double n2){
126
+// return (Math.pow(n1, n2));
127
+// }
128
+
129
+// public static Double cosine(double n1){
130
+// return (Math.cos(n1));
131
+// }
132
+
133
+// public static Double invCosine(double n1){
134
+// return (Math.acos(n1));
135
+// }
136
+

+ 0
- 20
Memory.java 查看文件

@@ -1,20 +0,0 @@
1
-
2
-/**
3
- * Write a description of class Memory here.
4
- *
5
- * @author (your name)
6
- * @version (a version number or a date)
7
- */
8
-public class Memory
9
-{
10
-    // instance variables - replace the example below with your ow
11
-
12
-
13
-
14
-    public void memorySet(double x)
15
-    {
16
-        mem = x;
17
-    }
18
-    
19
-    public void memoryRecall(double x)
20
-}

+ 7
- 0
package.bluej 查看文件

@@ -5,10 +5,17 @@ dependency1.type=UsesDependency
5 5
 dependency2.from=MainApplication
6 6
 dependency2.to=Console
7 7
 dependency2.type=UsesDependency
8
+<<<<<<< HEAD
8 9
 editor.fx.0.height=765
9 10
 editor.fx.0.width=863
10 11
 editor.fx.0.x=601
11 12
 editor.fx.0.y=-945
13
+=======
14
+editor.fx.0.height=1020
15
+editor.fx.0.width=1247
16
+editor.fx.0.x=1891
17
+editor.fx.0.y=66
18
+>>>>>>> 9d9574817b6e6ff98ab240c0e423add0236a0300
12 19
 objectbench.height=80
13 20
 objectbench.width=595
14 21
 package.divider.horizontal=0.5993322203672788