Roger L. Kraft

CS 27500 - Programming Assignment 6

This assignment makes use of the files contained in this zip file. This assignment is due Wednesday, December 9.

This assignment is based on pages 325 - 334 and pages 345 - 354 of our textbook.

In the zip file there is a file Expression.java which contains outlines of two static methods, Infix2Postfix() and EvaluatePostfix(), which you are to complete. Below is a description of each of these two methods.

The static method EvaluatePostfix() takes one parameter, a String, and returns an int. The input string holds an arithmetic expression in postfix notation. The arithmetic expression is made up of integers and six operators, +, -, *, /, %, and ^ (integer addition, subtraction, multiplication, division, remainder, and exponentiation). You can assume that the postfix expression does not have any syntax errors in it, and you can assume that all of the tokens in the expression have whitespace around them (this makes it easy to use a StringTokenizer object to scan through the string). The EvaluatePostfix() method should make use of a java.util.Stack<T> object to evaluate the postfix expression and return the resulting integer value. Here is an outline of the algorithm to evaluate a postfix expression. Read the postfix expression string from left to right. While the string has more tokens, if the current token is a number, push the number on the stack. If the current token is an operator, pop two operands off the stack, apply the operator and push the result back on the stack. When there are no more tokens, return the top of the stack.

The static method Infix2Postfix() takes one parameter, a String, and returns another String. The input string holds an arithmetic expression in regular infix notation. The arithmetic expression is made up of integers, parentheses, and six operators, +, -, *, /, %, and ^. You can assume that the infix expression does not have any syntax errors in it, and you can assume that all of the tokens in the expression have whitespace around them (this makes it easy to use a StringTokenizer object to scan through the string). The Infix2Postfix() method should make use of a java.util.Stack<T> object to convert the infix expression to a postfix expression and return a string that holds the postfix expression. (The postfix string should have whitespace around all of its tokens so that it will work as input to the EvaluatePostfix() method.) Here is an outline of the algorithm to convert an infix expression to a postfix expression. Let infix be the name for the infix string and let postfix be the name of the postfix string that is being created.

In the zip file along with this file there are three test programs, TestInfix2Postfix.java, TestEvaluatePostfix.java, and TestExpressions.java, that test your implementations of the Infix2Postfix() and EvaluatePostfix() methods. First test your implementation of Infix2Postfix() using TestInfix2Postfix.java. When that runs without errors, test your EvaluatePostfix() using TestEvaluatePostfix.java. When that runs without errors, the program TestExpressions.java should also run without errors.

Turn in a zip file called CS275Hw6Surname.zip (where Surname is your last name) containing your version of Expression.java.

This assignment is due Wednesday, December 9.