Homework 3: Stacks and Queues

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:

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:

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

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.