Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Evaluator for postfix expressions in java: Assign5.java: In this assignment, you

ID: 3852372 • Letter: E

Question

Evaluator for postfix expressions in java:

Assign5.java:

In this assignment, you will create an evaluator for postfix expressions Input: Your program should prompt for and read postfix expressions. Each expression will be on its own line and tokens will be separated by white space. Operators may be (*,and perform the expected operation. Assume all integers are positive and there be any invalid characters but the expressions may be not formed correctly. The input ends with an empty line. Processing: You will need to read in each string, parse it into the different tokens (either integer or operator) and then evaluate the expr Output: For each input line, print the original string and then the answer. The result wil be an integer. Error handling: You must handle the following errors. 1. Division by zero. 2. Not enough operators in the expression 3. Not enough operands in the expression. Here is an example of input and output. Enter an expression: 15 7 15 7 22 Enter an expression: 8 1055-/+ 8 1055-/+ Error: division by zero Enter an expression: 81055+/+ 81055/Error not enough operands Enter an expression: 8 1055 8 1055/ Error: not enough operators

Explanation / Answer

The answer is as follows:

import java.io.*;
import java.util.*;

public class IntegerStack {
  private ArrayList<Integer> ar;

public IntegerStack() {
   ar = new ArrayList<Integer>();
  }

public void push(int item) {
   ar.add(item);
  }

public int pop() {
   if (!isEmpty())
    return ar.remove(size()-1);
   else
    return -1;
  }

public boolean isEmpty() {
   return (ar.size() == 0);
  }

public int peek() {
   if (!isEmpty())
    return ar.get(size()-1);
   else
    return -1;
  }

public int size() {
   return ar.size();
  }

}

The function processLine is as follows:

private static void processLine(String line){
     Scanner tokens = new Scanner(line);
     IntegerStack stack = new IntegerStack();
     char ch;
     int a,b;
     int error;
     String error_message;
     error = 0;
     while (tokens.hasNext() && error != 1){
         ch = sc.next();
         if (ch >= '0' and ch <= '9'){
            stack.push(Character.getNumericValue(ch));
         }
         else {
            if (stack.size() < 2){
               error = 1;
               error_message = "Error:Not enough operands"
            }
            else {
               
               
               switch (ch) {
                    case '+':

                             a = stack.pop();

                             b = stack.pop();
                             stack.push(b+a);
                    case '-':

                             a = stack.pop();

                             b = stack.pop();

                             stack.push(b-a);
                    case '*':

                             a = stack.pop();

                             b = stack.pop();

                             stack.push(b*a);
                    case '/':

                             a = stack.pop();

                             b = stack.pop();

                             if (a != 0)
                                stack.push(b/a);
                             else {
                                error = 1;
                                error_messgae = "Error:Division by zero";
                             }

               }
            }
         }
     }
    
     if (error == 1){
        System.out.println(line + " " + error_message);
     }
     else {
        if (stack.size() > 1){
           error_message = "Error: Not enough operators";
           System.out.println(line + " " + error_message);
        }
        else {
           System.out.println(line + " " + "=" + stack.pop());
        }
     }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote