-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
67 lines (64 loc) · 1.59 KB
/
Calculator.java
File metadata and controls
67 lines (64 loc) · 1.59 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
package Rough;
import java.util.Scanner;
public class Calculator {
double a,b;
Calculator(int a, int b){
this.a=a;
this.b=b;
}
public void multipy(){
double M = this.a*this.b;
System.out.println("Result:");
System.out.println(this.a + "*" + this.b + " = "+ M);
}
public void divide() {
double D = this.a/this.b;
System.out.println("Result:");
System.out.println(this.a + "%" + this.b + " = "+ D);
}
public void addition() {
double A = this.a+this.b;
System.out.println("Result:");
System.out.println(this.a + "+" + this.b + " = "+ A);
}
public void subtraction() {
if(this.a>this.b) {
double S = this.a-this.b;
System.out.println("Result:");
System.out.println(this.a + "-" + this.b + " = "+ S);
}else if(this.b>this.a) {
double S = this.b-this.a;
System.out.println("Result:");
System.out.println(this.b + "-" + this.a + " = "+ S);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
System.out.println("");
Calculator myObj = new Calculator(sc.nextInt(),sc.nextInt());
System.out.print("Enter the arithmetic symbol to be executed on the given numbers: ");
System.out.println("+ or - or % or *");
System.out.println("");
Scanner symbol = new Scanner(System.in);
String sym = symbol.nextLine();
String w = "+";
String x = "-";
String y = "%";
String z = "*";
symbol.close();
sc.close();
if (sym.equals(w) ) {
myObj.addition();
}
else if(sym.equals(x)) {
myObj.subtraction();
}
else if(sym.equals(y)) {
myObj.divide();
}
else if(sym.equals(z)) {
myObj.multipy();
}
}
}