-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
543 lines (492 loc) · 21.2 KB
/
Main.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
import java.lang.Math;
import java.util.*;
public class Main {
public static void clearConsole() {
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("");
System.out.println("");
}
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
boolean end = false;
while (!end) {
Menu();
System.out.print("Enter option: ");
String choice = myObj.nextLine();
switch (choice) {
case "0" -> end = true;
case "1" -> CalcBMI();
case "2" -> CalcArea();
case "3" -> CalcCost();
case "4" -> Calculator();
case "5" -> NumberGuessingGame();
default -> System.out.println("Enter a valid input");
}
}
if (end) {
System.out.println("Thank You for using the calculator");
}
}
public static void Menu() {
System.out.println("------------- M E N U -------------");
System.out.println("-----------------------------------");
System.out.println("1. BMI Calculator");
System.out.println("2. Surface Area Calculator");
System.out.println("3. Cost Calculator");
System.out.println("4. Basic Calculator");
System.out.println("5. Number Guessing Game");
System.out.println("0. Exit");
}
public static void CalcBMI() {
clearConsole();
Scanner myObj = new Scanner(System.in);
System.out.print("Enter height in meters(m): ");
double height = Double.parseDouble(myObj.nextLine());
System.out.print("Enter weight in Kilograms(Kg): ");
double weight = Double.parseDouble(myObj.nextLine());
double bmi = weight / (height * height);
System.out.println("The BMI for a person with the weight of " + weight + "Kg and a height of " + height + "m is " + bmi);
System.out.println("");
}
public static void SelectShape() {
System.out.println("Calculate Area of Shapes");
System.out.println("-----------------------------------");
System.out.println("1. Circle");
System.out.println("2. Square");
System.out.println("3. Triangle");
System.out.println("4. Trapezium");
System.out.println("5. Rhombus");
System.out.println("0. Exit");
}
public static void CalcCircle() {
System.out.println("Calculating Area of Circle");
System.out.println("-----------------------------------");
System.out.println("");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter radius of Circle: ");
double r = Double.parseDouble(myObj.nextLine());
System.out.println("Pi Values Available");
System.out.println("-------------------");
System.out.println("1. 22/7");
System.out.println("2. 3.14");
System.out.println("3. Calculator Pi Value");
boolean Continue = true;
while (Continue) {
System.out.print("Select Pi Value: ");
String piOption = myObj.nextLine();
switch (piOption) {
case "1" -> {
double fracArea = (22 * r * r) / 7;
System.out.println("The Area of the Circle is " + fracArea);
System.out.println("");
Continue = false;
}
case "2" -> {
double simpArea = 3.14 * r * r;
System.out.println("The Area of the Circle is " + simpArea);
System.out.println("");
Continue = false;
}
case "3" -> {
double mathPiArea = Math.PI * r * r;
System.out.println("The Area of the Circle is " + mathPiArea);
System.out.println("");
Continue = false;
}
default -> {
System.out.println("Enter a valid input");
System.out.println("");
}
}
}
}
public static void CalcSquare() {
System.out.println("Calculating Area of Square");
System.out.println("-----------------------------------");
System.out.println("");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter length of one side: ");
double length = Double.parseDouble(myObj.nextLine());
double area = (length * length);
System.out.println("The area of a square with the length of " + length + " is " + area);
System.out.println("");
}
public static void CalcTriangle() {
System.out.println("Calculating Area of Triangle");
System.out.println("-----------------------------------");
System.out.println("");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter base of triangle: ");
double base = Double.parseDouble(myObj.nextLine());
System.out.print("Enter height of triangle: ");
double height = Double.parseDouble(myObj.nextLine());
double area = (base * height) / 2;
System.out.println("The area of a triangle with the base of " + base + " and height " + height + " is " + area);
System.out.println("");
}
public static void CalcTrapezium() {
System.out.println("Calculating Area of Trapezium");
System.out.println("-----------------------------------");
System.out.println("");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter first base of trapezium: ");
double base1 = Double.parseDouble(myObj.nextLine());
System.out.print("Enter opposite base of trapezium: ");
double base2 = Double.parseDouble(myObj.nextLine());
System.out.print("Enter height of trapezium: ");
double height = Double.parseDouble(myObj.nextLine());
double area = ((base1 + base2) * height) / 2;
System.out.println("The area of a trapezium with the base of " + base1 + " and " + base2 + " with height " + height + " is " + area);
System.out.println("");
}
public static void CalcRhombus() {
System.out.println("Calculating Area of Rhombus");
System.out.println("-----------------------------------");
System.out.println("");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter height of rhombus: ");
double height = Double.parseDouble(myObj.nextLine());
System.out.print("Enter length of any side of rhombus: ");
double length = Double.parseDouble(myObj.nextLine());
double area = (length * height);
System.out.println("The area of a rhombus with the length of one side as " + length + " and height " + height + " is " + area);
System.out.println("");
}
public static void CalcArea() {
clearConsole();
Scanner myObj = new Scanner(System.in);
boolean exit = true;
while (exit) {
SelectShape();
System.out.print("Enter option: ");
String choice = myObj.nextLine();
switch (choice) {
case "0" -> {
System.out.println("");
exit = false;
}
case "1" -> CalcCircle();
case "2" -> CalcSquare();
case "3" -> CalcTriangle();
case "4" -> CalcTrapezium();
case "5" -> CalcRhombus();
default -> {
System.out.println("Enter a valid input");
System.out.println("");
}
}
}
}
public static void CalcCost() {
clearConsole();
Scanner myObj = new Scanner(System.in);
System.out.print("Enter cost of product: ");
double cost = Double.parseDouble(myObj.nextLine());
System.out.print("Enter percentage discount: ");
double percentageDiscount = Double.parseDouble(myObj.nextLine());
double priceafterdiscount = (cost / 100) * (100 - percentageDiscount);
System.out.println("The price of the product after a " + percentageDiscount + "% discount before GST is " + priceafterdiscount);
System.out.println("");
System.out.print("Would you like to calculate the cost with GST? (Y/N): ");
String response = myObj.nextLine().toUpperCase(Locale.ROOT);
if (response.equals("Y") | response.equals("YES")){
System.out.print("Enter percentage of GST: ");
double gst = Double.parseDouble(myObj.nextLine());
double pricewithGST = ((priceafterdiscount / 100) * gst) + priceafterdiscount;
System.out.println("The price of the product after GST is " + pricewithGST);
System.out.println("");
}
}
public static void CalculatorMenu() {
System.out.println("Calculator Menu");
System.out.println("-----------------------------------");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Multiplication Table");
System.out.println("0. Exit");
}
public static void Add() {
System.out.println("Addition");
System.out.println("-----------------------------------");
System.out.println("");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = Double.parseDouble(myObj.nextLine());
System.out.print("Enter the second number: ");
double num2 = Double.parseDouble(myObj.nextLine());
double sum = (num1 + num2);
System.out.println(num1 + "+" + num2 + "=" + sum);
System.out.println("");
}
public static void Subtract() {
System.out.println("Subtraction");
System.out.println("-----------------------------------");
System.out.println("");
System.out.println("Enter the numbers in the following format when prompted");
System.out.println("num1 - num2 = answer");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = Double.parseDouble(myObj.nextLine());
System.out.print("Enter the second number: ");
double num2 = Double.parseDouble(myObj.nextLine());
double ans = (num1 - num2);
System.out.println(num1 + "-" + num2 + "=" + ans);
System.out.println("");
}
public static void Multiplication() {
System.out.println("Multiplication");
System.out.println("-----------------------------------");
System.out.println("");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = Double.parseDouble(myObj.nextLine());
System.out.print("Enter the second number: ");
double num2 = Double.parseDouble(myObj.nextLine());
double ans = (num1 * num2);
System.out.println(num1 + "X" + num2 + "=" + ans);
System.out.println("");
}
public static void Division() {
System.out.println("Division");
System.out.println("-----------------------------------");
System.out.println("");
System.out.println("Enter the numbers in the following format when prompted");
System.out.println("num1 / num2 = answer");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = Double.parseDouble(myObj.nextLine());
System.out.print("Enter the second number: ");
double num2 = Double.parseDouble(myObj.nextLine());
double ans = (num1 / num2);
System.out.println(num1 + "/" + num2 + "=" + ans);
System.out.println("");
}
public static void MultiplicationTable() {
System.out.println("Multiplication Table");
System.out.println("-----------------------------------");
System.out.println("");
Scanner myObj = new Scanner(System.in);
System.out.print("Enter a Integer: ");
int num = Integer.parseInt(myObj.nextLine());
for (int i = 0; i < 16; i++) {
int product = num * i;
System.out.println(i + " X " + num + " = " + product);
}
System.out.println("");
}
public static void Calculator() {
clearConsole();
Scanner myObj = new Scanner(System.in);
boolean exit = true;
while (exit) {
CalculatorMenu();
System.out.print("Enter option: ");
String choice = myObj.nextLine();
switch (choice) {
case "0" -> exit = false;
case "1" -> Add();
case "2" -> Subtract();
case "3" -> Multiplication();
case "4" -> Division();
case "5" -> MultiplicationTable();
default -> System.out.println("Enter a valid input");
}
}
}
public static void NumberGuessingGameMenu() {
System.out.println("Number Guessing Game Menu");
System.out.println("-----------------------------------");
System.out.println("1. Easy Mode - Unlimited Attempts");
System.out.println("2. Hard Mode - 10 Attempts");
System.out.println("3. Impossible Mode - 5 Attempts");
System.out.println("0. Exit");
}
public static void EasyMode() {
Random rand = new Random();
Scanner myObj = new Scanner(System.in);
int upperbound = 101;
int int_random = rand.nextInt(upperbound);
int guess = 101;
int guessnum = 1;
while (true) {
if (guessnum == 1) {
System.out.println("Welcome to number guessing game Easy Mode");
System.out.println("The integer is in the range of 0 and 100");
System.out.println("Guess a number: ");
guess = Integer.parseInt(myObj.nextLine());
if (guess > 100) {
System.out.println("The integer is in the range of 0 and 100");
}
else if (guess == int_random) {
System.out.println("Congrats! you got it. The number was " + int_random);
System.out.println("The number was guessed in " + guessnum + " of tries.");
break;
}
else {
HigherLowerCheck(int_random, guess);
System.out.println("Guess Again!");
guessnum += 1;
System.out.println("");
System.out.println("Guess a number: ");
guess = Integer.parseInt(myObj.nextLine());
}
}
else {
if (guess > 100) {
System.out.println("The integer is in the range of 0 and 100");
}
else if (guess == int_random){
System.out.println("Congrats! you got it. The number was " + int_random);
System.out.println("The number was guessed in " + guessnum + " of tries.");
break;
}
else {
HigherLowerCheck(int_random, guess);
System.out.println("Guess Again!");
guessnum += 1;
System.out.println("");
System.out.println("Guess a number: ");
guess = Integer.parseInt(myObj.nextLine());
}
}
}
}
public static void HardMode() {
Random rand = new Random();
Scanner myObj = new Scanner(System.in);
int upperbound = 101;
int int_random = rand.nextInt(upperbound);
int guess = 101;
int guessnum = 1;
while (guessnum < 11) {
if (guessnum == 1) {
System.out.println("Welcome to number guessing game Hard Mode");
System.out.println("You have 10 tries");
System.out.println("The integer is in the range of 0 and 100");
System.out.println("Guess a number: ");
guess = Integer.parseInt(myObj.nextLine());
if (guess > 100) {
System.out.println("The integer is in the range of 0 and 100");
}
else if (guess == int_random) {
System.out.println("Congrats! you got it. The number was " + int_random);
System.out.println("The number was guessed in " + guessnum + " of tries.");
guessnum = 13;
}
else {
HigherLowerCheck(int_random, guess);
System.out.println("Guess Again!");
guessnum += 1;
System.out.println("");
System.out.println("Guess a number: ");
guess = Integer.parseInt(myObj.nextLine());
}
}
else {
if (guess > 100) {
System.out.println("The integer is in the range of 0 and 100");
}
else if (guess == int_random){
System.out.println("Congrats! you got it. The number was " + int_random);
System.out.println("The number was guessed in " + guessnum + " of tries.");
guessnum = 13;
}
else {
HigherLowerCheck(int_random, guess);
System.out.println("Guess Again!");
guessnum += 1;
System.out.println("");
System.out.println("Guess a number: ");
guess = Integer.parseInt(myObj.nextLine());
}
}
}
System.out.println("Thank You for Playing! The number was " + int_random);
}
public static void ImpossibleMode() {
Random rand = new Random();
Scanner myObj = new Scanner(System.in);
int upperbound = 101;
int int_random = rand.nextInt(upperbound);
int guess = 101;
int guessnum = 1;
while (guessnum < 6) {
if (guessnum == 1) {
System.out.println("Welcome to number guessing game Impossible Mode");
System.out.println("You have 5 attempts");
System.out.println("The integer is in the range of 0 and 100");
System.out.println("Guess a number: ");
guess = Integer.parseInt(myObj.nextLine());
if (guess > 100) {
System.out.println("The integer is in the range of 0 and 100");
}
else if (guess == int_random) {
System.out.println("Congrats! you got it. The number was " + int_random);
System.out.println("The number was guessed in " + guessnum + " of tries.");
guessnum = 13;
}
else {
HigherLowerCheck(int_random, guess);
System.out.println("Guess Again!");
guessnum += 1;
System.out.println("");
System.out.println("Guess a number: ");
guess = Integer.parseInt(myObj.nextLine());
}
}
else {
if (guess > 100) {
System.out.println("The integer is in the range of 0 and 100");
}
else if (guess == int_random){
System.out.println("Congrats! you got it. The number was " + int_random);
System.out.println("The number was guessed in " + guessnum + " of tries.");
guessnum = 13;
}
else {
HigherLowerCheck(int_random, guess);
System.out.println("Guess Again!");
guessnum += 1;
System.out.println("");
System.out.println("Guess a number: ");
guess = Integer.parseInt(myObj.nextLine());
}
}
}
System.out.println("Thank You for Playing! The number was " + int_random);
}
public static void HigherLowerCheck(int random, int guess) {
if (random > guess){
System.out.println("Guess Higher!");
}
else {
System.out.println("Guess Lower!");
}
}
public static void NumberGuessingGame() {
clearConsole();
Scanner myObj = new Scanner(System.in);
boolean exit = true;
while (exit) {
NumberGuessingGameMenu();
System.out.print("Enter option: ");
String choice = myObj.nextLine();
switch (choice) {
case "0" -> exit = false;
case "1" -> EasyMode();
case "2" -> HardMode();
case "3" -> ImpossibleMode();
default -> System.out.println("Enter a valid input");
}
}
}
}