In: Computer Science
PostFixEvaluator.java
import java.util.Stack;
import java.util.Scanner;
/**
* Represents an integer evaluator of postfix expressions.
Assumes
* the operands are constants.
*
* @author Java Foundations
* @version 4.0
*/
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 stack;
/**
* Sets up this evaluator by creating a new
stack.
*/
public PostfixEvaluator()
{
// complete this method
}
/**
* 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)
{
// Step 3- complete this method
}
/**
* 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)
{
// Step 1 - complete this
method
}
/**
* Performs 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)
{
// Step 2 - complete this method
}
}
PostFixTester.java
import java.util.Scanner;
/**
* Demonstrates the use of a stack to evaluate postfix
expressions.
*
* @author Java Foundations
* @version 4.0
*/
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();
result =
evaluator.evaluate(expression);
System.out.println();
System.out.println("That expression equals " + result);
System.out.print("Evaluate another expression [Y/N]? ");
again =
in.nextLine();
System.out.println();
}
while
(again.equalsIgnoreCase("y"));
}
}
Could you help, please?
/**
* Represents an integer evaluator of postfix expressions. Assumes
the operands
* are constants.
*
* @author Java Foundations
* @version 4.0
*/
class PostfixEvaluator {
private final static char ADD = '+';
private final static char SUBTRACT = '-';
private final static char MULTIPLY = '*';
private final static char DIVIDE = '/';
private Stack stack;
/**
* Sets up this evaluator by creating a new
stack.
*/
public PostfixEvaluator() {
// complete this method
}
/**
* 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) {
// create a stack
Stack<Integer> stack = new
Stack<>();
// iterate all characters one by
one
for (int i = 0; i <
expr.length(); i++) {
char c =
expr.charAt(i);
if(c==' ')
continue;
// If the
scanned character is an operand (number here),
// push it to
the stack.
if (isOperator(c
+ "")) {
int val1 = stack.pop();
int val2 = stack.pop();
stack.push(evaluateSingleOperator(c, val1,
val2));
} else {
stack.push(c - '0');
}
}
return stack.pop();
// Step 3- complete this
method
}
/**
* 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 res = false;
switch (token.charAt(0)) {
case ADD:
case SUBTRACT:
case DIVIDE:
case MULTIPLY:
res =
true;
break;
default:
res =
false;
}
return res;
// Step 1 - complete this
method
}
/**
* Performs 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
val1, int val2) {
int res = 0;
switch (operation) {
case ADD:
res = (val1 +
val2);
break;
case SUBTRACT:
res = (val2 -
val1);
break;
case DIVIDE:
res = (val2 /
val1);
break;
case MULTIPLY:
res = (val2 *
val1);
break;
}
// Step 2 - complete this
method
return res;
}
}
/**
* Demonstrates the use of a stack to evaluate postfix
expressions.
*
* @author Java Foundations
* @version 4.0
*/
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();
result =
evaluator.evaluate(expression);
System.out.println();
System.out.println("That expression equals " + result);
System.out.print("Evaluate another expression [Y/N]? ");
again =
in.nextLine();
System.out.println();
} while
(again.equalsIgnoreCase("y"));
}
}