|
@@ -1,8 +1,8 @@
|
|
1
|
+
|
1
|
2
|
import java.util.*;
|
2
|
3
|
|
3
|
4
|
public class Calculator{
|
4
|
5
|
|
5
|
|
-
|
6
|
6
|
/**
|
7
|
7
|
* Write a description of class Calculator here.
|
8
|
8
|
*
|
|
@@ -19,12 +19,15 @@ public class Calculator{
|
19
|
19
|
double result = 0; //The user's answer
|
20
|
20
|
boolean off = false; //The user typed quit, becomes true
|
21
|
21
|
|
|
22
|
+ double memory;
|
|
23
|
+
|
22
|
24
|
boolean error = false;
|
23
|
25
|
MathMethods calc = new MathMethods();
|
24
|
26
|
|
25
|
27
|
while (off == false){
|
26
|
28
|
Scanner input1 = new Scanner(System.in);
|
27
|
29
|
System.out.println("What do you want to do?");
|
|
30
|
+
|
28
|
31
|
toDo = input1.next();
|
29
|
32
|
|
30
|
33
|
if (toDo.equalsIgnoreCase("quit")){
|
|
@@ -34,7 +37,7 @@ public class Calculator{
|
34
|
37
|
} else {
|
35
|
38
|
switch (toDo) {
|
36
|
39
|
//System.out.println("toDO is: " + toDo);
|
37
|
|
-
|
|
40
|
+
|
38
|
41
|
case "add":
|
39
|
42
|
case "subtract":
|
40
|
43
|
case "multiply":
|
|
@@ -52,181 +55,182 @@ public class Calculator{
|
52
|
55
|
case "tangent" :
|
53
|
56
|
case "inverse tangent" :
|
54
|
57
|
case "inverse" :
|
55
|
|
-
|
|
58
|
+
|
56
|
59
|
System.out.println(toDo + " -- Enter first number:");
|
57
|
60
|
num1 = input1.nextDouble();
|
58
|
61
|
//System.out.println("num1 is: " + num1);
|
59
|
|
- }
|
60
|
|
- switch (toDo){
|
61
|
|
- // case "previous":
|
62
|
|
- // num1 = lastAns;
|
63
|
|
- // System.out.println(toDo + " -- Enter second number:");
|
64
|
|
- // num2 = input1.nextDouble();
|
65
|
|
- //break;
|
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
|
|
-
|
79
|
|
-
|
80
|
|
- case "add":
|
81
|
|
- result = calc.add(num1, num2);
|
82
|
|
- lastAns = result;
|
83
|
|
- calc.printAns(String.valueOf(result));
|
84
|
|
- break;
|
85
|
|
-
|
86
|
|
- case "subtract":
|
87
|
|
- result = calc.subtract(num1, num2);
|
88
|
|
- lastAns = result;
|
89
|
|
- calc.printAns(String.valueOf(result));
|
90
|
|
- break;
|
91
|
|
-
|
92
|
|
- case "multiply":
|
93
|
|
- result = calc.multiply(num1, num2);
|
94
|
|
- lastAns = result;
|
95
|
|
- calc.printAns(String.valueOf(result));
|
96
|
|
- break;
|
97
|
|
-
|
98
|
|
- case "divide":
|
99
|
|
- result = calc.divide(num1, num2);
|
100
|
|
- lastAns = result;
|
101
|
|
- calc.printAns(String.valueOf(result));
|
102
|
|
- break;
|
103
|
|
-
|
104
|
|
- case "exponent":
|
105
|
|
- result = calc.exponent(num1, num2);
|
106
|
|
- lastAns = result;
|
107
|
|
- calc.printAns(String.valueOf(result));
|
108
|
|
- break;
|
109
|
|
-
|
110
|
|
- case "cosine":
|
111
|
|
- result = calc.cosine(num1);
|
112
|
|
- lastAns = result;
|
113
|
|
- calc.printAns(String.valueOf(result));
|
114
|
|
- break;
|
115
|
|
-
|
116
|
|
- case "inverse cosine":
|
117
|
|
- result = calc.invCosine(num1);
|
118
|
|
- lastAns = result;
|
119
|
|
- calc.printAns(String.valueOf(result));
|
120
|
|
- break;
|
121
|
|
-
|
122
|
|
- case "square root":
|
123
|
|
- result = calc.squareRoot(num1);
|
124
|
|
- lastAns = result;
|
125
|
|
- calc.printAns(String.valueOf(result));
|
126
|
|
- break;
|
127
|
|
-
|
128
|
|
- case "invert":
|
129
|
|
- result = calc.invert(num1);
|
130
|
|
- lastAns = result;
|
131
|
|
- calc.printAns(String.valueOf(result));
|
132
|
|
- break;
|
133
|
|
-
|
134
|
|
- case "sine":
|
135
|
|
- result = calc.sin(num1);
|
136
|
|
- lastAns = result;
|
137
|
|
- calc.printAns(String.valueOf(result));
|
138
|
|
- break;
|
139
|
|
-
|
140
|
|
- case "inverse sine":
|
141
|
|
- result = calc.iSin(num1);
|
142
|
|
- lastAns = result;
|
143
|
|
- calc.printAns(String.valueOf(result));
|
144
|
|
- break;
|
145
|
|
-
|
146
|
|
- case "degree to radian":
|
147
|
|
- result = calc.degToRad(num1);
|
148
|
|
- lastAns = result;
|
149
|
|
- calc.printAns(String.valueOf(result));
|
150
|
|
- break;
|
151
|
|
-
|
152
|
|
- case "radian to degree":
|
153
|
|
- result = calc.radToDeg(num1);
|
154
|
|
- lastAns = result;
|
155
|
|
- calc.printAns(String.valueOf(result));
|
156
|
|
- break;
|
157
|
|
-
|
158
|
|
- case "fortune cookie":
|
159
|
|
- calc.printAns(calc.fortuneCookie());
|
160
|
|
- break;
|
161
|
|
-
|
162
|
|
- case "display mode decimal":
|
163
|
|
- calc.printAns(String.valueOf(lastAns));
|
164
|
|
- break;
|
165
|
|
-
|
166
|
|
- case "display mode hexadecimal":
|
167
|
|
- calc.printAns(calc.switchDisplayMode("hexadecimal"));
|
168
|
|
-
|
169
|
|
- case "display mode octal":
|
170
|
|
- calc.printAns(calc.switchDisplayMode("octal"));
|
171
|
|
- break;
|
172
|
|
-
|
173
|
|
- case "display mode binary":
|
174
|
|
- calc.printAns(calc.switchDisplayMode("binary"));
|
175
|
|
- break;
|
176
|
|
-
|
177
|
|
- case "display mode" :
|
178
|
|
- calc.printAns(calc.switchDisplayMode());
|
179
|
|
- break;
|
180
|
|
-
|
181
|
|
- case "random number":
|
182
|
|
- result = calc.randomNumber();
|
183
|
|
- lastAns = result;
|
184
|
|
- calc.printAns(String.valueOf(result));
|
185
|
|
- break;
|
186
|
|
-
|
187
|
|
- case "square":
|
188
|
|
- result = calc.square(num1);
|
189
|
|
- lastAns = result;
|
190
|
|
- calc.printAns(String.valueOf(result));
|
191
|
|
- break;
|
192
|
|
-
|
193
|
|
- case "inverse":
|
194
|
|
- result = calc.inverse(num1);
|
195
|
|
- lastAns = result;
|
196
|
|
- calc.printAns(String.valueOf(result));
|
197
|
|
- break;
|
198
|
|
-
|
199
|
|
- case "tangent":
|
200
|
|
- result = calc.tangent(num1);
|
201
|
|
- lastAns = result;
|
202
|
|
- calc.printAns(String.valueOf(result));
|
203
|
|
- break;
|
204
|
|
-
|
205
|
|
- case "inverse tangent":
|
206
|
|
- result = calc.iTan(num1);
|
207
|
|
- lastAns = result;
|
208
|
|
- calc.printAns(String.valueOf(result));
|
209
|
|
- break;
|
210
|
|
-
|
211
|
|
- case "MR" :
|
212
|
|
- result = Memory.memRead();
|
213
|
|
- Memory.printAns(String.valueOf(result));
|
214
|
|
- break;
|
215
|
|
-
|
216
|
|
- case "M+" :
|
217
|
|
- result = Memory.memUpdate(lastAns);
|
218
|
|
- Memory.printAns(String.valueOf(result));
|
219
|
|
- break;
|
220
|
|
-
|
221
|
|
- case "MC" :
|
222
|
|
- result = Memory.memClear();
|
223
|
|
- Memory.printAns(String.valueOf(result));
|
224
|
|
- break;
|
225
|
62
|
|
|
63
|
+ switch (toDo){
|
|
64
|
+ case "add":
|
|
65
|
+ case "subtract":
|
|
66
|
+ case "multiply":
|
|
67
|
+ case "divide":
|
|
68
|
+ case "exponent":
|
|
69
|
+ System.out.println(toDo + " -- Enter second number:");
|
|
70
|
+ num2 = input1.nextDouble();
|
|
71
|
+ //System.out.println("num2 is: " + num2);
|
|
72
|
+
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ switch (toDo) {
|
|
76
|
+
|
|
77
|
+ case "add":
|
|
78
|
+ result = calc.add(num1, num2);
|
|
79
|
+ lastAns = result;
|
|
80
|
+ calc.printAns(String.valueOf(result));
|
|
81
|
+ break;
|
|
82
|
+
|
|
83
|
+ case "subtract":
|
|
84
|
+ result = calc.subtract(num1, num2);
|
|
85
|
+ lastAns = result;
|
|
86
|
+ calc.printAns(String.valueOf(result));
|
|
87
|
+ break;
|
|
88
|
+
|
|
89
|
+ case "multiply":
|
|
90
|
+ result = calc.multiply(num1, num2);
|
|
91
|
+ lastAns = result;
|
|
92
|
+ calc.printAns(String.valueOf(result));
|
|
93
|
+ break;
|
|
94
|
+
|
|
95
|
+ case "divide":
|
|
96
|
+ result = calc.divide(num1, num2);
|
|
97
|
+ lastAns = result;
|
|
98
|
+ calc.printAns(String.valueOf(result));
|
|
99
|
+ break;
|
|
100
|
+
|
|
101
|
+ case "exponent":
|
|
102
|
+ result = calc.exponent(num1, num2);
|
|
103
|
+ lastAns = result;
|
|
104
|
+ calc.printAns(String.valueOf(result));
|
|
105
|
+ break;
|
|
106
|
+
|
|
107
|
+ case "cosine":
|
|
108
|
+ result = calc.cosine(num1);
|
|
109
|
+ lastAns = result;
|
|
110
|
+ calc.printAns(String.valueOf(result));
|
|
111
|
+ break;
|
|
112
|
+
|
|
113
|
+ case "inverse cosine":
|
|
114
|
+ result = calc.invCosine(num1);
|
|
115
|
+ lastAns = result;
|
|
116
|
+ calc.printAns(String.valueOf(result));
|
|
117
|
+ break;
|
|
118
|
+
|
|
119
|
+ case "square root":
|
|
120
|
+ result = calc.squareRoot(num1);
|
|
121
|
+ lastAns = result;
|
|
122
|
+ calc.printAns(String.valueOf(result));
|
|
123
|
+ break;
|
|
124
|
+
|
|
125
|
+ case "invert":
|
|
126
|
+ result = calc.invert(num1);
|
|
127
|
+ lastAns = result;
|
|
128
|
+ calc.printAns(String.valueOf(result));
|
|
129
|
+ break;
|
|
130
|
+
|
|
131
|
+ case "sine":
|
|
132
|
+ result = calc.sin(num1);
|
|
133
|
+ lastAns = result;
|
|
134
|
+ calc.printAns(String.valueOf(result));
|
|
135
|
+ break;
|
|
136
|
+
|
|
137
|
+ case "inverse sine":
|
|
138
|
+ result = calc.iSin(num1);
|
|
139
|
+ lastAns = result;
|
|
140
|
+ calc.printAns(String.valueOf(result));
|
|
141
|
+ break;
|
|
142
|
+
|
|
143
|
+ case "degree to radian":
|
|
144
|
+ result = calc.degToRad(num1);
|
|
145
|
+ lastAns = result;
|
|
146
|
+ calc.printAns(String.valueOf(result));
|
|
147
|
+ break;
|
|
148
|
+
|
|
149
|
+ case "radian to degree":
|
|
150
|
+ result = calc.radToDeg(num1);
|
|
151
|
+ lastAns = result;
|
|
152
|
+ calc.printAns(String.valueOf(result));
|
|
153
|
+ break;
|
|
154
|
+
|
|
155
|
+ case "fortune cookie":
|
|
156
|
+ calc.printAns(calc.fortuneCookie());
|
|
157
|
+ break;
|
|
158
|
+
|
|
159
|
+ case "display mode decimal":
|
|
160
|
+ calc.printAns(String.valueOf(lastAns));
|
|
161
|
+ break;
|
|
162
|
+
|
|
163
|
+ case "display mode hexadecimal":
|
|
164
|
+ calc.printAns(calc.switchDisplayMode("hexadecimal"));
|
|
165
|
+
|
|
166
|
+ case "display mode octal":
|
|
167
|
+ calc.printAns(calc.switchDisplayMode("octal"));
|
|
168
|
+ break;
|
|
169
|
+
|
|
170
|
+ case "display mode binary":
|
|
171
|
+ calc.printAns(calc.switchDisplayMode("binary"));
|
|
172
|
+ break;
|
|
173
|
+
|
|
174
|
+ case "display mode" :
|
|
175
|
+ calc.printAns(calc.switchDisplayMode());
|
|
176
|
+ break;
|
|
177
|
+
|
|
178
|
+ case "random number":
|
|
179
|
+ result = calc.randomNumber();
|
|
180
|
+ lastAns = result;
|
|
181
|
+ calc.printAns(String.valueOf(result));
|
|
182
|
+ break;
|
|
183
|
+
|
|
184
|
+ case "square":
|
|
185
|
+ result = calc.square(num1);
|
|
186
|
+ lastAns = result;
|
|
187
|
+ calc.printAns(String.valueOf(result));
|
|
188
|
+ break;
|
|
189
|
+
|
|
190
|
+ case "inverse":
|
|
191
|
+ result = calc.inverse(num1);
|
|
192
|
+ lastAns = result;
|
|
193
|
+ calc.printAns(String.valueOf(result));
|
|
194
|
+ break;
|
|
195
|
+
|
|
196
|
+ case "tangent":
|
|
197
|
+ result = calc.tangent(num1);
|
|
198
|
+ lastAns = result;
|
|
199
|
+ calc.printAns(String.valueOf(result));
|
|
200
|
+ break;
|
|
201
|
+
|
|
202
|
+ case "inverse tangent":
|
|
203
|
+ result = calc.iTan(num1);
|
|
204
|
+ lastAns = result;
|
|
205
|
+ calc.printAns(String.valueOf(result));
|
|
206
|
+ break;
|
|
207
|
+
|
|
208
|
+ case "MR" :
|
|
209
|
+ result = Memory.memRead();
|
|
210
|
+ Memory.printAns(String.valueOf(result));
|
|
211
|
+ break;
|
|
212
|
+
|
|
213
|
+ case "M+" :
|
|
214
|
+ result = Memory.memUpdate(lastAns);
|
|
215
|
+ Memory.printAns(String.valueOf(result));
|
|
216
|
+ break;
|
|
217
|
+
|
|
218
|
+ case "MC" :
|
|
219
|
+ result = Memory.memClear();
|
|
220
|
+ Memory.printAns(String.valueOf(result));
|
|
221
|
+ break;
|
|
222
|
+
|
|
223
|
+ case "roundTo":
|
|
224
|
+ memory = result;
|
|
225
|
+ calc.roundTo(memory, (int)num1);
|
|
226
|
+ break;
|
|
227
|
+
|
|
228
|
+ }
|
226
|
229
|
}
|
227
|
230
|
}
|
228
|
231
|
}
|
229
|
|
- }
|
230
|
232
|
|
|
233
|
+ }
|
231
|
234
|
}
|
232
|
235
|
|
|
236
|
+
|