CSCI 151 - Lab 3
Reverse Polish Notation Calculator
Overview
In this lab, we will write two implementations of a stack and a RPN Calculator and test them with JUnit.
Materials
Setup
- Download the skeleton for this project.
- Unpack the code into a new Eclipse Java project.
Description
An expression such as 2 + 3 * 5
is ambiguous by itself, only
looking at syntax. To resolve it
properly we need to use the order of operations: Multiplication, then Division, then Addition,
then Subtraction, or use parenthesis to force our order. (2 + 3) * 5
is different than 2 + (3 * 5)
.
Alternately, we can use
Reverse
Polish Notation to embed the precedence in the expression. By placing the operator
at the end instead of in the middle of the two operands, we know exactly
what operation to perform when. Our ambiguity disappears, since the above expression
will need to be written as
2 3 5 * +
or
2 3 + 5 *
depending on the desired result.
This model of mathematical operators can be implemented with a stack. When the
calculator sees an integer, it should be added to the stack. When it sees
an operator (*, +, /, -, %), it should pop the two most recent elements from
the stack, perform the operation, and then push the result back on the stack.
When the order of the operation matters, such as a - b
, then your
code should use the first element popped as b
and the
second element popped as a
.
StackViewer
To help us visualize our implementations,
you will find the StackViewer
class in the numstack
package, written
using JavaFX. Run this program to see a text box for entry and a listing below
of the current contents of the stack.
Currently, the StackViewer does not work, because it is missing a stack
implementation. You must implement two versions of a stack, one using an
array, and the second using a list of nodes.
Step 1 - ArrayIntStack
In the numstack.model
package, you will find the following
classes:
IntStack.java
This is a general interface for a stack of integers. You will want to call
the default method emptyCheck()
at the right places, to ensure the appropriate
methods fail when necessary.
ArrayIntStack.java
You will use an array of integers to implement a stack.
Note: If you use ArrayList you will receive no credit for the assignment.
IntStackTest.java
These are the unit tests for your stack. Your grade depends on passing these tests.
ArrayIntStackTest.java
Run this file to see the results of the unit tests for your ArrayIntStack implementation.
Step 2 - ListIntStack
You will use a linked list of Nodes to implement a stack.
ListIntNode
You will first need to create a file called ListIntNode.java
that implements the Node class we
discussed. It should have an int item
, and a ListIntNode next
reference as
components.
ListIntStack.java
You will use a sequence of ListIntNode objects to implement a stack.
ListIntStackTest.java
Run this file to see the results of the unit tests for your ListIntStack implementation.
Step 3 - RPNCalc
In the rpn.model
package, you will find the following
classes:
RPNCalc.java
This is the main file you will have to complete. You will need to make a constructor
and three methods.
addTerm(String term)
This method brings in a single token as a String. If it is a number, then it should
be added to the stack. If it is a valid operator, then it should perform the
operation, using the top two elements of the stack, and push the result onto the stack.
Otherwise, it should throw an
IllegalArgumentException.
getLastValue()
This method should return the most
recent integer term added to the calculator. By the end of this
method, the calculator should be in EXACTLY THE SAME STATE as
when the method started.
getNextToLastValue()
This method should return the second most
recent integer term added to the calculator. By the end of this
method, the calculator should be in EXACTLY THE SAME STATE as
when the method started.
RPNCalcTest.java
These are the unit tests for your calculator. Your grade depends on passing these tests.
Op.java
This is an Enum. It is used in testing with JUnit, and you can safely ignore it.
Step 4 - TimeTest
TimeTest.java
Run this file to compare the performance of ArrayIntStack and ListIntStack.
What to Hand In
Submit your ArrayIntStack.java, ListIntStack.java, and RPNCalc.java implementations, along with
any additional files you created for your implementations.
Grading
- To earn a D, pass all tests for the ArrayIntStack implementation.
- To earn a C, pass all tests for the ListIntStack implementation.
- To earn a B, pass all tests for the RPN Calculator.
- To earn a A, write a short document comparing the running times of ArrayIntStack and ListIntStack using TimeTest.
- To earn a 100, rerun TimeTest with 104, 105, and 107 items, and analyze the results in your document.
© Mark Goadrich, Hendrix College