Question
given the following code: please answer each part separately (a,b,c,d) package ch02.postfix; import ch02.stacks.*; import java.util.Scanner; public class PostFixEvaluator { public static int evaluate(String expression)
given the following code: please answer each part separately (a,b,c,d)
package ch02.postfix; import ch02.stacks.*;
import java.util.Scanner;
public class PostFixEvaluator { public static int evaluate(String expression)
{ Scanner tokenizer = new Scanner(expression);
StackInterface stack = new ArrayBoundedStack(50);
int value;
String operator;
int operand1, operand2;
int result = 0;
while (tokenizer.hasNext()) { if (tokenizer.hasNextInt())
{ // Process operand. value = tokenizer.nextInt();
if (stack.isFull()) throw new PostFixException("Too many operands-stack overflow");
stack.push(value);
} else { // Process operator. operator = tokenizer.next();
// Check for illegal symbol if (!(operator.equals("/") || operator.equals("*") || operator.equals("+") ||
operator.equals("-"))) throw new PostFixException("Illegal symbol: " + operator);
// Obtain second operand from stack. if (stack.isEmpty())
throw new PostFixException("Not enough operands-stack underflow");
operand2 = stack.top(); stack.pop();
// Obtain first operand from stack. if (stack.isEmpty()) throw new PostFixException("Not enough operands-stack underflow");
operand1 = stack.top();
stack.pop();
// Perform operation. if (operator.equals("/")) result = operand1 / operand2;
else if(operator.equals("*")) result = operand1 * operand2;
else if(operator.equals("+")) result = operand1 + operand2;
else if(operator.equals("-")) result = operand1 - operand2;
// Push result of operation onto stack. stack.push(result); } }
// Obtain final result from stack. if (stack.isEmpty()) throw new PostFixException("Not enough operands-stack underflow");
result = stack.top();
stack.pop();
// Stack should now be empty. if (!stack.isEmpty()) throw new PostFixException("Too many operands-operands left over");
// Return the final. return result; } }
Revise and test the profix expression evaluator program as specified here. a. use the arrayListStack class instead of ArrayBoundedStack class. do not worry about stack over flow. b.catch and handle the divide by zero situation that was assumed not to happen. for example if the input expression is 5 3 3 - /, the result would be the message "illegal divide by zero." c.support a new operation indicated by "^" that returns the larger of its operands. for example 5 7 ^ = 7. d. keep track of statistics about the numbers pushed on to the stack during the vealuation of an expression. the program should output the largest and smallest numbers pushed, the total numbers pushed, and the average value of pushed numbers
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started