# Homework 3: Stacks and Queues

• Out on: September 29, 2017
• Due by: never
• Collaboration: open-ended

Homework is important practice for you, but it’s not graded so you don’t have to submit it. This particular homework mostly has you playing with stacks by writing a calculator program but you’ll also do some theoretical work on queues.

## Problem 1: Calculating Stacks

You’ll implement a basic RPN calculator that supports integer operands like `1`, `64738`, and `-42` as well as the (binary) integer operators `+`, `-`, `*`, `/`, and `%`. The style of arithmetic expressions our calculator will evaluate is also called a post-fix notation. Stacks are great for doing this job! Your task is to write a driver (client) program that uses our `Stack` interface and one of the given implementations to perform these calculations as specified here.

Your program should be called `Calc` and work as follows:

• The user enters input through `System.in` consisting of operands and operators, presumably in post-fix notation. We have also included some extra operators to get information about results and the current state of the stack.
• If the user enters a valid integer, you push that integer onto the stack.
• If the user enters a valid operator, you pop two integers off the stack, perform the requested operation, and push the result back onto the stack.
• If the user enters the symbol `?` (that’s a question mark), you print the current state of the stack using its `toString` method followed by a new line.
• If the user enters the symbol `^` (that’s a caret), you pop the top element off the stack and print only that element (not the entire stack) followed by a new line.
• If the user enters the symbol `!` (that’s an exclamation mark or bang), you exit the program.

Note that there are a number of error conditions that your program should deal with gracefully! We’ll give you two examples for free, you’ll have to figure out any further error conditions for yourself:

• If the user enters `blah` or `1.5` or anything else that doesn’t make sense for an integer calculator as specified above, your program should make it clear that it can’t do anything helpful with that input; but it should not stop at that point.
• If the user requests an operation for which there are not enough operands on the stack, your program should notify the user of the problem but leave the stack unchanged; again, it should certainly not stop at that point.

Of course this means that you’ll have to print error messages to the user. Error messages must be printed to standard error and not to standard output! (Of course, the regular input and output is done through standard input and standard output as usual.) Furthermore, all error messages must start with the symbol `#` (that’s a hash sign) and be followed by a new line!

### Examples

Here are two examples for interacting with `Calc` that will hopefully help you understand what you’re trying to achieve. First a “slow” example:

``````\$ java Calc
?
[]
10
?
[10]
20 30
?
[30, 20, 10]
*
?
[600, 10]
+
?
[610]
^
610
?
[]
!
\$
``````

Here `\$` is the shell prompt. After starting the program, the first command was `?` to print the stack (which is empty at this point, hence `[]` is the output). Then the user typed `10` followed by `?` and we see that the stack now holds that number: `[10]`. Now the user typed two numbers `20 30` in sequence before hitting return. When we check the stack now using `?` we get the answer `[30, 20, 10]` so obviously the “top” of the stack is to the left. Then we see the `*` operator being typed, which will multiply the top two numbers. We use `?` again to check the result: `[600, 10]`. This is followed by the `+` operator, which will add the top two numbers. Again we check with `?` and get `[610]` as we’d expect. The `^` command prints the same result `610` and pops if off the stack. So the next `?` shows an empty stack again. Finally the user typed the `!` command to quit, returning us to the shell. Here’s the same example, done “fast” this time:

``````\$ java Calc
? 10 ? 20 30 ? * ? + ? ^ ? !
[]
[10]
[30, 20, 10]
[600, 10]
[610]
610
[]
\$
``````

As you can see, if the entire sequence of integers, operators, and commands is entered on a single line, they are all executed in order. It’s like having our own little programming language! Finally, here’s an example for the sample error conditions described above:

``````\$ java Calc
1 2 blah 1.0 3 ?
#Invalid input.
#Invalid input.
[3, 2, 1]
+ + ?
[6]
+ + ?
#Not enough arguments.
#Not enough arguments.
[6]
!
\$
``````

Note in particular that `blah` and `1.0` lead to error messages but are otherwise ignored (the program doesn’t stop); same for the two `+` operations when the stack only has a single element (the program doesn’t even modify the stack in that case).

### Implementation Details and Hints

• You must create an empty `Stack` to hold intermediate results and then repeatedly accept input from the user. It doesn’t matter whether you use the `ArrayStack` or the `ListStack` we provide, what does matter is that the specific type only appears once in your program. (In other words, the type of the stack reference variable you use in your program must be `Stack` and not `ArrayStack` or `ListStack`. Polymorphism!)
• Note that we’re dealing with integers only (type `Integer` in Java) so `/` stands for integer division and `%` stands for integer remainder. Both of these should behave in `Calc` just like they do in Java. The details are messy but worth knowing about, especially regarding modulus.
• You may find it interesting to read up on Autoboxing and Unboxing in Java. It’s the reason we can use our generic `Stack` implementations for `Integer` objects yet still do arithmetic like we would on regular `int` variables.
• Only if you’re not afraid of learning on your own: You’ll be able to use the `matches` method of the `String` class to your advantage when it comes to checking whether a valid operator was entered. (But you can just as well do it with a bunch of separate comparisons or a simple `String` variable containing all the valid operation symbols if you don’t want to learn about regular expressions.)
• Only if you’re not afraid of learning on your own: You can automate the system testing of your calculator program in UNIX using shell scripts and tools like `diff` or `expect`. The details are up to you! We won’t require automated system tests for the rest of the semester, but they can be very handy and cut down on what is otherwise “busywork” to some extent.
• Ensure that the version of your code you hand in does not produce any extraneous debugging output anymore!
• Pay attention to edge cases in the input your classes and programs are expected to handle! For example, make sure that you handle the empty input in a reasonable way.
• Private helper methods are your friends. Your best friends, actually! If you don’t write plenty of them, you’ll have a much harder time getting your code to work.

## Problem 2: The ADT Queue

Develop an algebraic specification for the abstract data type `Queue`. Use `new`, `empty`, `enqueue`, `dequeue`, and `front` (with the meaning of each as discussed in lecture) as your set of operations. Consider unbounded queues only (unless of course you want to learn even more, than do bounded queues as well).

The difficulty is going to be modelling the FIFO (first-in-first-out) behavior accurately. You’ll probably need at least one axiom with a case distinction using an `if` expression; the syntax for this in the `Array` specification for example.

## Artifacts

At minimum, you should have the file `Calc.java` containing your calculator program as well as the various supporting stack and exception classes from Piazza for completeness. All of these should compile without warnings with the `-Xlint:all` flag; there should be no `checkstyle` warnings either with the 601.226 configuration file posted on Piazza. The ADT Queue should go into your `README` file.