YCP Logo Assignment 2: Postfix calculator

Due: Thursday, Feb 4th by 11:59 PM

Updated Feb 1st: You may use java.util.StringTokenizer.

Getting started

Download CS350_Assign2.zip. Import it into your Eclipse workspace (File->Import...->General->Existing projects into workspace->Archive File). You will see a project called CS350_Assign2 in the Package Explorer.

Do not use java.util

In this course, you are required to implement all data structures from scratch. Do not use any of the classes in the java.util package.

Exception: You may use java.util.StringTokenizer.

Do not copy code from textbook or other sources

Some assignments in this course may involve data structures which are implemented by source code in the textbook. And, of course, lots of code is available from web sites and other sources.

I expect you to implement all data structures and algorithms from scratch, using your knowledge of how they work. Do not copy code from any source, including the textbook.

Your task

Your task is to implement a postfix calculator class.

The RPNCalculator interface defines the single method for evaluating postfix expressions:

public interface RPNCalculator {
        public Double evaluate(String expr, Map<String, Double> variableMap);

This method takes a string containing a postfix expression and returns the double value which results from evaluating the expression.

If any variables appear in the expression, their values can be looked up by name in the map referenced by the variableMap parameter.

You should implement the MyRPNCalculator class, which implements this interface.

The MyRPNCalculatorTest JUnit test class has test cases that demonstrate how the class should work. You should add additional test to ensure that your calculator class works as expected.


Use a StringTokenizer to split the expression into tokens. Each token will be either an operand (literal number or variable) or an operator (+, -, *, or /). (Note that you are allowed to use StringTokenizer even though it is in the java.util package.)

Use a stack of Double values to assist in evaluating the expression. You should use an instance of your MyStack class from Assignment 1 as your stack. You can copy in the entire edu.ycp.cs350.util package from that assignment into the src folder of this assignment.

The stack can be used as follows:

  • Operands should have their values pushed onto the stack
  • When an operator is encountered, pop its operand value(s) off the stack, perform the operation, and push the result onto the stack

When the end of the expression is reached, the stack should contain a single value which is the result of the expression.

You may assume that variable names will consist entirely of letters, as determined by the Character.isLetter static method.

You can use the Double.valueOf(String) static method to convert literal numbers to Double values.


For a grade of up to 85/100, implement the basic features described above.

Features you may implement for additional credit:

  • 10 points: Support sin and cos operators which compute the sine/consine of a single operand.
  • 20 points: Implement a GUI which plots the value of a function of a single variable, specified as a postfix expression, within a given range of values. For example, the user could enter the expression "x sin" to see a plot of y = sin(x) for a particular range of x values.


Export your finished project to a zip file (right-click on the project, then Export...->Archive File). Upload the zip file to the marmoset server as project assign2. The server URL is


IMPORTANT: after uploading, you should download a copy of your submission and double-check it to make sure that it contains the correct files. You are responsible for making sure your submission is correct. You may receive a grade of 0 for an incorrectly submitted assignment.