-
Notifications
You must be signed in to change notification settings - Fork 30
/
Calculator.java
67 lines (67 loc) · 1.3 KB
/
Calculator.java
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
class Calculator
{
float num1,num2;
public void add(float num1,float num2)
{
this.num1=num1;
this.num2=num2;
System.out.println(+num1+"+"+num2+"="+(num1+num2));
}
public void sub(float num1,float num2)
{
this.num1=num1;
this.num2=num2;
System.out.println(+num1+"-"+num2+"="+(num1-num2));
}
public void mul(float num1,float num2)
{
this.num1=num1;
this.num2=num2;
System.out.println(+num1+"*"+num2+"="+(num1*num2));
}
public void div(float num1,float num2)
{
this.num1=num1;
this.num2=num2;
System.out.println(+num1+"/"+num2+"="+(num1/num2));
}
public void mod(float num1,float num2)
{
this.num1=num1;
this.num2=num2;
System.out.println(+num1+"%"+num2+"="+(num1%num2));
}
}
class Calc extends Calculator
{
public static void main(String[] args)
{
Calculator ob =new Calculator();
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers: ");
float first = sc.nextFloat();
float second = sc.nextFloat();
System.out.println("Select your choice \n 1)Addition\n2)Subtraction\n3)Multiplication\n4)Division\n5)Modulo");
int choice = sc.nextInt();
switch(choice)
{
case 1:
ob.add(first,second);
break;
case 2:
ob.sub(first,second);
break;
case 3:
ob.mul(first,second);
break;
case 4:
ob.div(first,second);
break;
case 5:
ob.mod(first,second);
break;
default:
System.out.printf("Error! operator is not correct");
}
}
}