-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
63 lines (51 loc) · 1.74 KB
/
Copy pathCalculator.java
File metadata and controls
63 lines (51 loc) · 1.74 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
import java.util.Scanner;
class MathOperations {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public double divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("You cannot divide by zero!");
}
return (double) a / b;
}
}
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MathOperations mathOps = new MathOperations();
System.out.print("Enter First Number: ");
int fn = sc.nextInt();
System.out.print("Enter the Second Number: ");
int sn = sc.nextInt();
System.out.print("Please Enter Your Operator (* , / , + , -) ---> ");
char operator = sc.next().charAt(0);
switch (operator) {
case '+':
System.out.println("The sum is = " + mathOps.add(fn, sn));
break;
case '-':
System.out.println("The subtraction = " + mathOps.subtract(fn, sn));
break;
case '*':
System.out.println("The product is = " + mathOps.multiply(fn, sn));
break;
case '/':
if (sn == 0) {
System.out.println("You can't divide (0 / 0)!");
} else {
System.out.println("The division is = " + mathOps.divide(fn, sn));
}
break;
default:
System.out.println("Error!!! Invalid Operator.");
}
sc.close();
}
}