USING JAVA // Complete this class by inserting the code specified by the single
ID: 3865615 • Letter: U
Question
USING JAVA
// Complete this class by inserting the code specified by the single line comments.
// Provide the required import statements for the Stack and Scanner classes.
/**
* Represents an integer evaluator of postfix expressions. Assumes
* the operands are constants.
*
* @author Lewis and Chase
* @version 4.0
*/
public class PostfixEvaluator
{
// Declare three class constants of type char: ADD (+), SUBTRACT (-), MULTIPLY (*) and DIVIDE (/)
// Declare a variable called stack of type Stack (from the Java API).
// The stack will hold objects of type Integer (the int wrapper class.
private Stack<Integer> stack;
/**
* Sets up this evalutor by creating a new stack.
*/
public PostfixEvaluator()
{
// create the stack here.
}
/**
* 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
*/
// Add one formal parameter called expr of type String to the method evaluate.
public int evaluate()
{
int op1, op2, result = 0;
String token;
Scanner parser = new Scanner(expr);
while (parser.hasNext())
{
// Get the next token of input from the Scanner variable and assign it to the variable token.
// reference: Java API
// For the boolean expression of the following if statement, call the isOperator method.
if ()
{
op2 = (stack.pop()).intValue();
// Get the first operator off the stack in a similar fashion to the second operator in line 53.
// Call the evaluateSingleOperator method and store the returned value in result.
// Push the result onto the stack. You'll have to use the wrapper class for the integer result.
}
else
stack.push(new Integer(Integer.parseInt(token)));
}
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)
{
return ( token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/") );
}
/**
* 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
*/
// Add the formal parameters required by the method evaluateSingleOperator as indicated by the Javadoc comments.
private int evaluateSingleOperator()
{
int result = 0;
// Write a switch statement using operation.
// Perform the operation for each case (ADD, SUBTRACT, MULTIPLY, DIVIDE)
// Store the result of the operation in the local variable result.
return result;
}
}
Explanation / Answer
import java.util.Scanner;
import java.util.Stack;
/**
*
* @author Namburi Ramesh
*/
public class PostfixEvaluator
{
// Declare three class constants of type char: ADD (+), SUBTRACT (-), MULTIPLY (*) and DIVIDE (/)
// Declare a variable called stack of type Stack (from the Java API).
// The stack will hold objects of type Integer (the int wrapper class.
private final char ADD='+';
private final char SUBTRACT='-';
private final char MULTIPLY='*';
private final char DIVIDE='/';
private Stack<Integer> stack;
/**
* Sets up this evalutor by creating a new stack.
*/
public PostfixEvaluator()
{
// create the stack here.
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
*/
// Add one formal parameter called expr of type String to the method evaluate.
public int evaluate(String expr)
{
int op1, op2, result = 0;
String token;
Scanner parser = new Scanner(expr);
parser.useDelimiter(" ");
while (parser.hasNext())
{
// Get the next token of input from the Scanner variable and assign it to the variable token.
// reference: Java API
token=parser.next();
// For the boolean expression of the following if statement, call the isOperator method.
if (isOperator(token))
{
op2 = (stack.pop()).intValue();
// Get the first operator off the stack in a similar fashion to the second operator in line 53.
op1=(stack.pop()).intValue();
// Call the evaluateSingleOperator method and store the returned value in result.
result=evaluateSingleOperator(token.charAt(0), op1, op2);
// Push the result onto the stack. You'll have to use the wrapper class for the integer result.
stack.push(new Integer(result));
}
else
stack.push(new Integer(Integer.parseInt(token)));
}
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)
{
return ( token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/") );
}
/**
* 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
*/
// Add the formal parameters required by the method evaluateSingleOperator as indicated by the Javadoc comments.
private int evaluateSingleOperator(char operation,int op1,int op2)
{
int result = 0;
// Write a switch statement using operation.
// Perform the operation for each case (ADD, SUBTRACT, MULTIPLY, DIVIDE)
// Store the result of the operation in the local variable result.
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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.