// represent an operato key on a calculator abstract class Operator { abstract int evaluate(int n1, int n2); } class Plus extends Operator{ Plus(){} int evaluate(int n1, int n2) { return n1 + n2; } } class Subtract extends Operator{ Subtract(){} int evaluate(int n1, int n2) { return n1 - n2; } } class Multiply extends Operator{ Multiply(){ } int evaluate(int n1, int n2) { return n1 * n2; } } class Divide extends Operator{ Divide(){} int evaluate(int n1, int n2) { return n1 / n2; } } class Clear extends Operator{ Clear(){} int evaluate(int n1, int n2) { return 0; } } // to repesent a simple calculator class Calculator { int display; //current display on calculator int num1; //current number that's been hit Operator op; //operator int num2; //next number that's been hit // Create a Calculator instance // Initial values are (0, 0, new Plus(), 0) Calculator(int display, int num1, Operator op, int num2) { this.display = display; this.num1 = num1; this.op = op; this.num2 = num2; } // consumes an int and produces a new Calculator Calculator digit(int n){ return new Calculator((this.num2 * 10) + n, this.num1, this.op, (this.num2 * 10) + n); } // consumes an operator and produces a new Calculator Calculator operation(Operator op){ return new Calculator(this.op.evaluate(this.num1, this.num2), this.op.evaluate(this.num1, this.num2), op, 0); } } class Client { Client(){} Operator p = new Plus(); Operator s = new Subtract(); Operator m = new Multiply(); Operator d = new Divide(); Operator clear = new Clear(); boolean testPlus = this.p.evaluate(3, 5) == 8; boolean testSubtract = this.s.evaluate(5, 3) == 2; boolean testMultiply = this.m.evaluate(3, 5) == 15; boolean testDivide = this.d.evaluate(30, 5) == 6; boolean testClear = this.clear.evaluate(3, 5) == 0; boolean testOperators(){ return this.testPlus && this.testSubtract && this.testMultiply && this.testDivide && this.testClear; } Calculator c = new Calculator(0, 0, this.p, 0); boolean testCalc1 = this.c.digit(8).display == 8; boolean testCalc2 = this.c.digit(8).num2 == 8; Calculator c2 = this.c.digit(8); Calculator c3 = this.c2.digit(2); boolean testCalc3 = (this.c3.display == 82) && (this.c3.num1 == 0) && (this.c3.op.equals(this.p)) && (this.c3.num2 == 82); Calculator c4 = this.c2.operation(this.m); boolean testCalc4 = (this.c4.display == 8) && (this.c4.num1 == 8) && (this.c4.op.equals(this.m)) && (this.c4.num2 == 0); Calculator c5 = this.c2.operation(this.p).digit(3).operation(this.p); boolean testCalc5 = (this.c5.display == 11) && (this.c5.num1 == 11) && (this.c5.op.equals(this.p)) && (this.c5.num2 == 0); Calculator c6 = this.c2.operation(this.m).digit(3).digit(4).operation(this.p); boolean testCalc6 = (this.c6.display == 272) && (this.c6.num1 == 272) && (this.c6.op.equals(this.p)) && (this.c6.num2 == 0); boolean calcTestAll = this.testCalc1 && this.testCalc2 && this.testCalc3 && this.testCalc4 && this.testCalc5 && this.testCalc6; /* Run the tests in the Interactions: > Client c = new Client > c.testOperators() > c.calcTestAll */ }