Skip to content

Commit b04f7b7

Browse files
authored
Merge pull request #8 from formidablae/1-what-are-classes-objects-and-methods/1.3-a-basic-calculator-project
Did 1 What are Classes Objects and Methods / 13 A Basic Calculator Project
2 parents 46f31a8 + 0a1369a commit b04f7b7

File tree

1 file changed

+53
-0
lines changed
  • 1_What_are_Classes_Objects_and_Methods/1.3_A_Basic_Calculator_Project

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Calculator {
2+
public Calculator() {
3+
4+
}
5+
// Simple arithmetic calculator.
6+
public int add(int a, int b) {
7+
int sum;
8+
sum = a + b;
9+
return sum;
10+
}
11+
12+
public int subtract(int c, int d) {
13+
int diff;
14+
diff = c - d;
15+
return diff;
16+
}
17+
18+
public int multiply(int e, int f) {
19+
int prod;
20+
prod = e * f;
21+
return prod;
22+
}
23+
24+
public int divide(int g, int h) {
25+
int resu;
26+
if(h == 0) {
27+
System.out.println("Error! Dividing by zero is not allowed.");
28+
return 0;
29+
}
30+
else {
31+
resu = g / h;
32+
}
33+
return resu;
34+
}
35+
36+
public int modulo(int i, int j) {
37+
int modu;
38+
if(j == 0) {
39+
System.out.println("Error! Dividing by zero is not allowed.");
40+
return 0;
41+
}
42+
else {
43+
modu = i % j;
44+
return modu;
45+
}
46+
}
47+
48+
public static void main(String[] args) {
49+
Calculator myCalculator = new Calculator();
50+
System.out.println(myCalculator.add(5, 7));
51+
System.out.println(myCalculator.subtract(45, 11));
52+
}
53+
}

0 commit comments

Comments
 (0)