SE 2811, Lab 7
Commanding Calculators

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: 20
where 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: 42
Note 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.

Additional Requirements

Test Data

The data used by esubmit (inputs and outputs) is available as test_data.zip