Question: Postfix Tester: import java.util.Scanner; public class PostfixTester { public static void main(String[] args) { String expression, again; Scanner in = new Scanner(System. in ); System.

 Postfix Tester: import java.util.Scanner; public class PostfixTester { public static void

main(String[] args) { String expression, again; Scanner in = new Scanner(System.in); System.out.println("Postfix

Postfix Tester:

import java.util.Scanner;

public class PostfixTester {

public static void main(String[] args) {

String expression, again;

Scanner in = new Scanner(System.in);

System.out.println("Postfix Evaluation Team Member 1, Team Member 2, Team Member 3, ...");

do {

System.out.print(" Expression: ");

expression = in.nextLine();

try {

System.out.println("Result = " + PostfixEvaluator.evaluate(expression));

}

catch (ArithmeticException ae) {

System.out.println("ERROR: " + ae.getMessage());

}

System.out.print(" Evaluate another expression [Y/N]? ");

again = in.nextLine();

}

while (again.equalsIgnoreCase("y"));

in.close();

}

}

Postfix Evaluator:

import java.util.Scanner;

public class PostfixEvaluator {

public static int evaluate(String expr) {

ArrayStack stack = new ArrayStack(100);

Scanner parser = new Scanner(expr);

while (parser.hasNext()) {

String token = parser.next();

if ("+-*/".indexOf(token) >= 0) {

Integer op2 = stack.pop();

Integer op1 = stack.pop();

if (op1 == null || op2 == null) {

throw new java.lang.ArithmeticException("Insufficient operands for " + token + ".");

}

stack.push(evaluateSingleOperator(token.charAt(0), op1, op2));

} else {

stack.push(Integer.parseInt(token));

}

}

if (stack.size() != 1) {

throw new java.lang.ArithmeticException("" + (stack.size() - 1) + " too few operators.");

}

return stack.pop();

}

private static int evaluateSingleOperator(char operation, int op1, int op2) {

switch (operation) {

case '+': return op1 + op2;

case '-': return op1 - op2;

case '*': return op1 * op2;

case '/': return op1 / op2;

default: return 0;

}

}

}

Array Stack:

public class ArrayStack implements Stack {

private E[] S;

private int t;

public ArrayStack(int capacity) {

S = (E[]) new Object[capacity];

t = -1;

}

public int size() {

return t + 1;

}

public boolean isEmpty() {

return t == -1;

}

public E top() {

if (isEmpty())

return null;

return S[t];

}

public void push(E element) {

if (t == S.length - 1)

throw new IllegalStateException();

else {

t++;

S[t] = element;

}

}

public E pop() {

if (isEmpty())

return null;

E temp = S[t];

S[t] = null;

t--;

return temp;

}

public String toString() {

String retVal = "(";

for (int i = 0; i

retVal += S[i];

if (i

retVal += ", ";

}

}

retVal += ")";

return retVal;

}

}

Stack:

public interface Stack {

int size();

boolean isEmpty();

E top();

void push(E element);

E pop();

}

Enhanced Postfix Evaluator 1. Problem Description In class, we went over postfix expressions and an approach for evaluating them using a Stack. The Java code was likewise presented and is available in Canvas. You are to enhance this code and include the several additional operations listed below. Your Java program will continue to prompt the user for additional expressions until the user chooses to finish. Your solution should handle any exceptions gracefully and report the error. This program is to be done on your own, not part of a team. Add the following new binary operators: Modulus: el e2 % Power (e1 raised to the e2 power): e1 e2^ Example Expressions: 17 5 % (Result: 2) 34. ^ (Result: 81) Add the following new unary operators: Unary minus: e Factorial: e! Example Expressions: 12 ~ (Result: -12) 5 (Result: -5) 6 ! (Result: 720) 3! 4 * 53% - 4 2 - / * (Result: -44) Add the following relational, Boolean and ternary operators: Relational Operators: e1 e2>, e1 e2 (Result: 1) 53 23> | (Result: 1) 00 (Result: 0) 5 3 Result = 1 Post-fix expression: 53 Result = 0 Post-fix expression: 5 5 = Result = 1 Post-fix expression: 10 5 & Result = 1 Post-fix expression: 53 > 2 3 >| Result - 1 Post-fix expression: 1 Result = 0 Post-fix expression: 53

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!