While tidying up his house during the recent recess week, your lecturer found an old fx-series CASIO calculator which he has not been using since his school days. Fiddling with the calculator inspired him to define the problem statement for this practical assessment...
Your task is to design a software using OOP modeling to simulate the use of a calculator. For example, to caculate 1 + (2 × 3), we need to press a series of buttons, with corresponding changes to the LCD screen display.
| Button being pressed: | [AC] | [1] | [+] | [2] | [×] | [3] | [=] |
| Display after pressing button: | [0] | [1] | [1] | [2] | [2] | [3] | [7] |
We can design a calculation pipeline to represent the changes:
jshell> new Calc() // represents pressing [AC] $.. ==> [0] jshell> new Calc().val(1) $.. ==> [1] jshell> new Calc().val(1).operate(new Add()) $.. ==> [1] jshell> new Calc().val(1).operate(new Add()).val(2) $.. ==> [2] jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()) $.. ==> [2] jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()).val(3) $.. ==> [3] jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()).val(3).equal() $.. ==> [7]
This task comprises a number of levels. You are required to complete ALL levels.
The following are the constraints imposed on this task. In general, you should keep to the constructs and programming discipline instilled throughout the module.
In the following level specifications, you will be guided on the classes and non-private methods to define.
We first need to represent values in a calculator. Define the Value class to represent a value. In this task, we shall only be using integers as values.
The following is a sample run:
$ javac your_java_files $ jshell Value.java jshell> new Value(0) $.. ==> [0] jshell> new Value(0).val(1) $.. ==> [1] jshell> new Value(12) $.. ==> [12] jshell> new Value(0).val(1).val(2) $.. ==> [12] jshell> new Value(12).val(1) $.. ==> [121] jshell> new Value(12).val(1).val(2) $.. ==> [1212] jshell> new Value(12).val(1).val(2).val(0) $.. ==> [12120] jshell> new Value(-12).val(1).val(2).val(0) $.. ==> [-12120]
You may assume that the method val only takes in integer between 1 and 9 (both inclusive), while the constructor takes in any integer.
You are given the following Operator abstract class to support operations that you will define in the Add, Sub and Mul classes later. In this assessment, we will not perform division due to the possibility of a divide-by-zero error.
abstract class Operator {
abstract int operate(int x, int y);
}
Include the method operate in the Value class that takes in an Operator followed by another Value. The following example demonstrates the use of addition Add and subtraction Sub operators.
$ javac your_java_files $ jshell your_java_files_in_bottom-up_dependency_order jshell> new Value(1).val(2).operate(new Add(), new Value(2).val(3)) $.. ==> [35] jshell> new Value(1).val(2).operate(new Sub(), new Value(2).val(3)) $.. ==> [-11] jshell> new Value(1).val(2).operate(new Sub(), new Value(2).val(3)). ...> operate(new Add(), new Value(3).val(4)) $.. ==> [23]
Now implement the Mul operator to multiply two values. Ensure that you test the correctness of this operator.
We are now ready to implement the calculator. To make it manageable, we shall implement the calculator in stages, starting with only one operation.
$ javac your_java_files $ jshell your_java_files_in_bottom-up_dependency_order jshell> new Calc() // represents pressing [AC] $.. ==> [0] jshell> new Calc().val(1) // pressing [1] $.. ==> [1] jshell> new Calc().val(1).val(2) // pressing [2] $.. ==> [12] jshell> new Calc().val(1).val(2).operate(new Add()) // pressing [+] $.. ==> [12] jshell> new Calc().val(1).val(2).operate(new Add()).val(3) // pressing [3] $.. ==> [3] jshell> new Calc().val(1).val(2).operate(new Add()).val(3).val(4) // pressing [4] $.. ==> [34] jshell> new Calc().val(1).val(2).operate(new Add()).val(3).val(4).equal() // pressing [=] $.. ==> [46]
The above represents:
| Button being pressed: | [AC] | [1] | [2] | [+] | [3] | [4] | [=] |
| Display after pressing button: | [0] | [1] | [12] | [12] | [3] | [34] | [46] |
Here are some implementation details to take note:
jshell> new Calc().val(1).val(2) instanceof Calc $.. ==> true
jshell> new Calc().val(1).val(2).operate(new Add()) instanceof CalcOp $.. ==> true
This is to get ready for the val method to accumulate digits for the right operand.
jshell> new Calc().val(1).val(2).operate(new Add()).val(3) instanceof CalcEval $.. ==> true jshell> new Calc().val(1).val(2).operate(new Add()).val(3).val(4) instanceof CalcEval $.. ==> true
jshell> new Calc().val(1).val(2).operate(new Add()).val(3).val(4).equal() instanceof Result $.. ==> trueThis ensures that no further methods can be called, hence terminating the pipeline.
Note that the classes Calc, CalcOp, CalcEval and Result are only meant to guide you to the eventual calculator implementation. As such, they can be standalone classes with the only dependencies.
Calc --> CalcOp --> CalcEval --> Result
Most importantly, you need to ensure that there should not be any cyclic dependencies among all classes.
Moreover, you should also test your implementation with single multiply and subtract operations as shown below:
jshell> new Calc().operate(new Sub()).val(1).equal() $.. ==> [-1] jshell> new Calc().val(2).operate(new Mul()).val(3).equal() $.. ==> [6]
Now that we have a working calculation pipeline involving one operator, it is time to include more operations.
Within the CalcEval class, include the operate method that returns a Calc2 object. Any subsequent invocations of val, operate or equal methods will only result in other Calc2 objects.
Note that Calc2 should only have properties that are typed Value, Operator, Optional<Value> or Optional<Operator>. You are also not allowed to create any other utility classes (like Pair, Triple, etc.) other than those specified.
In order to operate on mixed operators in the correct order, include a precedence() method in the Operator that returns an integer representing the precedence value of the operator (lower number with higher precedence).
jshell> new Mul().precedence() $.. ==> 1 jshell> new Add().precedence() $.. ==> 2 jshell> new Sub().precedence() $.. ==> 2
Now ensure that your operators are comparable. In the sample run below, since Mul is higher in precedence, compareTo should return a positive integer.
jshell> new Add().compareTo(new Mul()) > 0 $.. ==> true
Henceforth, when writing all other classes, you should only rely on compareTo to ascertain the precedence of operators and not access precedence() directly.
Let us now focus on the calculation pipeline. Suppose the following buttons are pressed:
[AC][1][+][2][×][3][=]
The following sample run depicts the above calculation pipeline at every stage.
jshell> new Calc() $.. ==> [0] jshell> new Calc().val(1) $.. ==> [1] jshell> new Calc().val(1).operate(new Add()) $.. ==> [1] jshell> new Calc().val(1).operate(new Add()).val(2) $.. ==> [2] jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()) $.. ==> [2] jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()).val(3) $.. ==> [3] jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()).val(3).equal() $.. ==> [7]
Notice that multiply is performed first due to the higher precedence.
Now what happens if instead of pressing [=] above, we press [+] or [×] instead?
jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()).val(3). ...> operate(new Add()) $.. ==> [7] jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()).val(3). ...> operate(new Mul()) $.. ==> [6]
Here is an explanation:
| Button being pressed: | [AC] | [1] | [+] | [2] | [×] | [3] | [+] |
| Display after pressing button: | [0] | [1] | [1] | [2] | [2] | [3] | [7] |
| Button being pressed: | [AC] | [1] | [+] | [2] | [×] | [3] | [×] |
| Display after pressing button: | [0] | [1] | [1] | [2] | [2] | [3] | [6] |
Below are some more concrete examples:
jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()).val(3). ...> operate(new Add()).val(4).equal() // 1 + 2 * 3 + 4 $.. ==> [11] jshell> new Calc().val(1).operate(new Add()).val(2).operate(new Mul()).val(3). ...> operate(new Mul()).val(4).equal() // 1 + 2 * 3 * 4 $.. ==> [25]
You are reminded to ensure that the calculation pipeline works with the subtraction operator.
Having been guided on defining the Calc class right up to the Calc2, you may soon realize that the Calc2 class is sufficient for us to perform any calculation. All you need to do for this level is to create a Calc2 constructor to start the pipeline. In essence, whatever works for a calculation pipeline starting with new Calc(), should also work if the pipeline starts with new Calc2().
Here are some examples:
jshell> new Calc2().val(1).operate(new Add()).val(2).operate(new Mul()).val(3). ...> operate(new Add()).val(4).equal() $.. ==> [11] jshell> new Calc2().val(1).operate(new Add()).val(2).operate(new Mul()).val(3). ...> operate(new Mul()).val(4).equal() $.. ==> [25]
Every stage of the pipeline is now a Calc2 object. Consequently, it is now possible to invoke val, operate and equal at any stage of the pipeline. The following are some sample pipelines:
jshell> new Calc2().equal() $.. ==> [0] jshell> new Calc2().operate(new Add()).equal() $.. ==> [0] jshell> new Calc2().operate(new Add()).equal().equal() $.. ==> [0] jshell> new Calc2().operate(new Add()).equal().equal().val(1) $.. ==> [1] jshell> new Calc2().val(2).operate(new Add()).operate(new Mul()).val(3).equal() $.. ==> [6] jshell> new Calc2().val(1).operate(new Add()).val(2).operate(new Mul()).val(3). ...> equal(). // note the [=] that calculates 1+(2*3) = 7 ...> operate(new Mul()).val(4).equal() $.. ==> [28] jshell> new Calc2().val(1).operate(new Add()).val(2). ...> operate(new Add()).operate(new Add()). // replacing with same operator ...> val(3).equal() $.. ==> [6]
The following are some interesting examples involving switching operations:
jshell> new Calc2().val(1).operate(new Add()).val(2).operate(new Mul()).val(3). ...> equal() // expected behaviour 1+(2*3) $.. ==> [7] jshell> new Calc2().val(1).operate(new Add()).val(2). // 1 + 2 ...> operate(new Add()). // 3 + ...> operate(new Mul()). // changed to 3 * ...> val(3).equal() // 3 * 3 $.. ==> [9] jshell> new Calc2().val(1).operate(new Add()).val(2). // 1 + 2 ...> operate(new Mul()). // 1 + 2 * ...> operate(new Add()). // changed to 3 + ...> val(3).equal() // 3 + 3 $.. ==> [6] jshell> new Calc2().val(1).operate(new Add()).val(2). // 1 + 2 ...> operate(new Mul()). // 1 + 2 * ...> operate(new Add()). // changed to 3 + ...> operate(new Mul()). // changed to 3 * ...> val(3).equal() // 3 * 3 $.. ==> [9]