Spaces:
Running
Running
@Override public void add(int number) {this.result += number;}
Browse files
Week 6: Methods of OO Programming/06. The Calculator implements an interface
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Now write a class 'Calculator' that implements the interface 'CalculatorInterface' written in the previous task.
|
2 |
+
|
3 |
+
The class should have a CONSTRUCTOR that initializes the calculator to its initial state (result is 0.0).
|
4 |
+
|
5 |
+
Additionally, the class has the methods add, subtract, multiply, and divide,
|
6 |
+
coming from the interface, which all change the result in memory as their names suggest.
|
7 |
+
|
8 |
+
The method getResult() returns the result in memory.
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
Example of using the class:
|
14 |
+
Calculator calc = new Calculator();
|
15 |
+
System.out.println(calc.getResult());
|
16 |
+
|
17 |
+
calc.add(10);
|
18 |
+
System.out.println(calc.getResult());
|
19 |
+
|
20 |
+
calc.subtract(5);
|
21 |
+
System.out.println(calc.getResult());
|
22 |
+
|
23 |
+
calc.multiply(3);
|
24 |
+
System.out.println(calc.getResult());
|
25 |
+
|
26 |
+
calc.divide(2);
|
27 |
+
System.out.println(calc.getResult());
|
28 |
+
|
29 |
+
|
30 |
+
The program prints:
|
31 |
+
0.0
|
32 |
+
10.0
|
33 |
+
5.0
|
34 |
+
15.0
|
35 |
+
7.5
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
import java.util.Random;
|
42 |
+
|
43 |
+
public class Test {
|
44 |
+
public static void main(String[] args) {
|
45 |
+
final Random r = new Random();
|
46 |
+
|
47 |
+
|
48 |
+
System.out.println("Testing the class Calculator...");
|
49 |
+
|
50 |
+
CalculatorInterface calc = new Calculator();
|
51 |
+
|
52 |
+
System.out.println("The class implements the interface CalculatorInterface!");
|
53 |
+
|
54 |
+
System.out.println("Testing addition...");
|
55 |
+
int[] numbers = {2, 4, 3, 7};
|
56 |
+
for (int number : numbers) {
|
57 |
+
calc.add(number);
|
58 |
+
System.out.println("Added " + number + ", now the result is " + calc.getResult());
|
59 |
+
}
|
60 |
+
|
61 |
+
System.out.println("Testing subtraction...");
|
62 |
+
numbers = new int[]{2, 1, 3, 2};
|
63 |
+
for (int number : numbers) {
|
64 |
+
calc.subtract(number);
|
65 |
+
System.out.println("Subtracted " + number + ", now the result is " + calc.getResult());
|
66 |
+
}
|
67 |
+
|
68 |
+
System.out.println("Testing multiplication...");
|
69 |
+
numbers = new int[]{2, 1, 3};
|
70 |
+
for (int number : numbers) {
|
71 |
+
calc.multiply(number);
|
72 |
+
System.out.println("Multiplied by " + number + ", now the result is " + calc.getResult());
|
73 |
+
}
|
74 |
+
|
75 |
+
System.out.println("Testing division...");
|
76 |
+
numbers = new int[]{2, 2, 5};
|
77 |
+
for (int number : numbers) {
|
78 |
+
calc.divide(number);
|
79 |
+
System.out.println("Divided by " + number + ", now the result is " + calc.getResult());
|
80 |
+
}
|
81 |
+
}
|
82 |
+
}
|
83 |
+
|
84 |
+
|
85 |
+
//ADD
|
86 |
+
class Calculator implements CalculatorInterface {
|
87 |
+
private double result;
|
88 |
+
|
89 |
+
public Calculator() {
|
90 |
+
this.result = 0.0;
|
91 |
+
}
|
92 |
+
|
93 |
+
@Override
|
94 |
+
public void add(int number) {
|
95 |
+
this.result += number;
|
96 |
+
}
|
97 |
+
|
98 |
+
@Override
|
99 |
+
public void subtract(int number) {
|
100 |
+
this.result -= number;
|
101 |
+
}
|
102 |
+
|
103 |
+
@Override
|
104 |
+
public void multiply(int number) {
|
105 |
+
this.result *= number;
|
106 |
+
}
|
107 |
+
|
108 |
+
@Override
|
109 |
+
public void divide(int number) {
|
110 |
+
this.result /= number;
|
111 |
+
}
|
112 |
+
|
113 |
+
@Override
|
114 |
+
public double getResult() {
|
115 |
+
return this.result;
|
116 |
+
}
|
117 |
+
}
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
Testing the class Calculator...
|
123 |
+
The class implements the interface CalculatorInterface!
|
124 |
+
Testing addition...
|
125 |
+
Added 2, now the result is 2.0
|
126 |
+
Added 4, now the result is 6.0
|
127 |
+
Added 3, now the result is 9.0
|
128 |
+
Added 7, now the result is 16.0
|
129 |
+
|
130 |
+
Testing subtraction...
|
131 |
+
Subtracted 2, now the result is 14.0
|
132 |
+
Subtracted 1, now the result is 13.0
|
133 |
+
Subtracted 3, now the result is 10.0
|
134 |
+
Subtracted 2, now the result is 8.0
|
135 |
+
|
136 |
+
Testing multiplication...
|
137 |
+
Multiplied by 2, now the result is 16.0
|
138 |
+
Multiplied by 1, now the result is 16.0
|
139 |
+
Multiplied by 3, now the result is 48.0
|
140 |
+
|
141 |
+
Testing division...
|
142 |
+
Divided by 2, now the result is 24.0
|
143 |
+
Divided by 2, now the result is 12.0
|
144 |
+
Divided by 5, now the result is 2.4
|
145 |
+
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
|
150 |
+
|