TurkuBasicOOPinJava / Week 6: Methods of OO Programming /06. The Calculator implements an interface
KaiquanMah's picture
@Override public void add(int number) {this.result += number;}
fe7addf verified
Now write a class 'Calculator' that implements the interface 'CalculatorInterface' written in the previous task.
The class should have a CONSTRUCTOR that initializes the calculator to its initial state (result is 0.0).
Additionally, the class has the methods add, subtract, multiply, and divide,
coming from the interface, which all change the result in memory as their names suggest.
The method getResult() returns the result in memory.
Example of using the class:
Calculator calc = new Calculator();
System.out.println(calc.getResult());
calc.add(10);
System.out.println(calc.getResult());
calc.subtract(5);
System.out.println(calc.getResult());
calc.multiply(3);
System.out.println(calc.getResult());
calc.divide(2);
System.out.println(calc.getResult());
The program prints:
0.0
10.0
5.0
15.0
7.5
import java.util.Random;
public class Test {
public static void main(String[] args) {
final Random r = new Random();
System.out.println("Testing the class Calculator...");
CalculatorInterface calc = new Calculator();
System.out.println("The class implements the interface CalculatorInterface!");
System.out.println("Testing addition...");
int[] numbers = {2, 4, 3, 7};
for (int number : numbers) {
calc.add(number);
System.out.println("Added " + number + ", now the result is " + calc.getResult());
}
System.out.println("Testing subtraction...");
numbers = new int[]{2, 1, 3, 2};
for (int number : numbers) {
calc.subtract(number);
System.out.println("Subtracted " + number + ", now the result is " + calc.getResult());
}
System.out.println("Testing multiplication...");
numbers = new int[]{2, 1, 3};
for (int number : numbers) {
calc.multiply(number);
System.out.println("Multiplied by " + number + ", now the result is " + calc.getResult());
}
System.out.println("Testing division...");
numbers = new int[]{2, 2, 5};
for (int number : numbers) {
calc.divide(number);
System.out.println("Divided by " + number + ", now the result is " + calc.getResult());
}
}
}
//ADD
class Calculator implements CalculatorInterface {
private double result;
public Calculator() {
this.result = 0.0;
}
@Override
public void add(int number) {
this.result += number;
}
@Override
public void subtract(int number) {
this.result -= number;
}
@Override
public void multiply(int number) {
this.result *= number;
}
@Override
public void divide(int number) {
this.result /= number;
}
@Override
public double getResult() {
return this.result;
}
}
Testing the class Calculator...
The class implements the interface CalculatorInterface!
Testing addition...
Added 2, now the result is 2.0
Added 4, now the result is 6.0
Added 3, now the result is 9.0
Added 7, now the result is 16.0
Testing subtraction...
Subtracted 2, now the result is 14.0
Subtracted 1, now the result is 13.0
Subtracted 3, now the result is 10.0
Subtracted 2, now the result is 8.0
Testing multiplication...
Multiplied by 2, now the result is 16.0
Multiplied by 1, now the result is 16.0
Multiplied by 3, now the result is 48.0
Testing division...
Divided by 2, now the result is 24.0
Divided by 2, now the result is 12.0
Divided by 5, now the result is 2.4