KaiquanMah commited on
Commit
f4eefc6
·
verified ·
1 Parent(s): f4649dd

class Class1 implements Interface1 {...}

Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/16. Implement a Class Hierarchy ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The class hierarchy is defined in the UML diagram below.
2
+
3
+ Implement the classes as per the diagram.
4
+ The functionality of the methods is not tested; definitions are important.
5
+
6
+ However, the code must compile.
7
+ https://ville.utu.fi/static_resources/jubery_utufi/calculator_calculatorMachine_UML.png
8
+
9
+
10
+
11
+ <interface>
12
+ Calculator
13
+ +getResult(): double
14
+
15
+
16
+ ^
17
+ -
18
+ |
19
+ |
20
+ |
21
+ |
22
+ |
23
+
24
+
25
+ CalculatorMachine
26
+ -result: double
27
+
28
+ +CalculatorMachine (result: double)
29
+ +calculate(operation: String, parameter: double): void
30
+ +getResult(); double
31
+
32
+
33
+ ====================================================
34
+
35
+ import java.util.Random;
36
+ import java.lang.reflect.Field;
37
+
38
+ public class Test{
39
+ public static void main(String[] args){
40
+ final Random r = new Random();
41
+
42
+ }
43
+ }
44
+
45
+
46
+
47
+ //ADD
48
+ interface Calculator {
49
+ public double getResult();
50
+ }
51
+
52
+
53
+ //ADD
54
+ class CalculatorMachine implements Calculator {
55
+ private double result;
56
+
57
+ public CalculatorMachine(double result) {
58
+ this.result = result;
59
+ }
60
+
61
+ public void calculate(String operation, double parameter) {
62
+ // turn String operation into math operation
63
+ if (operation == "+") {
64
+ this.result = this.result + parameter;
65
+ }
66
+ else if (operation == "-") {
67
+ this.result = this.result - parameter;
68
+ }
69
+ else if (operation == "*") {
70
+ this.result = this.result * parameter;
71
+ }
72
+ else if (operation == "/") {
73
+ this.result = this.result / parameter;
74
+ }
75
+ else if (operation == "%") {
76
+ this.result = this.result % parameter;
77
+ }
78
+ }
79
+
80
+ @Override
81
+ public double getResult() {
82
+ return this.result;
83
+ }
84
+
85
+
86
+ }
87
+
88
+
89
+
90
+
91
+
92
+ Testing the class CalculatorMachine...
93
+ Constructor found, CalculatorMachine object created!
94
+ Class implements the Calculator interface!
95
+
96
+ Checking attributes...
97
+ Attribute result found!
98
+ Attribute type: double
99
+ Attribute private: true
100
+
101
+ Checking methods....
102
+ getResult defined in the Calculator interface!
103
+ getResult works!
104
+ calculate works!
105
+
106
+
107
+