|
@@ -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
|
+ // }
|