KaiquanMah's picture
class Class1 implements Interface1 {...}
f4eefc6 verified
raw
history blame
1.97 kB
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!