HertzletPackard HP for short makes highend calculators used

Hertzlet-Packard (HP for short) makes high-end calculators used by Hertz-speare scholars, ceramic engineers, and other nerds. Unlike humans, HP users type equations in reverse polish notation - a highly logical and thoroughly incomprehensible way of writing calculations that eliminates needing parentheses. HP code traditionally used arrays. After many arguments, they have grudginly accepted the benefits of using basic data structures. Complete the calculate method by implementing the given algorithm. This algorithm processes the reverse-polish notation equation in q and returns the value calculated. Using a methods, while legal Java, will earn an automatic 0 AND you will be required to sit in a room and think of all the kittens you made sad.

Solution

import java.util.Queue;
import java.util.Stack;

public class Calculate {
public double calculate(Queue<String> q)
{
   Stack<Double> s;
   s=new Stack<Double>();
   while(!q.isEmpty())
   {
       double num;
       String val;
       val=q.poll();
       if(val!=null)
       {
           if(NumberUtils.isNumber(val))
           {
               num=Double.parseDouble(val);
           }
           else if(s.size()<=2)
           {
               System.out.println(\"Invalid Operation Exception\");
               return 0;
           }
           else
           {
               double left=s.pop();
               double right=s.pop();
               num=left+right;
           }
       }
       s.push(num);
   }
   double retVal;
   retVal=s.pop();
   return retVal;
}
}

 Hertzlet-Packard (HP for short) makes high-end calculators used by Hertz-speare scholars, ceramic engineers, and other nerds. Unlike humans, HP users type equa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site