In this assignment, you will learn how to use and program stacks, get an
introduction to interpretation by implementing a reverse polish notation
calculator, and develop an application within a model-view-controller
Implement Transparent Stacks
The stack data structure is
explained in your textbook on pages 648 to 650.
In this assignment, you will implement a variation of the stack data
structure called a transparent stack.
A stack data type is normally defined as a
last-in-first-out linear data structure with two basic operations, push and
pop. Stacks also typically contain two
additional operations: peek (sometimes called “top”) and empty. Normally these four operations are the only
operations which can be used on a stack.
As a result, the stack is opaque: you cannot see what is inside it, only
what is at the top by using peek.
In this assignment, you should
implement a variation of the stack data structure: a transparent stack. A transparent stack is a stack where not only
the top of the stack is visible, but each element also is. More precisely, you
need to implement the class TransparentClass which has been specified in the TransparentStack.java file as
Implement the specified constructors for
You must use a heterogeneous array to store the
stack entries (in other words, an array of Object references).
The initial size of the array is specified in
the initialsize parameter of the
TransparentStack constructor. If no size
is specified, the default initialsize
Implement the specified public methods for
If an element needs to be pushed onto a full
stack, you should increase the size of the stack by an extra initialsize entries. To do this, make a new array of the new size,
copy the existing elements into the new array, and replace the old array by the
You do not need to decrease the size of the
array when it gets small.
You cannot use the Java ArrayList and Stack classes to implement the class
Test Transparent Stacks with Visible Stacks
In the first part of the
assignment, you were asked to build a transparent stack. For the rest of the assignment, you will use
another stack structure called a visible stack, which is a transparent stack that
also has one or more displays.
You do not need to implement a
visible stack class because one has been implemented for you in VisibleStack.java. You will notice that a VisibleStack object
contains a reference to a TransparentStack object. You can therefore test that your
TransparentStack objects work properly using the VisibleStack class. Since VisibleStacks have displays, you can see
the contents of your stack as you are pushing and popping elements on it.
VisibleStacks contain an array of
displays called viewers. To add a viewer for your visible stack,
simply use the addViewer method. Three
types of viewers have been provided for visible stacks:
is a very simple text viewer which simply displays the top of the stack.
displays a text version of the stack. It
is not very nice looking and it is very tedious because the stack is redrawn
each time anything happens to it.
However, this can be very useful when you are testing and debugging
because it will output everything that happens to the stack using
System.out.println and you will have a record of every step, which you can look
displays a graphical version of the stack.
This is a much nicer viewer that can be used for final products.
TextStackViewer and GraphicsStackViewer are
subclasses of StackViewer.
You do not need to implement
anything other than the TransparentStack class to make the three viewer classes
and VisibleStack class work. You should,
however, make sure that your TransparentStack class is working properly with
these classes before you move on to the next part of the assignment. If you run into problems, do not change the VisibleStack,
StackViewer, TextStackViewer, and GraphicsStackViewer classes because we
will be using the ones we gave you to test your TransparentStack class. You should instead fix your TransparentStack
to make it work with the other four classes.
Implement Reverse Polish Notation Calculator
The first serious handheld
scientific calculators produced by Hewlett-Packard in 1972 used reverse polish notation,
also called postfix notation, which works are follows: instead of entering binary operations in infix
notation <operand1> <operator> <operand2> as you are used to,
operations are entered in postfix form with the two operands first and
the operator last.
For example, the operation “ 1 + 2”
would be entered as “ 1 2 +”
and the operation “3´2 + 4´5”
would be entered as: “3 2 ´
4 5 ´ +”
This seems a bit strange, but once
you get used to it, postfix notation is much superior to infix notation because
you do not need parentheses to override
precedence. For example “3´
(4+5)” can be entered in postfix notation as “3 4 5 + ´”. (Try other examples yourself).
The HP RPN page also
explains other advantages of reverse polish notation. However, another additional advantage of
reverse polish notation is that the calculators are very easy to implement with
when the user enters a number, it is simply
pushed on the stack
when the user enters an operation, then the
right number of operands are popped from the stack, combined using the
operator, and the result is pushed back on the stack.
In this part of the assignment you
should write a class Calculator which implements a reverse polish notation
calculator with 4 binary operators: +, -, ´, and ¸
. The exact specifications for the
Calculator classes are in the Calculator.java
file. In particular:
Calculators have two possible user interfaces:
which provides a GUI input window class
for the calculator. You can test this
class using TestGUICalculator.java
which provides a text input user interface for the calculator. The code for this class is much easier to
understand than the code for the GUICalculator, so it is recommended that you
read this file to understand how the Calculator methods are used. You can test this class using TestTextCalculator.java
The calculator should work with both
classes without modifying them.
Numbers can be entered as integers or floats,
but they are all converted to Floats before they are given to the calculator to
push on the stack. It is assumed that
the operators work on Floats and the results are Floats.
Unary + and unary – are accepted by the
calculators but they are considered part of a number and not expressed in
postfix notation. In the GUICalculator,
they are entered in the number field as part of the number (for example -12.3
or +23.5). In the TextCalculator they
are intered in front of the number (see the source for how this works).
The calculator should check for the following
errors and push the String “Error” on the stack instead of performing an
operation when it encounters one of these situations:
A division by zero
Not enough operands for an operation
One of the operands is the String “error”.
When a new number is pushed onto the stack, and
the top of the stack is “Error”, “Error” is popped off the stack before the
number is pushed.
The calculator will include a reference to a VisibleStack
of initialsize 20 that is used to
make it work. Viewers for the calculator
stack are added using the addViewer method described in Calculator.java
In addition to entering numbers and operations,
users of the calculator can also request that the whole stack be cleared or
that the top entry on the stack be removed.
See Calculator.java for
specifications for these two methods and TextCalculator.java
to see how they are used.