Spaces:
Running
Running
File size: 1,969 Bytes
f4eefc6 | 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | The class hierarchy is defined in the UML diagram below.
Implement the classes as per the diagram.
The functionality of the methods is not tested; definitions are important.
However, the code must compile.
https://ville.utu.fi/static_resources/jubery_utufi/calculator_calculatorMachine_UML.png
<interface>
Calculator
+getResult(): double
^
-
|
|
|
|
|
CalculatorMachine
-result: double
+CalculatorMachine (result: double)
+calculate(operation: String, parameter: double): void
+getResult(); double
====================================================
import java.util.Random;
import java.lang.reflect.Field;
public class Test{
public static void main(String[] args){
final Random r = new Random();
}
}
//ADD
interface Calculator {
public double getResult();
}
//ADD
class CalculatorMachine implements Calculator {
private double result;
public CalculatorMachine(double result) {
this.result = result;
}
public void calculate(String operation, double parameter) {
// turn String operation into math operation
if (operation == "+") {
this.result = this.result + parameter;
}
else if (operation == "-") {
this.result = this.result - parameter;
}
else if (operation == "*") {
this.result = this.result * parameter;
}
else if (operation == "/") {
this.result = this.result / parameter;
}
else if (operation == "%") {
this.result = this.result % parameter;
}
}
@Override
public double getResult() {
return this.result;
}
}
Testing the class CalculatorMachine...
Constructor found, CalculatorMachine object created!
Class implements the Calculator interface!
Checking attributes...
Attribute result found!
Attribute type: double
Attribute private: true
Checking methods....
getResult defined in the Calculator interface!
getResult works!
calculate works!
|