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: 3853068 • 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


    private static void processLine (String line) {
        Scanner tokens = new Scanner (line);
        ArrayList<Integer> stack = new ArrayList<>();
    // declare other variables as needed
        boolean error = false;
        String errString = "";
        while (tokens.hasNext()) {//while there are more tokens and not an error
            String newToken = tokens.next();//get the token
            try {//if the token is a digit
                int operand = Integer.parseInt(newToken);
                stack.add(operand);
            } catch (NumberFormatException ex) {
                try {
                    int operand2 = stack.remove(stack.size()-1);
                    int operand1 = stack.remove(stack.size()-1);
                    int result = 0;
              
                    switch (newToken) {
                        case "+":
                            result = operand1 + operand2;
                            break;
                        case "-":
                            result = operand1 - operand2;
                            break;
                        case "*":
                            result = operand1 * operand2;
                            break;
                        case "/":
                            result = operand1 / operand2;
                            break;
                    }
                  
                    stack.add(result);
                } catch (ArrayIndexOutOfBoundsException ex1){
                    errString = "Error: Not enough opperand";
                    error = true;
                } catch (ArithmeticException ex1) {
                    errString = "Error: division by zero";
                    error = true;
                }
            }
        }
        if (stack.size() > 1) {
            errString = "Error: not enough operator";
            error = true;
        }
      
        System.out.print(line);
        if (error)
            System.out.println(errString);
        else
            System.out.println("=" + stack.remove(0));
    }

Here you go champ. Hope you like the code. Tried my best to maintain industry standard and be simple at the same time. Let me know if you have any doubts

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