Question: Help?? why am I getting an error? import java.util.Stack; import java.util.Scanner; public class PostfixEvaluator { private final static char ADD = ' + ' ;
Help?? why am I getting an error?
import java.util.Stack;
import java.util.Scanner;
public class PostfixEvaluator
private final static char ADD ;
private final static char SUBTRACT ;
private final static char MULTIPLY ;
private final static char DIVIDE ;
private final static char REMAINDER ;
private final static char MINUS m;
private final static char EXCHANGE r;
private final static char DUPLICATE d;
private final static char PRINT p;
private final static char PRINTNREMOVE n;
private final static char ALL f;
private final static char CLEAR c;
private final static char HELP h;
private Stack stack;
public PostfixEvaluator
stack new Stack;
public int evaluateString expr
int op op result ;
String token;
Scanner scanner new Scannerexpr;
while scannerhasNext
token scanner.next;
if isOperatortoken
opstackpopintValue;
opstackpopintValue;
result evaluateSingleOperatortokencharAt op op;
stack.pushresult;
else
stack.pushIntegerparseInttoken;
return result;
private boolean isOperatorString token
return token.equals token.equals
token.equals token.equals;
private int evaluateSingleOperatorchar operation, int op int op
int result ;
switch operation
case ADD:
result op op;
break;
case SUBTRACT:
result op op;
break;
case MULTIPLY:
result op op;
break;
case DIVIDE:
result op op;
case REMAINDER:
result op op;
case MINUS:
stack.pushstack.pop;
break;
case EXCHANGE:
op stack.pop;
op stack.pop;
stack.pushop;
stack.pushop;
break;
case DUPLICATE:
int top stack.peek;
stack.pushtop;
break;
case PRINT:
System.out.printlnstackpeek;
break;
case PRINTNREMOVE:
System.out.printlnstackpop;
break;
case ALL:
for int i ; i stack.size; i
System.out.printlnstackpop;
break;
case CLEAR:
stack.clear;
break;
case HELP:
System.out.printlnEnter the expression using the following integers:
m r d p f n c q h;
break;
default :
System.out.printlnInvalid input.";
return result;
import java.util.Scanner;
Demonstrates the use of a stack to evaluate post fix expressions.
public class PostFixTester
public static void mainString args
String expression, again;
int result;
Scanner in new ScannerSystemin;
do
PostfixEvaluator evaluator new PostfixEvaluator;
System.out.printlnEnter a valid postfix expression one token
at a time with a space between each token eg;
System.out.printlnEach token must be an integer or an operator ;
expression innextLine;
result evaluator.evaluateexpression;
System.out.println;
System.out.printlnThat expression equals result;
System.out.printEvaluate another expression YN;
again innextLine;
System.out.println;
while againequalsIgnoreCasey;
inclose;
public class InStack
private int stack;
private int top;
public InStackint capacity
stack new intcapacity;
top ;
public void pushint value
stacktop value;
public int pop
return stacktop;
public int peek
return stacktop;
public boolean isEmpty
return top ;
public int size
return top ;
public void clear
top ;
Enter a valid postfix expression one token at a time with a space between each token eg
Each token must be an integer or an operator
r
ERROR
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
