-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.java
More file actions
84 lines (72 loc) · 2.07 KB
/
Calc.java
File metadata and controls
84 lines (72 loc) · 2.07 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
import java.util.Scanner;
class Calculator {
Scanner scan = new Scanner(System.in);
int a;
int b;
public Calculator(int a, int b) {
this.a = a;
this.b = b;
}
public int add() {
return a + b;
}
public int sub() {
return a - b;
}
public int mul() {
return a * b;
}
public int div(){
if(b==0){
System.out.println("Cannot divide by zero, Syntax Error");
return 0;
}else{
return a/b;
}
}
public void getVal() {
System.out.print("Enter 1st number: ");
a = scan.nextInt();
System.out.print("Enter 2nd number: ");
b = scan.nextInt();
}
}
public class Calc {
public static void main(String[] args) {
Calculator c = new Calculator(0, 0);
Scanner scan = new Scanner(System.in);
boolean keepRunning = true;
while(keepRunning){
System.out.println("\nWelcom to Calculator");
System.out.println("**************");
System.out.println("Enter choice \n1.Addition \n2.Subtrraction \n3.Multiplication \n4.Division \n5.Exit");
System.out.println("**************");
int n = scan.nextInt();
switch (n) {
case 1:
c.getVal();
System.out.print(c.add());
break;
case 2:
c.getVal();
System.out.print(c.sub());
break;
case 3:
c.getVal();
System.out.print(c.mul());
break;
case 4:
c.getVal();
System.out.print(c.div());
break;
case 5:
System.out.println("Exiting...");
keepRunning = false;
break;
default:
System.out.println("Wrong choice, Please select from above options");
}
}
scan.close();
}
}