-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGeneral_purpose_calc.java
510 lines (508 loc) · 30.5 KB
/
General_purpose_calc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import java.util.Scanner;
public class General_purpose_calc
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for(int k = 1; k <= 2; k++)
{
try
{
char ch1;
System.out.println(" \n \n> What would you like to calculate? < ");// You could also use \n at the end of an option instead of System.out.println again and again
for( ; ; )
{
Thread.sleep(900);
break;
}
System.out.println(" \n< Calculators > ");
System.out.println(" - Press '1' for a Pythagoras Theorem calculator ");
System.out.println(" - Press '2' for a Normal calculator ");
System.out.println(" - Press '3' for a Square Root calculator ");
System.out.println(" - Press '4' for a interest calculator (For recurring account) ");
System.out.println(" - Press '5' for an Area calculator ");
System.out.println(" - Press '6' for a Perimeter calculator ");
System.out.println(" - Press '7' for a Volume calculator ");
System.out.println(" - Press '8' for a Percentage calculator ");
for( ; ; )
{
Thread.sleep(500);
break;
}
System.out.println(" \n< Miscellaneous > ");
System.out.println(" - Press 'A' for a ASCII code finder ");
System.out.println(" - Press 'V' for version information ");
ch1 = sc.next().charAt(0); // Any input entered at this point will be taken as the value for variable "ch1" and used to determine what the user wants to do
if(ch1 == '1') // This part gets executed if user enters "1"
{
System.out.println(" > What information are you provided with ? ");
System.out.println(" - Press 1 if base and height are given ");
System.out.println(" - Press 2 if base and hypotenuse are given ");
System.out.println(" - press 3 if height and hypotenuse are given ");
double b, h, hp; // Declaring variables to be used in the following switch case
char chp;
chp = sc.next().charAt(0);
switch(chp)
{
case '1' :System.out.println(" > Enter the length of base ");
b = sc.nextDouble();
System.out.println(" > Enter the length of height ");
h = sc.nextDouble();
hp = b * b + h * h;
System.out.println(" Under root Hypotenuse is --> √" + hp );
double rhp = Math.sqrt(hp);
System.out.println(" Solved Hypotenuse is --> " + rhp );
break;
case '2' :System.out.println(" > Enter the length of base ");
b = sc.nextDouble();
System.out.println(" > Enter the length of hypotenuse ");
hp = sc.nextDouble();
h = hp * hp - b * b;
double rh;
System.out.println(" Under root Height is --> √" + h );
rh = Math.sqrt(h);
System.out.println(" Solved Height is --> " + rh );
break;
case '3' :System.out.println(" > Enter the length of height ");
h = sc.nextDouble();
System.out.println(" > Enter the length of hypotenuse ");
hp = sc.nextDouble();
b = hp * hp - h * h;
double rb;
System.out.println(" Under root Base is --> √" + b );
rb = Math.sqrt(b);
System.out.println(" Solved Base is --> " + rb );
break;
default :System.out.println(" ERROR --> Invalid ");
}
}
else if(ch1 == '2') // This part gets executed if user enters "2"
{
double a1 = 0;
char vnum;
System.out.println(" > What would you like to do ? < ");
System.out.println(" - Press '1' for Addition ");
System.out.println(" - Press '2' for Subtraction ");
System.out.println(" - Press '3' for Multiplication ");
System.out.println(" - Press '4' for Division ");
vnum = sc.next().charAt(0);
switch(vnum)
{
case '1' : char chn;
System.out.println(" > Enter the number of terms (2 - 5) ");
chn = sc.next().charAt(0);
switch(chn)
{
case '1' : System.out.println(" > Number of terms can not be 1 ");
break;
case '2' : double t1, t2;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
a1 = t1 + t2;
System.out.println(" Answer is --> " + a1);
break;
case '3' : double t3;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
a1 = t1 + t2 + t3;
System.out.println(" Answer is --> " + a1);
break;
case '4' : double t4;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
System.out.println(" > Enter the value of the fourth term ");
t4 = sc.nextDouble();
a1 = t1 + t2 + t3 + t4;
System.out.println(" Answer is --> " + a1);
break;
case '5' : double t5;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
System.out.println(" > Enter the value of the fourth term ");
t4 = sc.nextDouble();
System.out.println(" > Enter the value of the fifth term ");
t5 = sc.nextDouble();
a1 = t1 + t2 + t3 + t4 + t5;
System.out.println(" Answer is --> " + a1);
break;
default : System.out.println(" ERROR --> \n> You might have entered a character instead of a number \n> Or you might have entered a number greater than 5 \n> Please note 6 or more terms are not supported yet !");
}
break;
case '2' : System.out.println(" > Enter the number of terms (2 - 5) ");
chn = sc.next().charAt(0);
switch(chn)
{
case '1' : System.out.println(" > Number of terms can not be 1 ");
break;
case '2' : double t1, t2;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
a1 = t1 - t2;
System.out.println(" Answer is --> " + a1);
break;
case '3' : double t3;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
a1 = t1 - t2 - t3;
System.out.println(" Answer is --> " + a1);
break;
case '4' : double t4;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
System.out.println(" > Enter the value of the fourth term ");
t4 = sc.nextDouble();
a1 = t1 - t2 - t3 - t4;
System.out.println(" Answer is --> " + a1);
break;
case '5' : double t5;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
System.out.println(" > Enter the value of the fourth term ");
t4 = sc.nextDouble();
System.out.println(" > Enter the value of the fifth term ");
t5 = sc.nextDouble();
a1 = t1 - t2 - t3 - t4 - t5;
System.out.println(" Answer is --> " + a1);
break;
default : System.out.println(" ERROR --> \n> You might have entered a character instead of a number \n> Or you might have entered a number greater than 5 \n> Please note 6 or more terms are not supported yet !");
}
break;
case '3' : System.out.println(" > Enter the number of terms (2 - 5) ");
chn = sc.next().charAt(0);
switch(chn)
{
case '1' : System.out.println(" > Number of terms can not be 1 ");
break;
case '2' : double t1, t2;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
a1 = t1 * t2;
System.out.println(" Answer is --> " + a1);
break;
case '3' : double t3;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
a1 = t1 * t2 * t3;
System.out.println(" Answer is --> " + a1);
break;
case '4' : double t4;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
System.out.println(" > Enter the value of the fourth term ");
t4 = sc.nextDouble();
a1 = t1 * t2 * t3 * t4;
System.out.println(" Answer is --> " + a1);
break;
case '5' : double t5;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
System.out.println(" > Enter the value of the fourth term ");
t4 = sc.nextDouble();
System.out.println(" > Enter the value of the fifth term ");
t5 = sc.nextDouble();
a1 = t1 * t2 * t3 * t4 * t5;
System.out.println(" Answer is --> " + a1);
break;
default : System.out.println(" ERROR --> \n> You might have entered a character instead of a number \n> Or you might have entered a number greater than 5 \n> Please note 6 or more terms are not supported yet !");
}
break;
case '4' : System.out.println(" > Enter the number of terms (2 - 5) ");
chn = sc.next().charAt(0);
switch(chn)
{
case '1' : System.out.println(" > Number of terms can not be 1 ");
break;
case '2' : double t1, t2;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
a1 = t1 / t2;
System.out.println(" Answer is --> " + a1);
break;
case '3' : double t3;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
a1 = (t1 / t2) / t3;
System.out.println(" Answer is --> " + a1);
break;
case '4' : double t4;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
System.out.println(" > Enter the value of the fourth term ");
t4 = sc.nextDouble();
a1 = ((t1 / t2) / t3) / t4;
System.out.println(" Answer is --> " + a1);
break;
case '5' : double t5;
System.out.println(" > Enter the value of the first term ");
t1 = sc.nextDouble();
System.out.println(" > Enter the value of the second term ");
t2 = sc.nextDouble();
System.out.println(" > Enter the value of the third term ");
t3 = sc.nextDouble();
System.out.println(" > Enter the value of the fourth term ");
t4 = sc.nextDouble();
System.out.println(" > Enter the value of the fifth term ");
t5 = sc.nextDouble();
a1 = (((t1 / t2) / t3) / t4) / t5;
System.out.println(" Answer is --> " + a1);
break;
default : System.out.println(" ERROR --> \n> You might have entered a character instead of a number \n> Or you might have entered a number greater than 5 \n> Please note 6 or more terms are not supported yet !");
}
break;
default : System.out.println(" > Invalid choice ");
}
}
else if(ch1 == '3') // This part gets executed if user enters "3"
{
double srnum; // Using double instead of int as in the case of Math.sqrt int gives inaccurate values
System.out.println(" > Enter the number ");
srnum = sc.nextDouble();
double srnumR = Math.sqrt(srnum); // If you want to use int in place of double Use this 'int (Your int) = (int) Math.sqrt(Your int)<-- bracets madatory here
System.out.println(" Your answer is --> " + srnumR ); // using int in math operation is not recommended
}
else if(ch1 == '4') // This part is executed if user enters "4"
{
double p, r, tp, I, mv;
System.out.println(" > Enter principle ");
p = sc.nextDouble();
System.out.println(" > Enter the rate of interest ");
r = sc.nextDouble();
System.out.println(" > Enter Time period in months ");
tp = sc.nextDouble();
I = p * tp * (tp+1) * r / (2 * 12 * 100);
mv = p * tp + I;
System.out.println(" Intrest is --> " + I);
System.out.println(" Maturity value is --> " + mv);
}
else if(ch1 == '5') // This part gets executed when user enters "5"
{
char chA;
System.out.println(" > Choose the shape of the figure ");
System.out.println(" - Press '1' to calculate the Area of a square ");
System.out.println(" - Press '2' to calculate the Area of a rectangle ");
System.out.println(" - Press '3' to calculate the Area of a circle ");
chA = sc.next().charAt(0);
switch(chA)
{
case '1' :double ss;
System.out.println(" > Enter the value of side ");
ss = sc.nextDouble();
double ssA = ss * ss;
System.out.println(" Your answer is --> " + ssA );
break;
case '2' :double rl, rb;
System.out.println(" > Enter the value of breadth (width) ");
rb = sc.nextDouble();
System.out.println(" > Enter the value of length ");
rl = sc.nextDouble();
double rA = rb * rl;
System.out.println(" Your answer is --> " + rA );
break;
case '3' :double cr; // Once again using double for more accurate values
System.out.println(" Enter the value of radius ");
cr = sc.nextDouble();
double crX = 3.14159 * (cr * cr); // Defining PI to 5 decimals for even more accurate values
System.out.println(" Your answer is --> " + crX ); // value accurate upto 3 decimals only
break;
default : System.out.println(" ERROR --> Unkown choice ");
}
}
else if(ch1 == '6')
{
char chP;
System.out.println(" > Choose the shape of the figure ");
System.out.println(" - Press '1' to calculate the Perimeter of a square ");
System.out.println(" - Press '2' to calculate the Perimeter of a rectangle ");
System.out.println(" - Press '3' to calculate the Perimeter of a circle ");
chP = sc.next().charAt(0);
switch(chP)
{
case '1' :double sP;
System.out.println(" > Enter value of side ");
sP = sc.nextDouble();
double sPA = (4 * sP);
System.out.println(" Your answer is --> " + sPA );
break;
case '2' :double rPL, rPB;
System.out.println(" > Enter value of breadth (width) ");
rPB = sc.nextDouble();
System.out.println(" > Enter value of length ");
rPL = sc.nextDouble();
double rPA = 2 * (rPL + rPB);
System.out.println(" Your answer is --> " + rPA );
break;
case '3' :double crP;
System.out.println(" > Enter value of radius ");
crP = sc.nextDouble();
double crPA = 2 * 3.14159 * crP;
System.out.println(" Your answer is --> " + crPA ); // value accurate upto 3 decimals only
break;
default :System.out.println(" ERROR --> Unknown choice ");
}
}
else if(ch1 == '7')
{
char chv;
System.out.println(" > Choose the shape of the figure ");
System.out.println(" - Press '1' to calculate the Volume of a Cube ");
System.out.println(" - Press '2' to calculate the Volume of a Cubiod ");
System.out.println(" - Press '3' to calculate the Volume of a Sphere ");
System.out.println(" - Press '4' to calculate the Volume of a Cone ");
chv = sc.next().charAt(0);
if(chv == '1')
{
double cv;
System.out.println(" > Enter the lenght of the side ");
cv = sc.nextDouble();
double vac = cv * cv * cv;
System.out.println(" Your answer is --> " + vac );
}
else if(chv == '2')
{
double lc, lb, lh;
System.out.println(" > Enter the Length of the Cubiod ");
lc = sc.nextDouble();
System.out.println(" > Enter the Breadth(Width) of the Cubiod ");
lb = sc.nextDouble();
System.out.println(" > Enter the Height of the Cubiod ");
lh = sc.nextDouble();
double acc = lc * lb * lh;
System.out.println(" Your answer is --> " + acc );
}
else if(chv == '3')
{
double vao, ro;
System.out.println(" > Enter the value of Radius ");
ro = sc.nextDouble();
vao = 1.33333333333*3.14159*(ro*ro*ro);
System.out.println(" Your answer is --> " + vao );
}
else if(chv == '4')
{
double vac2, rc, hc;
System.out.println(" > Enter the height of the cone ");
hc = sc.nextDouble();
System.out.println(" > Enter the radius of the cone ");
rc = sc.nextDouble();
vac2 = 3.14159*(rc*rc)*(hc/3);
System.out.println(" Your answer is --> " + vac2 );
}
}
else if(ch1 == '8')
{
double p, pt, ans;
System.out.println(" > Enter the percentage you want to find (% value) ");
p = sc.nextDouble();
System.out.println(" > Enter the number of which you want to find " + p + "%");
pt = sc.nextDouble();
ans = (p/100) * pt;
System.out.println(" > " + p + "% of " + pt + " is --> " + ans);
}
else if(ch1 == 'v' || ch1 == 'V')
{
System.out.println(" ____________________ _ ");
System.out.println("| Running v2.1 | \\`*\\ ");
System.out.println("| Built By : | ) _`-. ");
System.out.println("| Kavin Mistry | / : `. . ");
System.out.println("| Github : | : _ ' \\ ");
System.out.println("| KavinMistry | ; *` _. `*-._ ");
System.out.println("|____________________| `-.-' `-. ");
System.out.println(" ; ` `. ");
System.out.println(" :. . \\ ");
System.out.println(" . \\ . : .-' . ");
System.out.println(" ' `+.; ; ' : ");
System.out.println(" : ' | ; ;-. ");
System.out.println(" ; ' : :`-: _.`* ; ");
System.out.println(" .*' / .*' ; .*`- +' `*' ");
System.out.println(" `*-* `*-* `*-*' ");
}
else if(ch1 == 'A' || ch1 == 'a')
{
char as;
System.out.println(" > Enter any number/letter/symbol to find the ASCII value of it ");
as = sc.next().charAt(0);
int as1; // declare a int variable
as1 = as; // then asign the char value of the char variable to the int variable basically a type cast which returns the ASCII value instead of the char value
System.out.println(" ASCII value of '" + as + "' is --> " + as1 );
}
else
{
System.out.println(" ERROR \n> You might have entered a undefined option or a symbol, try entering '1' or 'V' \n> If you continue to recive such errors please report them to my GitHub \n> Github username -- KavinMistry");
}
}
catch(Exception A)
{
System.out.println("ERROR --> " + A + " \n> You might have entered a special character \n> Or might have entered a undefined number option \n--> Report errors in github please ! \n # Github username -- KavinMistry < ");
}
System.out.println("\n> Would you like to run the program again ? ");
System.out.println("- Press 'y' to run again and any other key to exit ");
char z = sc.next().charAt(0);
if(z == 'y')
{
k = 1;
}
else if(z == 's')
{
System.out.println("\nYou found a secret ! ");
k = 3;
}
else
{
k = 3;
}
}
sc.close();
}
}