CoolFace
Apppublic

KaiquanMah/TurkuBasicOOPinJava

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
06. The Calculator implements an interface151 linesDownload Raw Back to Week 6: Methods of OO Programming
1Now write a class 'Calculator' that implements the interface 'CalculatorInterface' written in the previous task.2 3The class should have a CONSTRUCTOR that initializes the calculator to its initial state (result is 0.0).4 5Additionally, the class has the methods add, subtract, multiply, and divide, 6coming from the interface, which all change the result in memory as their names suggest.7 8The method getResult() returns the result in memory.9 10 11 12 13Example of using the class:14Calculator calc = new Calculator();15System.out.println(calc.getResult());16 17calc.add(10);18System.out.println(calc.getResult());19 20calc.subtract(5);21System.out.println(calc.getResult());22 23calc.multiply(3);24System.out.println(calc.getResult());25 26calc.divide(2);27System.out.println(calc.getResult());28 29 30The program prints:310.03210.0335.03415.0357.536 37 38 39 40 41import java.util.Random;42 43public class Test {44    public static void main(String[] args) {45        final Random r = new Random();46        47        48        System.out.println("Testing the class Calculator...");49        50        CalculatorInterface calc = new Calculator();51        52        System.out.println("The class implements the interface CalculatorInterface!");53        54        System.out.println("Testing addition...");55        int[] numbers = {2, 4, 3, 7};56        for (int number : numbers) {57            calc.add(number);58            System.out.println("Added " + number + ", now the result is " + calc.getResult());59        }60        61        System.out.println("Testing subtraction...");62        numbers = new int[]{2, 1, 3, 2};63        for (int number : numbers) {64            calc.subtract(number);65            System.out.println("Subtracted " + number + ", now the result is " + calc.getResult());66        }67        68        System.out.println("Testing multiplication...");69        numbers = new int[]{2, 1, 3};70        for (int number : numbers) {71            calc.multiply(number);72            System.out.println("Multiplied by " + number + ", now the result is " + calc.getResult());73        }74        75        System.out.println("Testing division...");76        numbers = new int[]{2, 2, 5};77        for (int number : numbers) {78            calc.divide(number);79            System.out.println("Divided by " + number + ", now the result is " + calc.getResult());80        }81    }82}83 84 85//ADD86class Calculator implements CalculatorInterface {87    private double result;88    89    public Calculator() {90        this.result = 0.0;91    }92 93    @Override94    public void add(int number) {95        this.result += number;96    }97 98    @Override99    public void subtract(int number) {100        this.result -= number;101    }102 103    @Override104    public void multiply(int number) {105        this.result *= number;106    }107 108    @Override109    public void divide(int number) {110        this.result /= number;111    }112 113    @Override114    public double getResult() {115        return this.result;116    }117}118 119 120 121 122Testing the class Calculator...123The class implements the interface CalculatorInterface!124Testing addition...125Added 2, now the result is 2.0126Added 4, now the result is 6.0127Added 3, now the result is 9.0128Added 7, now the result is 16.0129 130Testing subtraction...131Subtracted 2, now the result is 14.0132Subtracted 1, now the result is 13.0133Subtracted 3, now the result is 10.0134Subtracted 2, now the result is 8.0135 136Testing multiplication...137Multiplied by 2, now the result is 16.0138Multiplied by 1, now the result is 16.0139Multiplied by 3, now the result is 48.0140 141Testing division...142Divided by 2, now the result is 24.0143Divided by 2, now the result is 12.0144Divided by 5, now the result is 2.4145 146 147 148 149 150 151