-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyCalc_JE.java
628 lines (614 loc) · 35.5 KB
/
PyCalc_JE.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import java.util.Scanner;
public class PyCalc_JE {
private static final String CURRENT_VERSION = "1.1";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("PyCalc-JE : A Simple and Lightweight Calculator. Now in Java!\n" +
"Version : " + CURRENT_VERSION + "\n"); while (true) {
System.out.println("\nSelect a Mathematical Operation : \n" +
"1. Addition\n" + "2. Subtraction\n" + "3. Multiplication\n" + "4. Division\n" + "5. Exponents (x^y)\n" + "6. Square Root\n" +
"7. Cube Root\n" + "8. Approximate / Rounding\n" + "9. Heron's Formula\n" + "10. Simple Interest\n" + "11. Compound Interest\n" + "12. Prime No. Check\n" +
"13. Triangle Check\n" + "14. Right Triangle Check\n" + "15. Perimeter [Various Shapes]\n" + "16. Area [Various Shapes]\n" +
"17. Volume [Various Shapes]\n" + "18. Surface Area [Various Shapes]\n" + "19. Curved Surface Area [Various Shapes]\n" +
"20. Diagonal Calculation [Various Shapes]\n" + "21. Factorial Calculator\n" + "22. Exit PyCalc-JE");
System.out.print("Enter choice [ 1 - 22 ] : ");
String choice = scanner.nextLine();
System.out.println();
if (choice.equals("1")) { // Addition
try {
System.out.print("Enter first number : ");
double x = scanner.nextDouble();
System.out.println();
System.out.print("Enter second number : ");
double y = scanner.nextDouble();
System.out.println();
System.out.println(x + " + " + y + " = " + (x + y) + "\n");
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("2")) { // Subtraction
try {
System.out.print("Enter first number : ");
double x = scanner.nextDouble();
System.out.println();
System.out.print("Enter second number : ");
double y = scanner.nextDouble();
System.out.println();
System.out.println(x + " - " + y + " = " + (x - y) + "\n");
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("3")) { // Multiplication
try {
System.out.print("Enter first number : ");
double x = scanner.nextDouble();
System.out.println();
System.out.print("Enter second number : ");
double y = scanner.nextDouble();
System.out.println();
System.out.println(x + " * " + y + " = " + (x * y) + "\n");
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("4")) { // Division
try {
System.out.print("Enter first number : ");
double x = scanner.nextDouble();
System.out.println();
System.out.print("Enter second number : ");
double y = scanner.nextDouble();
System.out.println();
if (y == 0) {
System.out.println("Div. by Zero Not Defined!\n");
} else {
System.out.println(x + " / " + y + " = " + (x / y) + "\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("5")) { // Exponents
try {
System.out.print("Enter base number (x) : ");
double x = scanner.nextDouble();
System.out.println();
System.out.print("Enter exponent (y) : ");
double y = scanner.nextDouble();
System.out.println();
System.out.println(x + " ^ " + y + " = " + Math.pow(x, y) + "\n");
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("6")) { // Square Root
try {
System.out.print("Enter a number : ");
double x = scanner.nextDouble();
System.out.println();
if (x < 0) {
System.out.println("Square root of a negative number is not real.\n");
} else {
System.out.println("Root " + x + " = " + Math.sqrt(x) + "\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("7")) { // Cube Root
try {
System.out.print("Enter a number : ");
double x = scanner.nextDouble();
System.out.println();
System.out.println("C. Root " + x + " = " + Math.cbrt(x) + "\n");
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("8")) { // Rounding
try {
System.out.print("Enter a number : ");
double x = scanner.nextDouble();
System.out.println();
System.out.println(Math.round(x) + "\n");
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("9")) { // Heron's Formula
try {
System.out.println("NOTE: Some Calculations may print 0.0 depending on the values!\n");
System.out.print("Enter the first side [a] : ");
double a = scanner.nextDouble();
System.out.println();
System.out.print("Enter the second side [b] : ");
double b = scanner.nextDouble();
System.out.println();
System.out.print("Enter third side [c] : ");
double c = scanner.nextDouble();
System.out.println();
if (a <= 0 || b <= 0 || c <= 0) {
System.out.println("Sides must be +ve.\n");
} else {
double s = (a + b + c) / 2;
System.out.println("Perimeter : " + (a + b + c));
System.out.println("s = " + s);
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.println("Area : " + area);
System.out.println();
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("10")) { // Simple Interest
try {
System.out.print("Enter the Principal : ");
double p = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Rate [ % ] : ");
double r = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Time [ Years ] : ");
double t = scanner.nextDouble();
System.out.println();
double si = p * r * t / 100;
System.out.println("Simple Interest : " + si + "\nAmount : " + (si + p) + "\n");
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("11")) { // Compound Interest
try {
System.out.println("Compound Interest Calculation\n");
System.out.print("Enter the Principal : ");
double p = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Rate [ % ] : ");
double r = scanner.nextDouble();
System.out.println();
System.out.print("Enter the number of times interest is compounded per year : ");
double n = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Time [ Years ] : ");
double t = scanner.nextDouble();
System.out.println();
double amount = p * Math.pow((1 + r / (100 * n)), n * t);
double ci = amount - p;
System.out.println("Compound Interest : " + ci + "\nAmount : " + amount + "\n");
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("12")) { // Prime Number Check
try {
System.out.print("Enter a Number : ");
int x = scanner.nextInt();
System.out.println();
boolean isPrime = true;
if (x <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(x); i++) {
if (x % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(x + " is a Prime Number\n");
} else {
System.out.println(x + " is not a Prime Number\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter an integer.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("13")) { // Triangle Check
try {
System.out.print("Enter first side [a] : ");
double a = scanner.nextDouble();
System.out.println();
System.out.print("Enter second side [b] : ");
double b = scanner.nextDouble();
System.out.println();
System.out.print("Enter third side [c] : ");
double c = scanner.nextDouble();
System.out.println();
if (a <= 0 || b <= 0 || c <= 0) {
System.out.println("Sides must be +ve\n");
} else if ((a + b > c) && (a + c > b) && (b + c > a)) {
System.out.println("Valid Triangle\n");
} else {
System.out.println("Not a Valid Triangle\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("14")) { // Right Triangle Check
try {
System.out.print("Enter first side [a] : ");
double a = scanner.nextDouble();
System.out.println();
System.out.print("Enter second side [b] : ");
double b = scanner.nextDouble();
System.out.println();
System.out.print("Enter third side [c] : ");
double c = scanner.nextDouble();
System.out.println();
if (a <= 0 || b <= 0 || c <= 0) {
System.out.println("Sides must be +ve\n");
} else if (Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2) ||
Math.pow(a, 2) + Math.pow(c, 2) == Math.pow(b, 2) ||
Math.pow(b, 2) + Math.pow(c, 2) == Math.pow(a, 2)) {
System.out.println("Is a Right Triangle\n");
} else {
System.out.println("Not a Right Triangle.\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("15")) { // Perimeter [Various Shapes]
System.out.println("Perimeter Calculation [Various Shapes]\n" +
"Select a shape : \n" + "1. Equilateral Triangle\n" + "2. Isosceles Triangle\n" + "3. Square / Rhombus\n" + "4. Rectangle / Parallelogram\n" + "5. Circle\n");
System.out.print("Enter shape choice [ 1 - 5 ]: ");
String shapeChoice = scanner.nextLine();
System.out.println();
try {
if (shapeChoice.equals("1")) { // Equilateral Triangle
System.out.print("Enter Side Length [a] : ");
double a = scanner.nextDouble();
System.out.println();
System.out.println("Perimeter : " + (3 * a) + "\n");
} else if (shapeChoice.equals("2")) { // Isosceles Triangle
System.out.print("Enter Equal Side's Length : ");
double a = scanner.nextDouble();
System.out.println();
System.out.print("Enter Non-Equal Side's Length : ");
double b = scanner.nextDouble();
System.out.println();
System.out.println("Perimeter : " + (2 * a + b) + "\n");
} else if (shapeChoice.equals("3")) { // Square / Rhombus
System.out.print("Enter Length of Side [a] : ");
double a = scanner.nextDouble();
System.out.println();
System.out.println("Perimeter : " + (4 * a) + "\n");
} else if (shapeChoice.equals("4")) { // Rectangle / Parallelogram
System.out.print("Enter Length [l] : ");
double l = scanner.nextDouble();
System.out.println();
System.out.print("Enter Breadth [b] : ");
double b = scanner.nextDouble();
System.out.println();
System.out.println("Perimeter : " + (2 * (l + b)) + "\n");
} else if (shapeChoice.equals("5")) { // Circle
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
double circumference = 2 * Math.PI * r;
System.out.println("Circumference : " + circumference + "\n");
} else {
System.out.println("Invalid Option! Exiting Perimeter Calculation ...\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("16")) { // Area [Various Shapes]
System.out.println("Area Calculation [Various Shapes]\n" +
"Select a shape : \n" + "1. Equilateral Triangle\n" + "2. Isosceles Triangle\n" + "3. Square\n" + "4. Rectangle / Parallelogram\n" +
"5. Rhombus\n" + "6. Circle\n" + "7. Semi-circle\n");
System.out.print("Enter shape choice [ 1 - 7 ] : ");
String shapeChoice = scanner.nextLine();
System.out.println();
try {
if (shapeChoice.equals("1")) { // Equilateral Triangle
System.out.print("Enter Side Length [a] : ");
double a = scanner.nextDouble();
System.out.println();
double area = (Math.sqrt(3) / 4) * Math.pow(a, 2);
System.out.println("Area : " + area + "\n");
} else if (shapeChoice.equals("2")) { // Isosceles Triangle
System.out.print("Enter Equal Side's Length : ");
double a = scanner.nextDouble();
System.out.println();
System.out.print("Enter Non-Equal Side's Length : ");
double b = scanner.nextDouble();
System.out.println();
// Check if the triangle inequality holds for the base and two equal sides
if (2 * a <= b) {
System.out.println("Invalid side lengths for an isosceles triangle.\n");
} else {
double area = (b / 4) * Math.sqrt(4 * Math.pow(a, 2) - Math.pow(b, 2));
System.out.println("Area : " + area + "\n");
}
} else if (shapeChoice.equals("3")) { // Square
System.out.print("Enter Length of Side [a] : ");
double a = scanner.nextDouble();
System.out.println();
double area = Math.pow(a, 2);
System.out.println("Area : " + area + "\n");
} else if (shapeChoice.equals("4")) { // Rectangle / Parallelogram
System.out.print("Enter Length [l] : ");
double l = scanner.nextDouble();
System.out.println();
System.out.print("Enter Breadth [b] : ");
double b = scanner.nextDouble();
System.out.println();
double area = l * b;
System.out.println("Area : " + area + "\n");
} else if (shapeChoice.equals("5")) { // Rhombus
System.out.print("Enter Diagonal 1 [d1] : ");
double d1 = scanner.nextDouble();
System.out.println();
System.out.print("Enter Diagonal 2 [d2] : ");
double d2 = scanner.nextDouble();
System.out.println();
double area = 0.5 * d1 * d2;
System.out.println("Area of Rhombus : " + area + "\n");
} else if (shapeChoice.equals("6")) { // Circle
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
double area = Math.PI * Math.pow(r, 2);
System.out.println("Area : " + area + "\n");
} else if (shapeChoice.equals("7")) { // Semi-circle
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
double area = 0.5 * Math.PI * Math.pow(r, 2);
System.out.println("Area : " + area + "\n");
} else {
System.out.println("Invalid Option! Exiting Area Calculation ...\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("17")) { // Volume [Various Shapes]
System.out.println("Volume Calculation [Various Shapes]\n" +
"Select a shape : \n" + "1. Cube\n" + "2. Cuboid\n" + "3. Cylinder\n" + "4. Cone\n" + "5. Sphere\n");
System.out.print("Enter shape choice [ 1 - 5 ] : ");
String shapeChoice = scanner.nextLine();
System.out.println();
try {
if (shapeChoice.equals("1")) { // Cube
System.out.print("Enter Length of Side [a] : ");
double a = scanner.nextDouble();
System.out.println();
double vol = Math.pow(a, 3);
System.out.println("Volume : " + vol + "\n");
} else if (shapeChoice.equals("2")) { // Cuboid
System.out.print("Enter Length [l] : ");
double l = scanner.nextDouble();
System.out.println();
System.out.print("Enter Breadth [b] : ");
double b = scanner.nextDouble();
System.out.println();
System.out.print("Enter Height [h] : ");
double h = scanner.nextDouble();
System.out.println();
double vol = l * b * h;
System.out.println("Volume : " + vol + "\n");
} else if (shapeChoice.equals("3")) { // Cylinder
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Height [h] : ");
double h = scanner.nextDouble();
System.out.println();
double vol = Math.PI * Math.pow(r, 2) * h;
System.out.println("Volume : " + vol + "\n");
} else if (shapeChoice.equals("4")) { // Cone
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Height [h] : ");
double h = scanner.nextDouble();
System.out.println();
double vol = (1.0 / 3.0) * Math.PI * Math.pow(r, 2) * h;
System.out.println("Volume : " + vol + "\n");
} else if (shapeChoice.equals("5")) { // Sphere
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
double vol = (4.0 / 3.0) * Math.PI * Math.pow(r, 3);
System.out.println("Volume : " + vol + "\n");
} else {
System.out.println("Invalid Option! Exiting Volume Calculation ...\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("18")) { // Surface Area [Various Shapes]
System.out.println("Surface Area Calculation [Various Shapes]\n" +
"Select a shape : \n" + "1. Cube\n" + "2. Cuboid\n" + "3. Cylinder\n" + "4. Cone\n" + "5. Sphere\n");
System.out.print("Enter shape choice [ 1 - 5 ] : ");
String shapeChoice = scanner.nextLine();
System.out.println();
try {
if (shapeChoice.equals("1")) { // Cube
System.out.print("Enter Length of Side [a] : ");
double a = scanner.nextDouble();
System.out.println();
double sa = 6 * Math.pow(a, 2);
System.out.println("Surface Area : " + sa + "\n");
} else if (shapeChoice.equals("2")) { // Cuboid
System.out.print("Enter Length [l] : ");
double l = scanner.nextDouble();
System.out.println();
System.out.print("Enter Breadth [b] : ");
double b = scanner.nextDouble();
System.out.println();
System.out.print("Enter Height [h] : ");
double h = scanner.nextDouble();
System.out.println();
double sa = 2 * (l * b + b * h + h * l);
System.out.println("Surface Area : " + sa + "\n");
} else if (shapeChoice.equals("3")) { // Cylinder
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Height [h] : ");
double h = scanner.nextDouble();
System.out.println();
double sa = 2 * Math.PI * r * (r + h);
System.out.println("Surface Area : " + sa + "\n");
} else if (shapeChoice.equals("4")) { // Cone
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Height [h] : ");
double h = scanner.nextDouble();
System.out.println();
double sa = Math.PI * r * (r + Math.sqrt(Math.pow(h, 2) + Math.pow(r, 2)));
System.out.println("Surface Area : " + sa + "\n");
} else if (shapeChoice.equals("5")) { // Sphere
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
double sa = 4 * Math.PI * Math.pow(r, 2);
System.out.println("Surface Area : " + sa + "\n");
} else {
System.out.println("Invalid Option! Exiting Surface Area Calculation ...\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("19")) { // Curved Surface Area [Various Shapes]
System.out.println("Curved Surface Area Calculation [Various Shapes]\n" +
"Select a shape : \n" + "1. Cylinder\n" + "2. Cone\n" + "3. Sphere\n");
System.out.print("Enter shape choice [ 1 - 3 ] : ");
String shapeChoice = scanner.nextLine();
System.out.println();
try {
if (shapeChoice.equals("1")) { // Cylinder
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Height [h] : ");
double h = scanner.nextDouble();
System.out.println();
double csa = 2 * Math.PI * r * h;
System.out.println("Curved Surface Area : " + csa + "\n");
} else if (shapeChoice.equals("2")) { // Cone
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
System.out.print("Enter the Height [h] : ");
double h = scanner.nextDouble();
System.out.println();
double csa = Math.PI * r * Math.sqrt(Math.pow(h, 2) + Math.pow(r, 2));
System.out.println("Curved Surface Area : " + csa + "\n");
} else if (shapeChoice.equals("3")) { // Sphere
System.out.print("Enter the Radius [r] : ");
double r = scanner.nextDouble();
System.out.println();
double csa = 4 * Math.PI * Math.pow(r, 2); // Note: Python code had 2 * pi * r^2, full sphere CSA is 4 * pi * r^2
System.out.println("Curved Surface Area : " + csa + "\n");
} else {
System.out.println("Invalid Option! Exiting Curved Surface Area Calculation ...");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("20")) { // Diagonal Calculation [Various Shapes]
System.out.println("Diagonal Calculation [Various Shapes]\n" +
"Select a shape : \n" + "1. Square\n" + "2. Rectangle\n" + "3. Cube\n" + "4. Cuboid\n");
System.out.print("Enter shape choice [ 1 - 4 ] : "); // Corrected range in prompt
String shapeChoice = scanner.nextLine();
System.out.println();
try {
if (shapeChoice.equals("1")) { // Square
System.out.print("Enter Length of Side [a] : ");
double a = scanner.nextDouble();
System.out.println();
double d = Math.sqrt(2) * a;
System.out.println("Diagonal : " + d + "\n");
} else if (shapeChoice.equals("2")) { // Rectangle
System.out.print("Enter Length [l] : ");
double l = scanner.nextDouble();
System.out.println();
System.out.print("Enter Breadth [b] : ");
double b = scanner.nextDouble();
System.out.println();
double d = Math.sqrt(Math.pow(l, 2) + Math.pow(b, 2));
System.out.println("Diagonal : " + d + "\n");
} else if (shapeChoice.equals("3")) { // Cube
System.out.print("Enter Length of Side [a] : ");
double a = scanner.nextDouble();
System.out.println();
double d = Math.sqrt(3) * a;
System.out.println("Diagonal : " + d + "\n");
} else if (shapeChoice.equals("4")) { // Cuboid
System.out.print("Enter Length [l] : ");
double l = scanner.nextDouble();
System.out.println();
System.out.print("Enter Breadth [b] : ");
double b = scanner.nextDouble();
System.out.println();
System.out.print("Enter Height [h] : ");
double h = scanner.nextDouble();
System.out.println();
double d = Math.sqrt(Math.pow(l, 2) + Math.pow(b, 2) + Math.pow(h, 2));
System.out.println("Diagonal : " + d + "\n");
} else {
System.out.println("Invalid Option! Exiting Diagonal Calculation ...");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("21")) { // Factorial Calculation
try {
System.out.print("Enter a Number : ");
int x = scanner.nextInt();
System.out.println();
if (x < 0) {
System.out.println("\nFactorial Not Defined for Negative Numbers!\n");
} else if (x == 0 || x == 1) {
System.out.println("\nFactorial of " + x + " is 1\n");
} else {
long fact = 1; // Use long for larger factorials
for (int i = 2; i <= x; i++) {
fact *= i;
}
System.out.println("\nFactorial of " + x + " is " + fact + "\n");
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter an integer.\n");
scanner.next(); // Consume the invalid input
}
} else if (choice.equals("22")) { // Exit Program
System.out.println("Exiting PyCalc-JE....");
scanner.close(); // Close the scanner
System.exit(0);
} else { // Default Case
System.out.println("Please enter a Valid Input! Restarting PyCalc-JE...\n");
}
// Handle consuming the newline character left by scanner.nextDouble() or scanner.nextInt()
// before the next scanner.nextLine()
if (scanner.hasNextLine()) {
scanner.nextLine();
}
System.out.print("Do you Want to Perform Another Calculation? [y/n]: ");
String nextCalc = scanner.nextLine();
System.out.println();
if (nextCalc.equalsIgnoreCase("n") || nextCalc.equalsIgnoreCase("no")) {
System.out.println("Exiting PyCalc. Goodbye!");
break; // Exit the while loop
} else if (!(nextCalc.equalsIgnoreCase("y") || nextCalc.equalsIgnoreCase("yes"))) {
System.out.println("Please enter a Valid Input! Restarting PyCalc ...\n");
}
}
scanner.close(); // Ensure scanner is closed if loop is exited otherwise than System.exit(0)
}
}