Answered step by step
Verified Expert Solution
Question
1 Approved Answer
// here is my attempt at implementing a RPN calculator. I can't figure out why it is throwing exceptions when it shouldn't be. // for
// here is my attempt at implementing a RPN calculator. I can't figure out why it is throwing exceptions when it shouldn't be. // for example if the string is String[] e1 = {"1", "1", "+"}; // the output should be 2, but it throws an exception. // this method is suppossed to throw an exception when the token is invaild (not a number or operator), when the expression is invalid, when there is an integer overflow or underflow, and when division by zero is attempted. // any help on what i'm doing wrong would be grreatly appreciated! Thank you public class RPNCalculator { private Stackstack; private int size; public RPNCalculator (int initSize) { // create a new StackArray of Strings // and set the size field to the provided parameter initSize stack = new StackArray (initSize); size = initSize; } public int calculate(String[] expression) throws CalculatorException { int returnValue = 0; String operators = "+-*/"; Stack stack = new StackArray<>(size); try { for (String t : expression) { if (isInt(t)) { //push to stack if it is a number stack.push(t); } else if (operators.contains(t)) {//pop numbers from stack if it is an operator int a = Integer.valueOf(stack.pop()); int b = Integer.valueOf(stack.pop()); switch (t) { case "+": stack.push(String.valueOf(a + b)); break; case "-": stack.push(String.valueOf(b - a)); break; case "*": stack.push(String.valueOf(a * b)); break; case "/": if (a == 0) { throw new CalculatorException("Division by zero"); } stack.push(String.valueOf(b / a)); break; } } else throw new CalculatorException("Invalid Token");// if string is neither a number nor an operator } returnValue = Integer.valueOf(stack.pop()); }catch(StackException e){ System.out.print("Catching StackException "); } if(returnValue > Integer.MAX_VALUE){ throw new CalculatorException("Integer Overflow"); } if(returnValue < Integer.MIN_VALUE){ throw new CalculatorException("Integer Underflow"); } return returnValue; } public boolean isInt(String token){ int n = Integer.parseInt(token); if(n < Integer.MAX_VALUE && n > Integer.MIN_VALUE){ return true; }else return false; } } // If you need it, the working StackArray implementation is as follows: import java.util.Arrays; public class StackArray implements Stack { private T[] data; private int top; StackArray(int size) { data = (T[]) new Object[size]; top = -1; } public void push(T elem) throws StackException { if (isFull()) { throw new StackException("Stack is Full"); } data[++top] = elem; } public boolean isFull() { return top == data.length - 1; } public T pop() throws StackException { if(isEmpty()){ throw new StackException("Stack is Empty "); } return data[top--]; } public void popAll() { while( !this.isEmpty()){ try { this.pop(); }catch (StackException e){ break; } } } public T peek() throws StackException { if (isEmpty()) { throw new StackException("Stack is Empty"); } else { return data[top]; } }
public boolean isEmpty() { return top == -1; }
public String toString() { String result = "{"; if(top == -1) { return "{}"; }
for(int i = 0; i < data.length-1; i++) { result += data[i]; result += ", "; }
result += "}"; return result; }}
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