-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorUsingInterface.java
More file actions
50 lines (41 loc) · 1.5 KB
/
CalculatorUsingInterface.java
File metadata and controls
50 lines (41 loc) · 1.5 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
package InternSavy;
import java.util.Scanner;
interface Calculator {
public void add(int a, int b);
public void subtract(int a, int b);
public void multiply(int a, int b);
public void divide(int a, int b);
}
class BasicCalculator implements Calculator {
public void add(int a, int b) {
System.out.println("The Addition of "+ a + " + " + b + " = " + (a + b));
}
public void subtract(int a, int b) {
System.out.println("The Subtraction of "+a + " - " + b + " = " + (a - b));
}
public void multiply(int a, int b) {
System.out.println("The Multiplication of "+a + " * " + b + " = " + (a * b));
}
public void divide(int a, int b) {
if (b == 0) {
System.out.println("Cannot divide by zero.");
} else {
System.out.println("The Division of "+a + " / " + b + " = " + (a / b));
}
}
}
public class CalculatorUsingInterface {
public static void main(String[] args) {
Calculator calculator = new BasicCalculator();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number1");
int number1=sc.nextInt();
System.out.println("Enter the number1");
int number2=sc.nextInt();
calculator.add(number1, number2);
calculator.subtract(number1, number2);
calculator.multiply(number1, number2);
calculator.divide(number1, number2);
sc.close();
}
}