-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalcuate.java
More file actions
68 lines (50 loc) · 1.57 KB
/
calcuate.java
File metadata and controls
68 lines (50 loc) · 1.57 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
import java.util.Scanner;
class Calculator {
int a, b;
// Parameterized Constructor
Calculator(int x, int y) {
a = x;
b = y;
}
void calculate(int choice) {
switch (choice) {
case 1:
System.out.println("Sum = " + (a + b));
break;
case 2:
System.out.println("Subtraction = " + (a - b));
break;
case 3:
System.out.println("Multiplication = " + (a * b));
break;
case 4:
if (b != 0)
System.out.println("Division = " + (a / b));
else
System.out.println("Cannot divide by zero");
break;
default:
System.out.println("Invalid Choice");
}
}
}
public class calcuate {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter First Number: ");
int num1 = sc.nextInt();
System.out.print("Enter Second Number: ");
int num2 = sc.nextInt();
// Creating object using constructor
Calculator c = new Calculator(num1, num2);
System.out.println("\nChoose Operation:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
c.calculate(ch);
sc.close();
}
}