Spaces:
Running
Running
File size: 3,601 Bytes
fe7addf | 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | 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
|