|
@@ -8,7 +8,7 @@ import java.util.*;
|
8
|
8
|
*/
|
9
|
9
|
|
10
|
10
|
class Calculator{
|
11
|
|
-
|
|
11
|
+
|
12
|
12
|
public static void main(String args[]){
|
13
|
13
|
|
14
|
14
|
String toDo; //what the user wants to do
|
|
@@ -23,232 +23,255 @@ class Calculator{
|
23
|
23
|
while (off == false){
|
24
|
24
|
Scanner input1 = new Scanner(System.in);
|
25
|
25
|
System.out.println("What do you want to do?");
|
26
|
|
- toDo = input1.nextLine();
|
|
26
|
+ toDo = input1.next();
|
27
|
27
|
|
28
|
|
- if (toDo.equals("quit")){
|
|
28
|
+ if (toDo.equalsIgnoreCase("quit")){
|
29
|
29
|
off = true;
|
30
|
30
|
System.out.println("Goodbye!!");
|
31
|
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));
|
|
32
|
+ } else if(toDo.equalsIgnoreCase("memory")){
|
|
33
|
+ System.out.println("Read, Change or Clear?");
|
|
34
|
+ String mem = input1.next();
|
|
35
|
+ switch(mem) {
|
|
36
|
+ case "Read" :
|
|
37
|
+ result = Memory.memRead();
|
|
38
|
+ Memory.printAns(String.valueOf(result));
|
99
|
39
|
break;
|
100
|
40
|
|
101
|
|
- case "invert":
|
102
|
|
- result = calc.invert(num1);
|
103
|
|
- memory = result;
|
104
|
|
- calc.printAns(String.valueOf(result));
|
|
41
|
+ case "Change" :
|
|
42
|
+ System.out.println("Enter number:");
|
|
43
|
+ num1 = input1.nextDouble();
|
|
44
|
+ result = Memory.memUpdate(num1);
|
|
45
|
+ Memory.printAns(String.valueOf(result));
|
105
|
46
|
break;
|
106
|
47
|
|
107
|
|
- case "sine":
|
108
|
|
- result = calc.sin(num1);
|
109
|
|
- memory = result;
|
110
|
|
- calc.printAns(String.valueOf(result));
|
|
48
|
+ case "Clear" :
|
|
49
|
+ result = Memory.memClear();
|
|
50
|
+ Memory.printAns(String.valueOf(result));
|
111
|
51
|
break;
|
112
|
52
|
|
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
|
|
- case "square":
|
132
|
|
- result = calc.square(num1);
|
133
|
|
- memory = result;
|
134
|
|
- calc.printAns(String.valueOf(result));
|
135
|
|
- break;
|
136
|
|
-
|
137
|
|
- case "inverse":
|
138
|
|
- result = calc.inverse(num1);
|
139
|
|
- memory = result;
|
140
|
|
- calc.printAns(String.format("1/%.0f", result));
|
141
|
|
- break;
|
142
|
|
-
|
143
|
|
- case "tangent":
|
144
|
|
- result = calc.tangent(num1);
|
145
|
|
- memory = result;
|
146
|
|
- calc.printAns(String.valueOf(result));
|
147
|
|
- break;
|
148
|
|
-
|
149
|
|
- case "inverse tangent":
|
150
|
|
- result = calc.iTan(num1);
|
151
|
|
- memory = result;
|
152
|
|
- calc.printAns(String.valueOf(result));
|
153
|
|
- break;
|
154
|
|
-
|
155
|
|
-
|
156
|
53
|
}
|
157
|
54
|
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+ else {
|
|
58
|
+
|
|
59
|
+ //System.out.println("toDO is: " + toDo);
|
|
60
|
+
|
|
61
|
+ System.out.println(toDo + " -- Enter first number:");
|
|
62
|
+ num1 = input1.nextDouble();
|
|
63
|
+ //System.out.println("num1 is: " + num1);
|
|
64
|
+
|
|
65
|
+ switch (toDo){
|
|
66
|
+ case "add":
|
|
67
|
+ case "subtract":
|
|
68
|
+ case "multiply":
|
|
69
|
+ case "divide":
|
|
70
|
+ case "exponent":
|
|
71
|
+ System.out.println(toDo + " -- Enter second number:");
|
|
72
|
+ num2 = input1.nextDouble();
|
|
73
|
+ //System.out.println("num2 is: " + num2);
|
|
74
|
+
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ switch (toDo) {
|
|
78
|
+ case "add":
|
|
79
|
+ result = calc.add(num1, num2);
|
|
80
|
+ memory = result;
|
|
81
|
+ calc.printAns(String.valueOf(result));
|
|
82
|
+ break;
|
|
83
|
+
|
|
84
|
+ case "subtract":
|
|
85
|
+ result = calc.subtract(num1, num2);
|
|
86
|
+ memory = result;
|
|
87
|
+ calc.printAns(String.valueOf(result));
|
|
88
|
+ break;
|
|
89
|
+
|
|
90
|
+ case "multiply":
|
|
91
|
+ result = calc.multiply(num1, num2);
|
|
92
|
+ memory = result;
|
|
93
|
+ calc.printAns(String.valueOf(result));
|
|
94
|
+ break;
|
|
95
|
+
|
|
96
|
+ case "divide":
|
|
97
|
+ result = calc.divide(num1, num2);
|
|
98
|
+ memory = result;
|
|
99
|
+ calc.printAns(String.valueOf(result));
|
|
100
|
+ break;
|
|
101
|
+
|
|
102
|
+ case "exponent":
|
|
103
|
+ result = calc.exponent(num1, num2);
|
|
104
|
+ memory = result;
|
|
105
|
+ calc.printAns(String.valueOf(result));
|
|
106
|
+ break;
|
|
107
|
+
|
|
108
|
+ case "cosine":
|
|
109
|
+ result = calc.cosine(num1);
|
|
110
|
+ memory = result;
|
|
111
|
+ calc.printAns(String.valueOf(result));
|
|
112
|
+ break;
|
|
113
|
+
|
|
114
|
+ case "inverse cosine":
|
|
115
|
+ result = calc.invCosine(num1);
|
|
116
|
+ memory = result;
|
|
117
|
+ calc.printAns(String.valueOf(result));
|
|
118
|
+ break;
|
|
119
|
+
|
|
120
|
+ case "square root":
|
|
121
|
+ result = calc.squareRoot(num1);
|
|
122
|
+ memory = result;
|
|
123
|
+ calc.printAns(String.valueOf(result));
|
|
124
|
+ break;
|
|
125
|
+
|
|
126
|
+ case "invert":
|
|
127
|
+ result = calc.invert(num1);
|
|
128
|
+ memory = result;
|
|
129
|
+ calc.printAns(String.valueOf(result));
|
|
130
|
+ break;
|
|
131
|
+
|
|
132
|
+ case "sine":
|
|
133
|
+ result = calc.sin(num1);
|
|
134
|
+ memory = result;
|
|
135
|
+ calc.printAns(String.valueOf(result));
|
|
136
|
+ break;
|
|
137
|
+
|
|
138
|
+ case "inverse sine":
|
|
139
|
+ result = calc.iSin(num1);
|
|
140
|
+ memory = result;
|
|
141
|
+ calc.printAns(String.valueOf(result));
|
|
142
|
+ break;
|
|
143
|
+
|
|
144
|
+ case "degree to radian":
|
|
145
|
+ result = calc.degToRad(num1);
|
|
146
|
+ memory = result;
|
|
147
|
+ calc.printAns(String.valueOf(result));
|
|
148
|
+ break;
|
|
149
|
+
|
|
150
|
+ case "radian to degree":
|
|
151
|
+ result = calc.radToDeg(num1);
|
|
152
|
+ memory = result;
|
|
153
|
+ calc.printAns(String.valueOf(result));
|
|
154
|
+ break;
|
|
155
|
+
|
|
156
|
+ case "square":
|
|
157
|
+ result = calc.square(num1);
|
|
158
|
+ memory = result;
|
|
159
|
+ calc.printAns(String.valueOf(result));
|
|
160
|
+ break;
|
|
161
|
+
|
|
162
|
+ case "inverse":
|
|
163
|
+ result = calc.inverse(num1);
|
|
164
|
+ memory = result;
|
|
165
|
+ calc.printAns(String.valueOf(result));
|
|
166
|
+ break;
|
|
167
|
+
|
|
168
|
+ case "tangent":
|
|
169
|
+ result = calc.tangent(num1);
|
|
170
|
+ memory = result;
|
|
171
|
+ calc.printAns(String.valueOf(result));
|
|
172
|
+ break;
|
|
173
|
+
|
|
174
|
+ case "inverse tangent":
|
|
175
|
+ result = calc.iTan(num1);
|
|
176
|
+ memory = result;
|
|
177
|
+ calc.printAns(String.valueOf(result));
|
|
178
|
+ break;
|
|
179
|
+
|
|
180
|
+ }
|
158
|
181
|
}
|
159
|
182
|
}
|
160
|
183
|
}
|
|
184
|
+}
|
161
|
185
|
|
162
|
|
- // /**
|
163
|
|
- // * Created by leon on 2/9/18.
|
164
|
|
- // */
|
165
|
|
- // public class Console {
|
166
|
|
- // Calculator calc = new Calculator();
|
167
|
|
-
|
168
|
|
- // public static void print(String output, Object... args) {
|
169
|
|
- // System.out.printf(output, args);
|
170
|
|
- // }
|
171
|
|
-
|
172
|
|
- // public static void println(String output, Object... args) {
|
173
|
|
- // print(output + "\n", args);
|
174
|
|
- // }
|
175
|
|
-
|
176
|
|
- // public static String getMethod() {
|
177
|
|
- // Scanner scanner = new Scanner(System.in);
|
178
|
|
- // String userInput = scanner.nextLine();
|
179
|
|
- // return userInput;
|
180
|
|
- // }
|
181
|
|
-
|
182
|
|
- // public static String switchStatment() {
|
183
|
|
- // Scanner scanner = new Scanner(System.in);
|
184
|
|
- // String ultimateAnswer = "";
|
185
|
|
- // Boolean turnOff = false;
|
186
|
|
-
|
187
|
|
- // switch (getMethod()) {
|
188
|
|
- // case "add" :
|
189
|
|
- // System.out.println("Enter your numbers:");
|
190
|
|
- // double userInput = scanner.nextDouble();
|
191
|
|
- // double nextInput = scanner.nextDouble();
|
192
|
|
-
|
193
|
|
- // double answer = Calculator.add(userInput, nextInput);
|
194
|
|
- // ultimateAnswer = Double.toString(answer);
|
195
|
|
- // return Double.toString(answer);
|
196
|
|
-
|
197
|
|
- // case "subtract" :
|
198
|
|
- // System.out.println("Enter your numbers:");
|
199
|
|
- // userInput = scanner.nextDouble();
|
200
|
|
- // nextInput = scanner.nextDouble();
|
201
|
|
-
|
202
|
|
- // answer = Calculator.subtract(userInput, nextInput);
|
203
|
|
- // ultimateAnswer = Double.toString(answer);
|
204
|
|
- // return ultimateAnswer;
|
205
|
|
-
|
206
|
|
- // case "multiply" :
|
207
|
|
- // System.out.println("Enter your numbers:");
|
208
|
|
- // userInput = scanner.nextDouble();
|
209
|
|
- // nextInput = scanner.nextDouble();
|
210
|
|
-
|
211
|
|
- // answer = Calculator.multiply(userInput, nextInput);
|
212
|
|
- // ultimateAnswer = Double.toString(answer);
|
213
|
|
- // return ultimateAnswer;
|
214
|
|
-
|
215
|
|
- // case "divide" :
|
216
|
|
- // System.out.println("Enter your numbers:");
|
217
|
|
- // userInput = scanner.nextDouble();
|
218
|
|
- // nextInput = scanner.nextDouble();
|
219
|
|
-
|
220
|
|
- // answer = Calculator.divide(userInput, nextInput);
|
221
|
|
- // ultimateAnswer = Double.toString(answer);
|
222
|
|
- // return ultimateAnswer;
|
223
|
|
-
|
224
|
|
- // case "Square Root" :
|
225
|
|
-
|
226
|
|
-
|
227
|
|
- // }
|
228
|
|
- // return ultimateAnswer;
|
229
|
|
- // }
|
230
|
|
-
|
231
|
|
- // /*public static String getStringInput(String prompt) {
|
232
|
|
- // Scanner scanner = new Scanner(System.in);
|
233
|
|
- // println(prompt);
|
234
|
|
- // String userInput = scanner.nextLine();
|
235
|
|
- // return userInput;
|
236
|
|
- // }
|
237
|
|
-
|
238
|
|
- // public static Integer getIntegerInput(String prompt) {
|
239
|
|
- // Scanner scanner = new Scanner(System.in);
|
240
|
|
- // println(prompt);
|
241
|
|
- // int userInput = scanner.nextInt();
|
242
|
|
- // return null;
|
243
|
|
- // }
|
244
|
|
-
|
245
|
|
- // public static Double getDoubleInput(String prompt) {
|
246
|
|
- // Scanner scanner = new Scanner(System.in);
|
247
|
|
- // println(prompt);
|
248
|
|
- // double userInput = scanner.nextDouble();
|
249
|
|
- // double nextInput = scanner.nextDouble();
|
250
|
|
- // double answer = Calculator.add(userInput, nextInput);
|
251
|
|
- // return answer;
|
252
|
|
- // }*/
|
253
|
|
-
|
254
|
|
- // }
|
|
186
|
+// /**
|
|
187
|
+// * Created by leon on 2/9/18.
|
|
188
|
+// */
|
|
189
|
+// public class Console {
|
|
190
|
+// Calculator calc = new Calculator();
|
|
191
|
+
|
|
192
|
+// public static void print(String output, Object... args) {
|
|
193
|
+// System.out.printf(output, args);
|
|
194
|
+// }
|
|
195
|
+
|
|
196
|
+// public static void println(String output, Object... args) {
|
|
197
|
+// print(output + "\n", args);
|
|
198
|
+// }
|
|
199
|
+
|
|
200
|
+// public static String getMethod() {
|
|
201
|
+// Scanner scanner = new Scanner(System.in);
|
|
202
|
+// String userInput = scanner.nextLine();
|
|
203
|
+// return userInput;
|
|
204
|
+// }
|
|
205
|
+
|
|
206
|
+// public static String switchStatment() {
|
|
207
|
+// Scanner scanner = new Scanner(System.in);
|
|
208
|
+// String ultimateAnswer = "";
|
|
209
|
+// Boolean turnOff = false;
|
|
210
|
+
|
|
211
|
+// switch (getMethod()) {
|
|
212
|
+// case "add" :
|
|
213
|
+// System.out.println("Enter your numbers:");
|
|
214
|
+// double userInput = scanner.nextDouble();
|
|
215
|
+// double nextInput = scanner.nextDouble();
|
|
216
|
+
|
|
217
|
+// double answer = Calculator.add(userInput, nextInput);
|
|
218
|
+// ultimateAnswer = Double.toString(answer);
|
|
219
|
+// return Double.toString(answer);
|
|
220
|
+
|
|
221
|
+// case "subtract" :
|
|
222
|
+// System.out.println("Enter your numbers:");
|
|
223
|
+// userInput = scanner.nextDouble();
|
|
224
|
+// nextInput = scanner.nextDouble();
|
|
225
|
+
|
|
226
|
+// answer = Calculator.subtract(userInput, nextInput);
|
|
227
|
+// ultimateAnswer = Double.toString(answer);
|
|
228
|
+// return ultimateAnswer;
|
|
229
|
+
|
|
230
|
+// case "multiply" :
|
|
231
|
+// System.out.println("Enter your numbers:");
|
|
232
|
+// userInput = scanner.nextDouble();
|
|
233
|
+// nextInput = scanner.nextDouble();
|
|
234
|
+
|
|
235
|
+// answer = Calculator.multiply(userInput, nextInput);
|
|
236
|
+// ultimateAnswer = Double.toString(answer);
|
|
237
|
+// return ultimateAnswer;
|
|
238
|
+
|
|
239
|
+// case "divide" :
|
|
240
|
+// System.out.println("Enter your numbers:");
|
|
241
|
+// userInput = scanner.nextDouble();
|
|
242
|
+// nextInput = scanner.nextDouble();
|
|
243
|
+
|
|
244
|
+// answer = Calculator.divide(userInput, nextInput);
|
|
245
|
+// ultimateAnswer = Double.toString(answer);
|
|
246
|
+// return ultimateAnswer;
|
|
247
|
+
|
|
248
|
+// case "Square Root" :
|
|
249
|
+
|
|
250
|
+// }
|
|
251
|
+// return ultimateAnswer;
|
|
252
|
+// }
|
|
253
|
+
|
|
254
|
+// /*public static String getStringInput(String prompt) {
|
|
255
|
+// Scanner scanner = new Scanner(System.in);
|
|
256
|
+// println(prompt);
|
|
257
|
+// String userInput = scanner.nextLine();
|
|
258
|
+// return userInput;
|
|
259
|
+// }
|
|
260
|
+
|
|
261
|
+// public static Integer getIntegerInput(String prompt) {
|
|
262
|
+// Scanner scanner = new Scanner(System.in);
|
|
263
|
+// println(prompt);
|
|
264
|
+// int userInput = scanner.nextInt();
|
|
265
|
+// return null;
|
|
266
|
+// }
|
|
267
|
+
|
|
268
|
+// public static Double getDoubleInput(String prompt) {
|
|
269
|
+// Scanner scanner = new Scanner(System.in);
|
|
270
|
+// println(prompt);
|
|
271
|
+// double userInput = scanner.nextDouble();
|
|
272
|
+// double nextInput = scanner.nextDouble();
|
|
273
|
+// double answer = Calculator.add(userInput, nextInput);
|
|
274
|
+// return answer;
|
|
275
|
+// }*/
|
|
276
|
+
|
|
277
|
+// }
|