|
@@ -0,0 +1,243 @@
|
|
1
|
+import java.util.Scanner;
|
|
2
|
+import java.lang.*;
|
|
3
|
+
|
|
4
|
+public class Operations{
|
|
5
|
+
|
|
6
|
+ Scanner stdin = new Scanner(System.in);
|
|
7
|
+
|
|
8
|
+public double chooseOp(double num){
|
|
9
|
+
|
|
10
|
+ boolean isValid = false;
|
|
11
|
+ int choice;
|
|
12
|
+ double result = 0;
|
|
13
|
+
|
|
14
|
+ while(!isValid){
|
|
15
|
+ System.out.println("\n-------------------------------------------------");
|
|
16
|
+ System.out.println(num);
|
|
17
|
+ System.out.println("-------------------------------------------------");
|
|
18
|
+ System.out.println("Select type of operation to perform (enter # 1-3)");
|
|
19
|
+ System.out.println("(1) Core Functions");
|
|
20
|
+ System.out.println("(2) Trigonometry");
|
|
21
|
+ System.out.println("(3) Advanced Functions");
|
|
22
|
+ System.out.println("-------------------------------------------------");
|
|
23
|
+ System.out.print("> ");
|
|
24
|
+ choice = stdin.nextInt();
|
|
25
|
+
|
|
26
|
+ switch(choice){
|
|
27
|
+ case 1: result = coreFunc(num);
|
|
28
|
+ isValid = true;
|
|
29
|
+ break;
|
|
30
|
+ case 2: result = trigFunc(num);
|
|
31
|
+ isValid = true;
|
|
32
|
+ break;
|
|
33
|
+ case 3: result = advFunc(num);
|
|
34
|
+ isValid = true;
|
|
35
|
+ break;
|
|
36
|
+ default:
|
|
37
|
+ break;
|
|
38
|
+ }
|
|
39
|
+ }
|
|
40
|
+ return result;
|
|
41
|
+}
|
|
42
|
+
|
|
43
|
+public double coreFunc(double num){ //take displayNum as input parameter
|
|
44
|
+
|
|
45
|
+ boolean isValid = false;
|
|
46
|
+ int choice;
|
|
47
|
+ double entNum, result = 0;
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+ while(!isValid){
|
|
51
|
+ System.out.println("\n-------------------------------------------------");
|
|
52
|
+ System.out.println(num);
|
|
53
|
+ System.out.println("-------------------------------------------------");
|
|
54
|
+ System.out.println("Select core function to perform (enter # 1-9)");
|
|
55
|
+ System.out.println("(1) Add");
|
|
56
|
+ System.out.println("(2) Subtract");
|
|
57
|
+ System.out.println("(3) Multiply");
|
|
58
|
+ System.out.println("(4) Divide");
|
|
59
|
+ System.out.println("(5) Square");
|
|
60
|
+ System.out.println("(6) Square Root");
|
|
61
|
+ System.out.println("(7) Exponentiation");
|
|
62
|
+ System.out.println("(8) Inverse (1/x)");
|
|
63
|
+ System.out.println("(9) Change sign (+/-)");
|
|
64
|
+ System.out.println("---------------------------------------------");
|
|
65
|
+ System.out.print("> ");
|
|
66
|
+ choice = stdin.nextInt();
|
|
67
|
+
|
|
68
|
+ switch(choice){
|
|
69
|
+ case 1: System.out.println("Enter number to add to " + num + ": ");
|
|
70
|
+ System.out.print("> ");
|
|
71
|
+ entNum = stdin.nextDouble();
|
|
72
|
+ result = num + entNum;
|
|
73
|
+ isValid = true;
|
|
74
|
+ break;
|
|
75
|
+ case 2: System.out.println("Enter number to subtract from " + num + ": ");
|
|
76
|
+ System.out.print("> ");
|
|
77
|
+ entNum = stdin.nextDouble();
|
|
78
|
+ result = num - entNum;
|
|
79
|
+ isValid = true;
|
|
80
|
+ break;
|
|
81
|
+ case 3: System.out.println("Enter number to multiply to " + num + ": ");
|
|
82
|
+ System.out.print("> ");
|
|
83
|
+ entNum = stdin.nextDouble();
|
|
84
|
+ result = num * entNum;
|
|
85
|
+ isValid = true;
|
|
86
|
+ break;
|
|
87
|
+ case 4: System.out.println("Enter number to divide from " + num + ": ");
|
|
88
|
+ System.out.print("> ");
|
|
89
|
+ entNum = stdin.nextDouble();
|
|
90
|
+ result = num / entNum; //use Double.isNaN(x)
|
|
91
|
+ isValid = true;
|
|
92
|
+ break;
|
|
93
|
+ case 5: System.out.println("Squaring number on display (" + num + "^2) ");
|
|
94
|
+ System.out.print("> ");
|
|
95
|
+ result = num * num;
|
|
96
|
+ isValid = true;
|
|
97
|
+ break;
|
|
98
|
+ case 6: System.out.println("Taking square root of " + num);//cant take negative #
|
|
99
|
+ System.out.print("> ");
|
|
100
|
+ result = Math.sqrt(num);
|
|
101
|
+ isValid = true;
|
|
102
|
+ break;
|
|
103
|
+ case 7: System.out.println("Enter exponent to raise " + num + " to (" + num + "^n)");
|
|
104
|
+ System.out.print("> ");
|
|
105
|
+ entNum = stdin.nextDouble();
|
|
106
|
+ result = Math.pow(num, entNum);
|
|
107
|
+ isValid = true;
|
|
108
|
+ break;
|
|
109
|
+ case 8: System.out.println("Calculating inverse of " + num + " (1/" + num + ")");
|
|
110
|
+ System.out.print("> ");
|
|
111
|
+ result = 1/num;
|
|
112
|
+ isValid = true;
|
|
113
|
+ break;
|
|
114
|
+ case 9: System.out.println("Calculating negation of " + num + " (n -> -n , -n -> n)");
|
|
115
|
+ System.out.print("> ");
|
|
116
|
+ result = num * -1;
|
|
117
|
+ isValid = true;
|
|
118
|
+ break;
|
|
119
|
+ default:
|
|
120
|
+ break;
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ return result;
|
|
124
|
+}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+public double trigFunc(double num){ //take displayNum as input parameter
|
|
128
|
+
|
|
129
|
+ boolean isValid = false;
|
|
130
|
+ int choice;
|
|
131
|
+ double result = -0;
|
|
132
|
+
|
|
133
|
+ while(!isValid){
|
|
134
|
+ System.out.println("\n-------------------------------------------------");
|
|
135
|
+ System.out.println(num);
|
|
136
|
+ System.out.println("-------------------------------------------------");
|
|
137
|
+ System.out.println("Select trigonemtric function (enter # 1-6)");
|
|
138
|
+ System.out.println("(1) Sin");
|
|
139
|
+ System.out.println("(2) Cos");
|
|
140
|
+ System.out.println("(3) Tangent");
|
|
141
|
+ System.out.println("(4) ArcSin");
|
|
142
|
+ System.out.println("(5) ArcCos");
|
|
143
|
+ System.out.println("(6) ArcTan");
|
|
144
|
+ System.out.println("--------------------------------------------");
|
|
145
|
+ System.out.print("> ");
|
|
146
|
+ choice = stdin.nextInt();
|
|
147
|
+
|
|
148
|
+ switch(choice){
|
|
149
|
+ case 1: System.out.println("Calculating sine of " + num);
|
|
150
|
+ result = Math.sin(num);
|
|
151
|
+ isValid = true;
|
|
152
|
+ break;
|
|
153
|
+ case 2: System.out.println("Calculating cosine of " + num);
|
|
154
|
+ result = Math.cos(num);
|
|
155
|
+ isValid = true;
|
|
156
|
+ break;
|
|
157
|
+ case 3: System.out.println("Calculating tangent of " + num);
|
|
158
|
+ result = Math.tan(num);
|
|
159
|
+ isValid = true;
|
|
160
|
+ break;
|
|
161
|
+ case 4: System.out.println("Calculating inverse sine (arcSin) of " + num);
|
|
162
|
+ result = Math.asin(num);
|
|
163
|
+ isValid = true;
|
|
164
|
+ break;
|
|
165
|
+ case 5: System.out.println("Calculating inverse cosine (arcCos) of " + num);
|
|
166
|
+ result = Math.acos(num);
|
|
167
|
+ isValid = true;
|
|
168
|
+ break;
|
|
169
|
+ case 6: System.out.println("Calculating inverse tangent (arcTan) of " + num);
|
|
170
|
+ result = Math.atan(num);
|
|
171
|
+ isValid = true;
|
|
172
|
+ break;
|
|
173
|
+
|
|
174
|
+ }
|
|
175
|
+ }
|
|
176
|
+ return result;
|
|
177
|
+}
|
|
178
|
+
|
|
179
|
+public double advFunc(double num){ //take displayNum as input parameter
|
|
180
|
+
|
|
181
|
+ boolean isValid = false;
|
|
182
|
+ int choice;
|
|
183
|
+ double result = 0;
|
|
184
|
+
|
|
185
|
+ while(!isValid){
|
|
186
|
+ System.out.println("\n-------------------------------------------------");
|
|
187
|
+ System.out.println(num);
|
|
188
|
+ System.out.println("-------------------------------------------------");
|
|
189
|
+ System.out.println("Select advanced function to perform (enter # 1-7)");
|
|
190
|
+ System.out.println("(1) Factorial");
|
|
191
|
+ System.out.println("(2) Log");
|
|
192
|
+ System.out.println("(3) Inverse log (10^x)");
|
|
193
|
+ System.out.println("(4) Natural log (Ln x)");
|
|
194
|
+ System.out.println("(5) Inverse Natural log (e^x)");
|
|
195
|
+ System.out.println("(6) Cubed root"); //add custom 1
|
|
196
|
+ System.out.println("(7) Absolute value");//add custom 2
|
|
197
|
+ System.out.println("-------------------------------------------------");
|
|
198
|
+ System.out.print("> ");
|
|
199
|
+ choice = stdin.nextInt();
|
|
200
|
+
|
|
201
|
+ switch(choice){
|
|
202
|
+ case 1: System.out.println("Calculating factorial of " + num);
|
|
203
|
+ int i;
|
|
204
|
+ double fct = 1;
|
|
205
|
+ for(i=1; i<=num; i++){
|
|
206
|
+ fct *= i;
|
|
207
|
+ }
|
|
208
|
+ result = fct;
|
|
209
|
+ isValid = true;
|
|
210
|
+ break;
|
|
211
|
+ case 2: System.out.println("Calculating log of " + num);
|
|
212
|
+ result = Math.log10(num);
|
|
213
|
+ isValid = true;
|
|
214
|
+ break;
|
|
215
|
+ case 3: System.out.println("Calculating inverse log (10^x) of " + num);
|
|
216
|
+ result = Math.pow(10 , Math.log10(num));
|
|
217
|
+ isValid = true;
|
|
218
|
+ break;
|
|
219
|
+ case 4: System.out.println("Calculating natural log (Ln) of " + num);
|
|
220
|
+ result = Math.log(num);
|
|
221
|
+ isValid = true;
|
|
222
|
+ break;
|
|
223
|
+ case 5: System.out.println("Calculating inverese natural log (e^x) of " + num);
|
|
224
|
+ result = Math.exp(num);
|
|
225
|
+ isValid = true;
|
|
226
|
+ break;
|
|
227
|
+ case 6: System.out.println("Calculating cubed root of " + num);
|
|
228
|
+ result = Math.cbrt(num);
|
|
229
|
+ isValid = true;
|
|
230
|
+ break;
|
|
231
|
+ case 7: System.out.println("Calculating absolute value of " + num);
|
|
232
|
+ result = Math.abs(num);
|
|
233
|
+ isValid = true;
|
|
234
|
+ break;
|
|
235
|
+ default:
|
|
236
|
+ break;
|
|
237
|
+
|
|
238
|
+ }
|
|
239
|
+ }
|
|
240
|
+ return result;
|
|
241
|
+}
|
|
242
|
+
|
|
243
|
+}
|