-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.java
More file actions
92 lines (72 loc) · 1.95 KB
/
Copy pathCalc.java
File metadata and controls
92 lines (72 loc) · 1.95 KB
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
package calculator;
import java.util.Scanner;
public class Calc {
private static int num1, num2;
public static void main(String[] args) {
Calc calc = new Calc();
calc.showMenu(calc);
}
public float sum(float a, float b) {
return a + b;
}
public float subtract(float a, float b) {
return a - b;
}
public float mul(float a, float b) {
return a * b;
}
public float div(float a, float b) {
return a / b;
}
public void showMenu(Calc calc) {
float result;
String choice="0";
char end=0;
Scanner read =new Scanner(System.in);
do {
System.out.println("Select any one");
System.out.println("For addition press 1 or +");
System.out.println("For subtraction press 2 or -");
System.out.println("For multiplication press 3 or *");
System.out.println("For division press 4 or /");
System.out.println("For exit press c");
choice=read.nextLine();
switch(choice) {
case "1": case "+":
calc.takeInput();
result = calc.sum(num1, num2);
System.out.println("result is: "+result+"\n");
break;
case "2": case "-":
calc.takeInput();
result = calc.subtract(num1, num2);
System.out.println("result is: "+result+"\n");
break;
case "3": case "*":
calc.takeInput();
result = calc.mul(num1, num2);
System.out.println("result is: "+result+"\n");
break;
case "4": case "/":
calc.takeInput();
result = calc.div(num1, num2);
System.out.println("result is: "+result+"\n");
break;
case "c":
System.out.println("progrmm is terminated......");
end = choice.charAt(0);
// System.out.println(end);
break;
default:
System.out.println("you have pressed wrong input!!! plese try again\n");
}
}while(end!='c' );
}
public void takeInput() {
Scanner input =new Scanner(System.in);
System.out.println("Enter 1st number:");
num1=input.nextInt();
System.out.println("Enter 2nd number:");
num2=input.nextInt();
}
}