Description
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
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
Append a right parenthesis, “)”, to the end of infix
Push a left parenthesis, “(“, onto the stack
While infix has more tokens, do the following:
If the current token is an integer, append it to postfix.
If the current token is a left parenthesis, push it on the stack.
If the current token is an operator:
Pop operators (if there are any) off of the stack while they have equal or higher precedence than the current operator and append the popped operators to postfix.
Push the current operator on the stack.
If the current token is a right parenthesis:
Pop operators off the stack (there must be at least one) and append them to postfix until a left parenthesis is found at the top of the stack.
Pop the left parenthesis from the top of the stack.
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.