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

Can someone please help me with this. Use JAVA to do it. Thanks so much in advan

ID: 639679 • Letter: C

Question

Can someone please help me with this. Use JAVA to do it. Thanks so much in advanced for the help. If you have any questions please comment and I will get back to you. Will rate as soon as I verify that it works exactly as shown in the Example Dialog. The driver is pasted in below.

Write a class called ReversePolishCalculator (which by using the driver) will take a string, parse it, and then calculate the value in post-fix (reverse polish) notation.  To understand this mostly every expression you

Explanation / Answer

import java.util.*;

public class ReversePolishCalculator {
public Stack<Integer> valueStack;

public ReversePolishCalculator() {
valueStack = new Stack<>();
}
  
public int calculate(String input)
{
for(int i=0; i<input.length(); i++)
{
int temp=0, a, b;
  
char c = input.charAt(i);
if(c >= '0' && c <='9')
{
valueStack.add((int)c-'0');
}
else
{
switch(c)
{
case '+':
if(!(valueStack.size() >= 2))
{
System.out.println("This was not properly formatted");
return 0;
}
b=valueStack.pop();
a=valueStack.pop();
temp = a + b;
valueStack.push(temp);
break;
  
case '-':
if(!(valueStack.size() >= 2))
{
System.out.println("This was not properly formatted");
return 0;
}
b=valueStack.pop();
a=valueStack.pop();
temp = a - b;
valueStack.push(temp);
break;
  
case '/':
if(!(valueStack.size() >= 2))
{
System.out.println("This was not properly formatted");
return 0;
}
b=valueStack.pop();
a=valueStack.pop();
temp = a / b;
valueStack.push(temp);
break;
  
case '*':
if(!(valueStack.size() >= 2))
{
System.out.println("This was not properly formatted");
return 0;
}
b=valueStack.pop();
a=valueStack.pop();
temp = a * b;
valueStack.push(temp);
break;
}
}
}
  
if(valueStack.size() != 1)
{
System.out.println("This was not properly formatted");
return 0;
}
return valueStack.pop();
}
}