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 tests cover just the core system
without undo and redo.
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. 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.
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.
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
; if you do,
your version will be overwritten by the standard version. The
assumption is that you are not making any changes to that file.
The data used by esubmit (inputs and outputs) is available
as test_data.zip