This lab will give you experience with applying the Command Pattern to a
new system.
The file calculator.zip
contains source code for
a simple calculator with addition, subtraction, multiplication, and a
single memory store. You will add support for the Command pattern, allowing users to
undo and redo typing digits, applying operations, storing results in
memory, and clearing the calculator. The provided code includes a class for
each command to be implemented, but you will need to add the data and make
other changes to support undo and redo.
calculator.zip
contains both
generic code for procesisng commands (similar to the version
in myphonebook.zip
). It
also contains built-in tests using JUnit
; if you have errors
saying "package org.junit does not exist
" then
open CalculatorTest.java
and
select "Add 'JUnit4' to classpath", let IntelliJ download JUnit4 from
maven, and then rebuild the project. When you are finished, you should be
able to right click on CalculatorTest
in the project browser
and execute all of the tests. These are unit tests for just
the Calculator
class, not the (user actions) implemented
in Main
.
As distributed, the code will do simple postfix operations. Multiplying 4 by 5 can be done as
$ java calculator.Main 4,5* Display: 4 Multiply Display: 20 q Quit Final result: 20where the bold text is entered by the user. The
java
command just runs the program from the console; you can
certainly run it from IntelliJ in the usual way. Note the comma (,)
corresponds to the "enter" command; it signals that a complete number
has been entered.
To compute "(18 - 3) * 2" you would enter
18,3-2*Once you have implemented undo and redo, you will be able to add instead of subtract:
$ java calculator.Main 18,3-2* Display: 18 Subtract Display: 15 Multiply Display: 30 u Undo Display: 2 u Undo Display: 15 u Undo Display: 3 + Add Display: 21 2 * Multiply Display: 42 q Quit Final result: 42Note that undoing the multiply (after displaying 30) results in displaying a 2 in the middle of this run; that is so the user knows what number (in this case, a 2) is being applied to the accumulator. This is the behavior you would probably see on a real calculator if that calculator supported undo and redo. If you would like to see a bit more explanation of this example, you might check out a video illustrating it.
Main.java
for the list of commands and their effects.
The commands echo to the screen; on a real calculator, these would be
replaced by (say) lights that show particular operations have been
executed. Several of your changes will be in Main.java
.
History.java
is the History
class from
the phonebook example with
class Command
renamed to CalculatorCommand
. You should
not need to make any changes to History.java
or CalculatorCommand
.
appendDigit
,
the calculator should be in the state
that allows new numbers to be entered (newNumber
should
be true). That is, undoing most operations means that the next
digit starts a new number. Undoing appendDigit
should leave the
next digit state as it was before the appendDigit
command was executed. That is, appendDigit
will need to save
and restore Calculator.newNumber
.
lab7calc
.
calc
). Changing the package will likely break esubmit.
Calculator.java
. esubmit will use its own
version of the file, so if you have made your own changes to the file
then your solution will be unlikely to build.
Main.java
as the Main class. Since there
are ten files, you can click the Add File button enough times to
cover all of the other source files, but a faster way is to
add AppendCommand.java
, then click the Add File button,
select CalculatorCommand.java
and
shift-click TimesCommand.java
. Assuming the files are in
sorted order, this will select all of the
files but Calculator.java
. Main.java
will be listed twice, but that will not cause problems.
The data used by esubmit (inputs and outputs) is available
as test_data.zip