Due: Friday, Jan 28th by 11:59 PM
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.
You have two tasks:
Important: In this course, you are required to implement all data structures from scratch. See the Restrictions section below for details.
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:
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.
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 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 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.
An easy way to evaluate a postfix expression is to iterate through the tokens in the expression using:
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:
Example: the expression
2 x sin *
2 * sin(x)
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.
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:
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.