-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab1_Calculator
49 lines (43 loc) · 1.25 KB
/
lab1_Calculator
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
interface ICalculator {
int add(int x, int y);
float divide(int x, int y) throws RuntimeException;
}
public class Calculator implements ICalculator{
public int add(int x, int y){
return x + y;
}
public float divide(int x, int y) throws RuntimeException{
return (float) x/y;
}
public static void main(String[] args){
int num_1, num_2;
char operator;
Scanner sc = new Scanner(System.in);
num_1 = sc.nextInt();
operator = sc.next().charAt(0);
num_2 = sc.nextInt();
if(num_2 != 0) {
if(operator == '+'){
Calculator addition = new Calculator();
System.out.println(addition.add(num_1,num_2));
}else if(operator == '/'){
try{
Calculator division = new Calculator();
System.out.println(division.divide(num_1,num_2));
}catch(Exception e){
System.out.println("Error");
}
}
else{
System.out.println("Error");
}
}else {
System.out.println("Error");
}
}
}