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.
You’ll implement a basic
calculator that supports integer operands like
as well as the (binary) integer operators
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
interface and one of the given implementations to perform these calculations
as specified here.
Your program should be called
Calc and work as follows:
System.inconsisting 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.
?(that’s a question mark), you print the current state of the stack using its
toStringmethod followed by a new line.
^(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.
!(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:
1.5or 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.
Of course this means that you’ll have to print error messages to the
Error messages must be printed to standard error and not to
(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
a hash sign) and be followed by a new line!
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 ?  20 30 ? [30, 20, 10] * ? [600, 10] + ?  ^ 610 ?  ! $
$ is the shell prompt. After starting the program, the first command
? 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:
. Now the user typed two numbers
20 30 in
sequence before hitting return. When we check the stack now using
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
? and get
 as we’d expect. The
^ command prints the
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 ? * ? + ? ^ ? !   [30, 20, 10] [600, 10]  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] + + ?  + + ? #Not enough arguments. #Not enough arguments.  ! $
Note in particular that
1.0 lead to error messages but are
otherwise ignored (the program doesn’t stop); same for the two
when the stack only has a single element (the program doesn’t even modify the
stack in that case).
Stackto hold intermediate results and then repeatedly accept input from the user. It doesn’t matter whether you use the
ListStackwe 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
Integerin Java) so
/stands for integer division and
%stands for integer remainder. Both of these should behave in
Calcjust like they do in Java. The details are messy but worth knowing about, especially regarding modulus.
Integerobjects yet still do arithmetic like we would on regular
matchesmethod of the
Stringclass 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
Stringvariable containing all the valid operation symbols if you don’t want to learn about regular expressions.)
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.
Develop an algebraic specification for the abstract data type
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)
You’ll probably need at least one axiom with a case distinction using
if expression; the syntax for this in the
Array specification for
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
there should be no
checkstyle warnings either with the 601.226 configuration
file posted on Piazza.
The ADT Queue should go into your