|
@@ -6,9 +6,8 @@ import java.util.*;
|
6
|
6
|
* @author (your name)
|
7
|
7
|
* @version (a version number or a date)
|
8
|
8
|
*/
|
9
|
|
-
|
10
|
|
-class Calculator{
|
11
|
|
-
|
|
9
|
+public class Calculator{
|
|
10
|
+ public static double lastAns = 0;
|
12
|
11
|
public static void main(String args[]){
|
13
|
12
|
|
14
|
13
|
String toDo; //what the user wants to do
|
|
@@ -16,7 +15,6 @@ class Calculator{
|
16
|
15
|
double num2 = 0; //The user's second input
|
17
|
16
|
double result = 0; //The user's answer
|
18
|
17
|
boolean off = false; //The user typed quit, becomes true
|
19
|
|
- double memory;
|
20
|
18
|
boolean error = false;
|
21
|
19
|
MathMethods calc = new MathMethods();
|
22
|
20
|
|
|
@@ -31,148 +29,164 @@ class Calculator{
|
31
|
29
|
|
32
|
30
|
}
|
33
|
31
|
else {
|
34
|
|
-
|
35
|
|
- //System.out.println("toDO is: " + toDo);
|
36
|
|
-
|
37
|
|
- System.out.println(toDo + " -- Enter first number:");
|
38
|
|
- num1 = input1.nextDouble();
|
39
|
|
- //System.out.println("num1 is: " + num1);
|
40
|
|
-
|
41
|
|
- switch (toDo){
|
42
|
|
- case "add":
|
43
|
|
- case "subtract":
|
44
|
|
- case "multiply":
|
45
|
|
- case "divide":
|
46
|
|
- case "exponent":
|
47
|
|
- System.out.println(toDo + " -- Enter second number:");
|
48
|
|
- num2 = input1.nextDouble();
|
49
|
|
- //System.out.println("num2 is: " + num2);
|
50
|
|
-
|
51
|
|
- }
|
52
|
|
-
|
53
|
|
- switch (toDo) {
|
54
|
|
- case "add":
|
55
|
|
- result = calc.add(num1, num2);
|
56
|
|
- memory = result;
|
57
|
|
- calc.printAns(String.valueOf(result));
|
58
|
|
- break;
|
59
|
|
-
|
60
|
|
- case "subtract":
|
61
|
|
- result = calc.subtract(num1, num2);
|
62
|
|
- memory = result;
|
63
|
|
- calc.printAns(String.valueOf(result));
|
64
|
|
- break;
|
65
|
|
-
|
66
|
|
- case "multiply":
|
67
|
|
- result = calc.multiply(num1, num2);
|
68
|
|
- memory = result;
|
69
|
|
- calc.printAns(String.valueOf(result));
|
70
|
|
- break;
|
71
|
|
-
|
72
|
|
- case "divide":
|
73
|
|
- result = calc.divide(num1, num2);
|
74
|
|
- memory = result;
|
75
|
|
- calc.printAns(String.valueOf(result));
|
76
|
|
- break;
|
77
|
|
-
|
78
|
|
- case "exponent":
|
79
|
|
- result = calc.exponent(num1, num2);
|
80
|
|
- memory = result;
|
81
|
|
- calc.printAns(String.valueOf(result));
|
82
|
|
- break;
|
83
|
|
-
|
84
|
|
- case "cosine":
|
85
|
|
- result = calc.cosine(num1);
|
86
|
|
- memory = result;
|
87
|
|
- calc.printAns(String.valueOf(result));
|
88
|
|
- break;
|
89
|
|
-
|
90
|
|
- case "inverse cosine":
|
91
|
|
- result = calc.invCosine(num1);
|
92
|
|
- memory = result;
|
93
|
|
- calc.printAns(String.valueOf(result));
|
94
|
|
- break;
|
95
|
|
-
|
96
|
|
- case "square root":
|
97
|
|
- result = calc.squareRoot(num1);
|
98
|
|
- memory = result;
|
99
|
|
- calc.printAns(String.valueOf(result));
|
100
|
|
- break;
|
101
|
|
-
|
102
|
|
- case "invert":
|
103
|
|
- result = calc.invert(num1);
|
104
|
|
- memory = result;
|
105
|
|
- calc.printAns(String.valueOf(result));
|
106
|
|
- break;
|
107
|
|
-
|
108
|
|
- case "sine":
|
109
|
|
- result = calc.sin(num1);
|
110
|
|
- memory = result;
|
111
|
|
- calc.printAns(String.valueOf(result));
|
112
|
|
- break;
|
113
|
|
-
|
114
|
|
- case "inverse sine":
|
115
|
|
- result = calc.iSin(num1);
|
116
|
|
- memory = result;
|
117
|
|
- calc.printAns(String.valueOf(result));
|
118
|
|
- break;
|
119
|
|
-
|
120
|
|
- case "degree to radian":
|
121
|
|
- result = calc.degToRad(num1);
|
122
|
|
- memory = result;
|
123
|
|
- calc.printAns(String.valueOf(result));
|
124
|
|
- break;
|
125
|
|
-
|
126
|
|
- case "radian to degree":
|
127
|
|
- result = calc.radToDeg(num1);
|
128
|
|
- memory = result;
|
129
|
|
- calc.printAns(String.valueOf(result));
|
130
|
|
- break;
|
131
|
|
-
|
132
|
|
- case "square":
|
133
|
|
- result = calc.square(num1);
|
134
|
|
- memory = result;
|
135
|
|
- calc.printAns(String.valueOf(result));
|
136
|
|
- break;
|
137
|
|
-
|
138
|
|
- case "inverse":
|
139
|
|
- result = calc.inverse(num1);
|
140
|
|
- memory = result;
|
141
|
|
- calc.printAns(String.valueOf(result));
|
142
|
|
- break;
|
143
|
|
-
|
144
|
|
- case "tangent":
|
145
|
|
- result = calc.tangent(num1);
|
146
|
|
- memory = result;
|
147
|
|
- calc.printAns(String.valueOf(result));
|
148
|
|
- break;
|
149
|
|
-
|
150
|
|
- case "inverse tangent":
|
151
|
|
- result = calc.iTan(num1);
|
152
|
|
- memory = result;
|
153
|
|
- calc.printAns(String.valueOf(result));
|
154
|
|
- break;
|
155
|
|
-
|
156
|
|
- case "MR" :
|
157
|
|
- result = Memory.memRead();
|
158
|
|
- Memory.printAns(String.valueOf(result));
|
159
|
|
- break;
|
160
|
|
-
|
161
|
|
- case "M+" :
|
162
|
|
- System.out.println("Enter number:");
|
163
|
|
- num1 = input1.nextDouble();
|
164
|
|
- result = Memory.memUpdate(num1);
|
165
|
|
- Memory.printAns(String.valueOf(result));
|
166
|
|
- break;
|
167
|
|
-
|
168
|
|
- case "MC" :
|
169
|
|
- result = Memory.memClear();
|
170
|
|
- Memory.printAns(String.valueOf(result));
|
171
|
|
- break;
|
172
|
|
-
|
173
|
|
-
|
|
32
|
+ switch(toDo){
|
|
33
|
+ case "add":
|
|
34
|
+ case "subtract":
|
|
35
|
+ case "multiply":
|
|
36
|
+ case "divide":
|
|
37
|
+ case "exponent":
|
|
38
|
+ case "cosine" :
|
|
39
|
+ case "inverse cosine" :
|
|
40
|
+ case "square root" :
|
|
41
|
+ case "invert" :
|
|
42
|
+ case "sine" :
|
|
43
|
+ case "inverse sine" :
|
|
44
|
+ case "degree to radian" :
|
|
45
|
+ case "radian to degree" :
|
|
46
|
+ case "tangent" :
|
|
47
|
+ case "inverse tangent" :
|
|
48
|
+ case "square" :
|
|
49
|
+ case "M+" :
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+ //System.out.println("toDO is: " + toDo);
|
|
53
|
+ System.out.println(toDo + " -- Enter first number:");
|
|
54
|
+ num1 = input1.nextDouble();
|
|
55
|
+ //System.out.println("num1 is: " + num1);
|
|
56
|
+ }
|
|
57
|
+ switch (toDo){
|
|
58
|
+ case "add":
|
|
59
|
+ case "subtract":
|
|
60
|
+ case "multiply":
|
|
61
|
+ case "divide":
|
|
62
|
+ case "exponent":
|
|
63
|
+ System.out.println(toDo + " -- Enter second number:");
|
|
64
|
+ num2 = input1.nextDouble();
|
|
65
|
+ //System.out.println("num2 is: " + num2);
|
|
66
|
+
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ switch (toDo) {
|
|
70
|
+ case "add":
|
|
71
|
+ result = calc.add(num1, num2);
|
|
72
|
+ lastAns = result;
|
|
73
|
+ calc.printAns(String.valueOf(result));
|
|
74
|
+ break;
|
|
75
|
+
|
|
76
|
+ case "subtract":
|
|
77
|
+ result = calc.subtract(num1, num2);
|
|
78
|
+ lastAns = result;
|
|
79
|
+ calc.printAns(String.valueOf(result));
|
|
80
|
+ break;
|
|
81
|
+
|
|
82
|
+ case "multiply":
|
|
83
|
+ result = calc.multiply(num1, num2);
|
|
84
|
+ lastAns = result;
|
|
85
|
+ calc.printAns(String.valueOf(result));
|
|
86
|
+ break;
|
|
87
|
+
|
|
88
|
+ case "divide":
|
|
89
|
+ result = calc.divide(num1, num2);
|
|
90
|
+ lastAns = result;
|
|
91
|
+ calc.printAns(String.valueOf(result));
|
|
92
|
+ break;
|
|
93
|
+
|
|
94
|
+ case "exponent":
|
|
95
|
+ result = calc.exponent(num1, num2);
|
|
96
|
+ lastAns = result;
|
|
97
|
+ calc.printAns(String.valueOf(result));
|
|
98
|
+ break;
|
|
99
|
+
|
|
100
|
+ case "cosine":
|
|
101
|
+ result = calc.cosine(num1);
|
|
102
|
+ lastAns = result;
|
|
103
|
+ calc.printAns(String.valueOf(result));
|
|
104
|
+ break;
|
|
105
|
+
|
|
106
|
+ case "inverse cosine":
|
|
107
|
+ result = calc.invCosine(num1);
|
|
108
|
+ lastAns = result;
|
|
109
|
+ calc.printAns(String.valueOf(result));
|
|
110
|
+ break;
|
|
111
|
+
|
|
112
|
+ case "square root":
|
|
113
|
+ result = calc.squareRoot(num1);
|
|
114
|
+ lastAns = result;
|
|
115
|
+ calc.printAns(String.valueOf(result));
|
|
116
|
+ break;
|
|
117
|
+
|
|
118
|
+ case "invert":
|
|
119
|
+ result = calc.invert(num1);
|
|
120
|
+ lastAns = result;
|
|
121
|
+ calc.printAns(String.valueOf(result));
|
|
122
|
+ break;
|
|
123
|
+
|
|
124
|
+ case "sine":
|
|
125
|
+ result = calc.sin(num1);
|
|
126
|
+ lastAns = result;
|
|
127
|
+ calc.printAns(String.valueOf(result));
|
|
128
|
+ break;
|
|
129
|
+
|
|
130
|
+ case "inverse sine":
|
|
131
|
+ result = calc.iSin(num1);
|
|
132
|
+ lastAns = result;
|
|
133
|
+ calc.printAns(String.valueOf(result));
|
|
134
|
+ break;
|
|
135
|
+
|
|
136
|
+ case "degree to radian":
|
|
137
|
+ result = calc.degToRad(num1);
|
|
138
|
+ lastAns = result;
|
|
139
|
+ calc.printAns(String.valueOf(result));
|
|
140
|
+ break;
|
|
141
|
+
|
|
142
|
+ case "radian to degree":
|
|
143
|
+ result = calc.radToDeg(num1);
|
|
144
|
+ lastAns = result;
|
|
145
|
+ calc.printAns(String.valueOf(result));
|
|
146
|
+ break;
|
|
147
|
+
|
|
148
|
+ case "square":
|
|
149
|
+ result = calc.square(num1);
|
|
150
|
+ lastAns = result;
|
|
151
|
+ calc.printAns(String.valueOf(result));
|
|
152
|
+ break;
|
|
153
|
+
|
|
154
|
+ case "inverse":
|
|
155
|
+ result = calc.inverse(num1);
|
|
156
|
+ lastAns = result;
|
|
157
|
+ calc.printAns(String.valueOf(result));
|
|
158
|
+ break;
|
|
159
|
+
|
|
160
|
+ case "tangent":
|
|
161
|
+ result = calc.tangent(num1);
|
|
162
|
+ lastAns = result;
|
|
163
|
+ calc.printAns(String.valueOf(result));
|
|
164
|
+ break;
|
|
165
|
+
|
|
166
|
+ case "inverse tangent":
|
|
167
|
+ result = calc.iTan(num1);
|
|
168
|
+ lastAns = result;
|
|
169
|
+ calc.printAns(String.valueOf(result));
|
|
170
|
+ break;
|
|
171
|
+
|
|
172
|
+ case "MR" :
|
|
173
|
+ result = Memory.memRead();
|
|
174
|
+ Memory.printAns(String.valueOf(result));
|
|
175
|
+ break;
|
|
176
|
+
|
|
177
|
+ case "M+" :
|
|
178
|
+ System.out.println("Enter number:");
|
|
179
|
+ result = Memory.memUpdate(lastAns);
|
|
180
|
+ Memory.printAns(String.valueOf(result));
|
|
181
|
+ break;
|
|
182
|
+
|
|
183
|
+ case "MC" :
|
|
184
|
+ result = Memory.memClear();
|
|
185
|
+ Memory.printAns(String.valueOf(result));
|
|
186
|
+ break;
|
|
187
|
+
|
|
188
|
+ }
|
174
|
189
|
}
|
175
|
190
|
}
|
176
|
191
|
}
|
177
|
192
|
}
|
178
|
|
-}
|