Question: PostfixTester: PostfixEvaluator: ArrayStack: Stack: Enhanced Postfix Evaluator 1. Problem Description In class, we went over postfix expressions and an approach for evaluating them using a

 PostfixTester: PostfixEvaluator: ArrayStack: Stack: Enhanced Postfix Evaluator 1. Problem Description In

class, we went over postfix expressions and an approach for evaluating them

PostfixTester:

using a Stack. The Java code was likewise presented and is available

PostfixEvaluator:

in Canvas. You are to enhance this code and include the several

ArrayStack:

additional operations listed below. Your Java program will continue to prompt the

Stack:

user for additional expressions until the user chooses to finish. Your solution

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 stack = new ArrayStack(100); 27 Scanner parser = new Scanner(expr); 9 while (parser.hasNext()) { 10 String token = parser.next(); 11 12 if ("+*/".indexOf(token) >= 0) { 13 Integer op2 = stack.pop(); 14 Integer op1 = stack.pop(); 15 if (op1 == null || op2 == null) { 16 throw new java.lang.ArithmeticException("Insufficient operands for " + token + "."); 17 } 18 stack.push(evaluateSingleOperator(token.charAt(o), opi, op2)); 19 } else { 20 stack.push(Integer.parseInt(token)); 21 } 22 } 23 if (stack.size() != 1) { throw new java.lang.ArithmeticException("" + (stack.size() - 1) + " too few operators."); 26 } 27 28 return stack.pop(); 29 } 30 310 private static int evaluateSingleOperator(char operation, int op1, int op2) { 32 switch (operation) { 33 case '+': return op1 + op2; 34 case '-': return op1 - op2; 35 case '*': return opl * op2; 36 case '/': return op1 / op2; 37 default: return 0; 38 39 40 } 41 } } Stack.java PostfixTester.java PostfixEvaluator.java J ArrayStack.java X 1 2 public class ArrayStack implements Stack { 3 private E[] S; 4 private int t; 5 60 public ArrayStack(int capacity) { 7 S = (EL))...new..Object (capacityl; 8 t = -1; 9 } 10 - 110 public int size() { 12 return t + 1; 13 } 14 . 150 public boolean isEmpty() { 16 return t == -1; 17 } 18 - 190 public E top() { 20 if (isEmpty()) 21 return null; 22 return S[t]; 23 } 24 - 250 public void push(E element) { 26 if (t == S. length - 1) 27 throw new illegalStateException(); 28 else { 29 t++; 30 S[t] = element; 31 } 32 } 33 - 340 public E pop() { 35 if (isEmpty()) 36 return null; 37 E temp = S[t]; 38 S[t] = null; 39 40 return temp; 41 } 42 A430 public String toString() { 44 String retVal = "/"; 45 for (int i = 0; i { 3 int size(); 4 boolean isEmpty(); 5 E top(); 6 void push(E element); 7 E pop(); 8 } 9

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!