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