Lab 8 Commanding Calculators

This is an individual lab.

Most students are used to calculators which have parentheses so users can control the order of operations. For example, you might enter 3 * (4 + 5) to compute 23. An alternative that was popular decades ago was to use RPN which allowed you to enter the numbers and then apply operations. The HP-35 was one such calculator. See this HP-35 emulator. To use it, click on the 3, ENTER↑, 4, ENTER↑, 5, ×, +, and notice the calculator displays 23.

You are being given an implementation of a similar calculator. For most students, you will get the code through GitHub Classroom, but this code is also available in the file calculator.zip. While this calculator uses RPN, it has other key differences from the HP-35. You will not attempt to emulate the HP-35. Instead, you will extend the provided calculator so it supports undo and redo through the Command pattern.

To get started, check out (or download) the code and open the project in IntelliJ. Attempt to build CalculatorTest. If you have errors saying "package org.junit does not exist", then open CalculatorTest.java (in IntelliJ) and select "Add 'JUnit4' to classpath". This will cause IntelliJ to prompt you to download the library from the Maven Repository; click OK. Once built, right click on CalculatorTest in the project browser and execute all of the tests. These test just the basic calculator operations; they do not test undo and redo. You will test those in other ways.

Once the built-in tests are working, confirm that your calculator works through the console. Run the program and enter the bold-face text as shown below:

           4,5*

      Display: 4
      Multiply
      Display: 20

           q

      Quit
      Final result: 20

To compute "(18 - 3) * 2", enter

      18,3-2*q

Note the comma (,) roughly corresponds to the "ENTER↑" command on the HP-35; it signals that a complete number has been entered. A significant difference between this calculator and the HP-35 is that as values are computed, they are placed in an accumulator rather than on a stack. An operation such as add takes the current accumulator and the displayed value, adds them, displays the value, and stores that value in the accumulator. Thus if you enter **3,4,5* **, the calculator will replace the 3 by a 4 after the second comma, and the final result will be 20.

So you have an idea of where you are headed, the following shows what happens if the user decides to add instead of subtract and then goes on to do other operations:

           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.

Detailed Requirements

How to get started

  1. Start by getting the calculator tests to pass. You should not need to write any code, but you may need to refine your project setup.
  2. Implement the execute and unexecute for the add command. The execute operation will be very similar to the code that is in Main.java, and implementing unexecute will boil down to figuring out what data to save in execute and restoring the appropriate items in the unexecute.
  3. Revise Main.java to use the add command rather than simply adding values. Make sure you can handle an input sequence like 3,4+ud.
  4. Implement the other operations (-, *, /) in the same way, and test this portion.
  5. Implement the remainder of the system.

Submitting

Test Data

The data used for testing (inputs and outputs) is available as test_data.zip