YCP Logo Assignment 1: Stack, Postfix Calculator

Due: Friday, Jan 28th by 11:59 PM

Getting started

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

Your Task

You have two tasks:

  1. Implement the YStackImpl<E> class, which is a concrete implementation of the YStack<E> interface.
  2. Implement the CalculatorImpl class, which is a concrete implementation of the Calculator interface.

Important: In this course, you are required to implement all data structures from scratch. See the Restrictions section below for details.

Implement YStackImpl (and tests)

The YStack<E> interface describes operations for a generic stack datatype. You must complete the implementation of a class called YStackImpl<E> which implements this interface.

There are four methods:

  1. boolean isEmpty() determines whether or not the stack is empty.
  2. E peek() returns the element on the top of the stack without removing it, throwing NoSuchElementException if the stack is empty
  3. void push(E) pushes a new value onto the top of the stack
  4. E pop() pops the top element off of the stack and returns it, throwing NoSuchElementException if the stack is empty

The YStackImplTest JUnit test class contains some basic tests for this class. Add more test methods to this class to ensure that all methods are tested thoroughly.

You may use any underlying data structure that makes sense: however, all operations should have O(1) average-case running time. Using an array or linked list to store the elements on the stack are good approaches.

Implement CalculatorImpl

Complete the CalculatorImpl class so that it implements a calculator for postfix expressions.

The CalculatorImplTest JUnit test class has some basic tests. Add additional test methods to ensure that you have thoroughly tested the features of the calculator.

Postfix expressions

Postfix expressions are a way of writing mathematical expressions in which the operator follows its operands. For example, the "normal" (infix) way of writing the sum of the operands 4 and 5 is

4 + 5

In postfix form, the same expression is written

4 5 +

One interesting feature of postfix expressions is that the order of operations is never ambiguous. For example, the infix expression

3 + 4 * 5

is ambiguous, since it could mean either (3 + 4) * 5 or 3 + (4 * 5). (By convention, multiplication takes precedence over addition, so the latter is the accepted interpretation.) In contrast, written in postfix form, these two interpretations are distinct:

3 4 + 5 *

means (3 + 4) * 5, and

3 4 5 * +

means 3 + (4 * 5).

The Calculator interface

The Calculator interface defines a single method:

public double evaluate(String expr, Map<String, Double> env)

The first parameter (expr) is a string containing a postfix expression. The second parameter (env) is a map containing the values of variables used in the expression.

Evaluating a postfix expression

An easy way to evaluate a postfix expression is to iterate through the tokens in the expression using:

  • When an operand (literal number or variable) is seen, push its value on the stack
  • When an operator is seen, pop off its operand(s), perform the operation, and push the result

You can use the java.util.StringTokenizer class to divide the expression string into individual tokens.

Use your YStackImpl class to store pushed operand values and the results of operations.

After all of the tokens in the expression have been seen, the stack should contain a single value, which is the overall result of the expression.


Your CalculatorImpl class should support evaluating the following kinds of expressions:

  • Literal numbers (e.g., 4, 3.14159)
  • Variables (represented as sequences of letters, such as a, b, width)
  • Binary (two-operand) expressions using the operators +, -, *, or /
  • Unary (one-operand) expression using the operators sin or cos, to compute the sine or cosine of a value. (Note that as a special case, sin and cos should not be considered to be variables.)

Example: the expression

2 x sin *


2 * sin(x)


Do not use java.util data structure implementations

In this course, you are required to implement all data structures from scratch. Do not use any of the data structure implementation classes in the java.util package, such as LinkedList, Stack, HashMap, etc.

You may use the other classes and interfaces in java.util. For example StringTokenizer is fine to use, as are the exception classes (such as NoSuchElementException.)

Please see me if you have any questions about which classes are OK to use.

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 assignment grade will be determined as follows:

  • Correctness/efficiency of YStackImpl class: 30
  • Completeness of unit tests for YStackImpl class: 10
  • Correctness of CalculatorImpl class:
    • Literals: 10
    • Variables: 5
    • Binary expressions: 15
    • Unary expressions (sin and cos operators): 15
  • Completeness of unit tests for CalculatorImpl class: 10
  • Coding style: 5

For extra credit: for up to 25 bonus points, implement a GUI in which the user can enter minimum and maximum values for the variable x and a postfix expression describing a function of the variable x. The GUI should plot the function for the range of x values. Here is an example screenshot:



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 assign1. 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.