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

Hi Can someone help me with this question *in java* In this assignment, do not u

ID: 3578373 • Letter: H

Question

Hi Can someone help me with this question *in java* In this assignment, do not use any auxiliary data structures (in particular, do not use a stack). Write a recursive method, eval, to evaluate a postfix expression. The expression is represented as a String and contains the following operators: +, -, * and /. For simplicity, assume that all the numbers are single digit and unsigned, for instance 5, or 6 but not 23, 124 or -4. An example of an input is: ”873-*4+23- *58-+”. Programming hint: in order to transform a single character located at posi- tion i in a string exp to its numerical value, you may use: val = Character.getNumericValue(exp.charAt(i)); public class Postfix { // Private recursive method. private static double recEval(...) { ... } // Public non-recursive public static double eval(String exp) { ... }

Explanation / Answer

import java.util.Scanner;

public class Postfix {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter postfix expression : ");
       String postfixInput = sc.next();
       double result = recEval(postfixInput.trim());
       System.out.println("Result of postfix Expression is : "+ result);
   }
  
   private static double recEval(String postfixInput) {
         
       double a, b;
   try{  
       if (postfixInput.length() == 3 && !Character.isDigit(postfixInput.charAt(postfixInput.length()-1))
               && Character.isDigit((postfixInput.charAt(postfixInput.length()-2)))){
  
           a = postfixInput.charAt(0);
           b = postfixInput.charAt(1);
          
           switch (postfixInput.charAt(2)) {
               case '+':
                   return a + b;
               case '-':
                   return a - b;
               case '*':
                   return a * b;
               case '/':
                   return a / b;
               default :
                   System.out.println("bad input");
               }
       } else {
           return recEval(postfixInput.substring(1, postfixInput.length()-2)); // recursive call ....upto last 3 char remain
              
       }
   }catch(Exception e){
       System.out.println("bad input");
       return 0;
   }
       return 0;
   }

}