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: 3851311 • 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 +ul, and perform the expected operation. Assume a 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 will be a n 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 157 22 Enter an expression: 8 105 5 8 10 5 5 Error: division by zero Enter an expression: 8 105 5 8 10 55 Error not enough operands Enter an expression: 8 10 55 8 1055 Error: not enough operators

Explanation / Answer

Integer stack is implemented here using ArrayList. While processing each expression create token and store in the stack if it is integer and otherwise if it is operators pop first two element from stack and perform the operation. Then store the result to the stack.

If any error occurs update the error string and stop processing if error is not null.

To check if a string is an Integer I have used the below operation

str.chars().allMatch(Character:: isDigit) // checks if chars present in string are all digits

Integers are pushed in the stack arraylist in the 0th position. While popping just delete the 0th element and return.

code:

import java.util.ArrayList;
import java.util.Scanner;

public class postfix {
   public static void main(String args[]){
       Scanner input=new Scanner(System.in);
       String line;
       System.out.println("Enter an expression :");
       line=input.nextLine();
       while(line.length()!=0){
           processLine(line);
           System.out.println("Enter an expression :");
           line=input.nextLine();
       }
       System.out.println("Program Complete");
   }
   public static void processLine(String line){
       Scanner tokens=new Scanner(line);
       IntegerStack stack=new IntegerStack();
       String error=null;
       try{
           while(tokens.hasNext()){
              
               for(String str: tokens.next().split(" ")){
                   if(error !=null)
                       break;
                   if(str.chars().allMatch(Character:: isDigit)){
                       stack.push(Integer.parseInt(str));
                   }
                   else{
                       if(stack.isEmpty() || stack.stacklst.size()<2){
                           error="not enough operands";
                           break;
                          
                       }else{
                           int a=stack.pop();
                           int b=stack.pop();
                           int res=0;
                           if(str.equals("+")){
                               res=b+a;
                           }else if(str.equals("-")){
                               res=b-a;
                           }else if(str.equals("*")){
                               res=b*a;
                           }else if(str.equals("/") && a !=0){
                               res=b/a;
                           }else if(str.equals("/") && a==0){
                               error="division by zero";
                               break;
                           }
                          
                           stack.push(res);
                          
                       }
                   }
               }
          
           }
           if(stack.stacklst.size()>1){
               error="not enough operators";
           }
           System.out.println(line);
           if(error!=null){
               System.out.println("Error :"+error);
          
           }else{
               System.out.println("Answer = "+stack.pop());
           }
       }catch(Exception e){
           e.printStackTrace();
       }
  
      
   }
}
class IntegerStack {
   ArrayList<Integer> stacklst=new ArrayList<Integer>();
   public void push(int a){
       stacklst.add(0, a);
   }
   public int pop(){
       int a= stacklst.get(0);
       stacklst.remove(0);
       return a;
   }
   public boolean isEmpty(){
       return stacklst.size()==0;
   }
   public void print(){
       for(int a: stacklst){
           System.out.print(a+" ");
       }
       System.out.println("***********");
   }
  
}

output:

Enter an expression :
15 7 +
15 7 +
Answer = 22
Enter an expression :
8 10 5 5 - / +
8 10 5 5 - / +
Error :division by zero
Enter an expression :
8 10 5 5 + / + -
8 10 5 5 + / + -
Error :not enough operands
Enter an expression :
8 10 5 5 + /
8 10 5 5 + /
Error :not enough operators
Enter an expression :

Program Complete

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