Question: Please complete the TODO's Code: import java.util.Stack; import java.util.Scanner; /** * Represents an integer evaluator of postfix expressions. Assumes * the operands are constants. Stack
Please complete the TODO's
Code:
import java.util.Stack; import java.util.Scanner;
/** * Represents an integer evaluator of postfix expressions. Assumes * the operands are constants. Stack and its application in evaluation of postfix expression
The Postfix notation is used to represent arithmetic expressions, where the operators appear after their operands.
For example, instead of writing (2+14) *23 we write 2 14 + 23 *
With postfix notation, there are no precedence rules to learn, and parentheses are never needed. Thus, the expressions written in postfix form are evaluated faster compared to infix notation, where the operators appear in between their operands. In the infix notation, we follow a set of rules to determine which operation to carry out first. For example, multiplication is done before addition. we use parentheses to override the normal ordering rules.
More examples are here: Postfix expression Infix Equivalent Result 4 5 7 2 + - * 4 * (5 - (7 + 2)) -16 3 4 + 2 * 7 / (3 + 4) * 2 / 7 2 5 7 + 6 2 - * (5+7) * (6 -2) 48
To write a program that evaluates postfix expressions entered interactively by the user. In addition to computing and displaying the value of an expression, our program must display error messages when appropriate (""not enough operands", "too many operands", and "illegal symbol".
There are two basic items in a postfix expression: operands (numbers) and operators. We access items (an operand or an operator) from left to right, one at a time. When the item is an operator, we apply it to the preceding two operands.
Following is algorithm for evaluation postfix expressions. 1) Create a stack to store operands (or values). 2) Scan the given expression and do following for every scanned element. a) If the element is a number, push it into the stack b) If the element is a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack 3) When the expression is ended, the number in the stack is the final answer
Note: Please pay attention to the comments starting with "// TODO", which means that the statement is missing. Please fill in four missing statements and it is unnecessary to remove their comments **/
public class PostfixEvaluator { private final static char ADD = '+'; private final static char SUBTRACT = '-'; private final static char MULTIPLY = '*'; private final static char DIVIDE = '/';
private Stack
/** * Sets up this evalutor by creating a new stack. */ public PostfixEvaluator() { stack = new Stack
/** * Evaluates the specified postfix expression. If an operand is * encountered, it is pushed onto the stack. If an operator is * encountered, two operands are popped, the operation is * evaluated, and the result is pushed onto the stack. * @param expr string representation of a postfix expression * @return value of the given expression */ public int evaluate(String expr) { int op1, op2, result = 0; String token; Scanner parser = new Scanner(expr);
while (parser.hasNext()) { token = parser.next();
if (isOperator(token)) { //todo handle with operator } else //todo handle with an operand }
return result; }
/** * Determines if the specified token is an operator. * @param token the token to be evaluated * @return true if token is operator */ private boolean isOperator(String token) { boolean isOperator = token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/");
return isOperator; }
/** * Peforms integer evaluation on a single expression consisting of * the specified operator and operands. * @param operation operation to be performed * @param op1 the first operand * @param op2 the second operand * @return value of the expression */ private int evaluateSingleOperator(char operation, int op1, int op2) throws RuntimeException { int result = 0;
switch (operation) { case ADD: result = op1 + op2; break; case SUBTRACT: result = op1 - op2; break; case MULTIPLY: result = op1 * op2; break; case DIVIDE: result = op1 / op2; }
return result; } }
Code:
import java.util.Scanner;
/** * Demonstrates the use of a stack to evaluate postfix expressions. * Stack and its application in evaluation of postfix expression
The Postfix notation is used to represent arithmetic expressions, where the operators appear after their operands.
For example, instead of writing (2+14) *23 we write 2 14 + 23 *
With postfix notation, there are no precedence rules to learn, and parentheses are never needed. Thus, the expressions written in postfix form are evaluated faster compared to infix notation, where the operators appear in between their operands. In the infix notation, we follow a set of rules to determine which operation to carry out first. For example, multiplication is done before addition. we use parentheses to override the normal ordering rules.
More examples are here: Postfix expression Infix Equivalent Result 4 5 7 2 + - * 4 * (5 - (7 + 2)) -16 3 4 + 2 * 7 / (3 + 4) * 2 / 7 2 5 7 + 6 2 - * (5+7) * (6 -2) 48
To write a program that evaluates postfix expressions entered interactively by the user. In addition to computing and displaying the value of an expression, our program must display error messages when appropriate (""not enough operands", "too many operands", and "illegal symbol".
There are two basic items in a postfix expression: operands (numbers) and operators. We access items (an operand or an operator) from left to right, one at a time. When the item is an operator, we apply it to the preceding two operands.
Following is algorithm for evaluation postfix expressions. 1) Create a stack to store operands (or values). 2) Scan the given expression and do following for every scanned element. a) If the element is a number, push it into the stack b) If the element is a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack 3) When the expression is ended, the number in the stack is the final answer
Note: Please pay attention to the comments starting with "// TODO", which means that the statement is missing. Please fill in four missing statements and it is unnecessary to remove their comments * */ public class PostfixTester { /** * Reads and evaluates multiple postfix expressions. */ public static void main(String[] args) { String expression, again; int result; Scanner in = new Scanner(System.in); do { PostfixEvaluator evaluator = new PostfixEvaluator(); System.out.println("Enter a valid post-fix expression one token " + "at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)"); System.out.println("Each token must be an integer or an operator (+,-,*,/)"); expression = in.nextLine();
try{ result = evaluator.evaluate(expression);
System.out.println(); System.out.println("That expression equals " + result); } catch (Exception ex){ System.out.println("Exception: " + ex.getMessage()); }
System.out.print("Evaluate another expression [Y/N]? "); again = in.nextLine(); System.out.println(); } while (again.equalsIgnoreCase("y")); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
