Spaces:
Running
Running
Create 11B. Write class Calculator
Browse files
Week 4: Writing classes/11B. Write class Calculator
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the class Calculator. The class must have the following properties:
|
| 2 |
+
Private integer type attribute result
|
| 3 |
+
a void type method add(int number), which adds the given number to the result
|
| 4 |
+
Methods subtract(int number) and multiply(int number) that work in a similar way.
|
| 5 |
+
The method inverse(), which converts the result to the inverse (e.g. 3 is converted to -3)
|
| 6 |
+
Get method getResult(), which returns the current result
|
| 7 |
+
|
| 8 |
+
Note! Since the test class is public, the class Calculator cannot be. So do not use the attribute public in its definition.
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
import java.util.Random;
|
| 15 |
+
import java.lang.reflect.Field;
|
| 16 |
+
|
| 17 |
+
public class Test{
|
| 18 |
+
public static void main(String[] args){
|
| 19 |
+
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
class Calculator {
|
| 24 |
+
|
| 25 |
+
private int result;
|
| 26 |
+
|
| 27 |
+
// removed 'static'
|
| 28 |
+
// 'static' means the method belongs to the class, not an object instance of the class
|
| 29 |
+
// removing 'static' allows us to use 'this' to access instance variables/attributes
|
| 30 |
+
public void add(int number) {
|
| 31 |
+
this.result += number;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
public void subtract(int number) {
|
| 35 |
+
this.result -= number;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
public void multiply(int number) {
|
| 39 |
+
this.result *= number;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
public void inverse() {
|
| 43 |
+
this.result = -1 * this.result;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
public int getResult() {
|
| 47 |
+
return this.result;
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
Testing class Calculator...
|
| 58 |
+
Constructor Calculator() found, object created!
|
| 59 |
+
|
| 60 |
+
Checking attribute...
|
| 61 |
+
Attribute result found!
|
| 62 |
+
Attribute type: int
|
| 63 |
+
Attribute private: true
|
| 64 |
+
Testing methods....
|
| 65 |
+
Calling add(12)
|
| 66 |
+
Result is now: 12
|
| 67 |
+
Calling add(2)
|
| 68 |
+
Result is now: 14
|
| 69 |
+
Calling add(12)
|
| 70 |
+
Result is now: 26
|
| 71 |
+
|
| 72 |
+
Calling subtract(3)
|
| 73 |
+
Result is now: 23
|
| 74 |
+
Calling subtract(23)
|
| 75 |
+
Result is now: 0
|
| 76 |
+
Calling subtract(12)
|
| 77 |
+
Result is now: -12
|
| 78 |
+
|
| 79 |
+
Calling multiply(-1)
|
| 80 |
+
Result is now: 12
|
| 81 |
+
Calling multiply(-1)
|
| 82 |
+
Result is now: -12
|
| 83 |
+
Calling multiply(6)
|
| 84 |
+
Result is now: -72
|
| 85 |
+
|
| 86 |
+
Calling inverse()
|
| 87 |
+
Result is now: 72
|
| 88 |
+
Calling inverse()
|
| 89 |
+
Result is now: -72
|
| 90 |
+
|
| 91 |
+
|