I tried to google this problem first, and couldn't find an answer. I am writing a code for a simple calculator for a project, and we are using a stack to handle operators in a mathematical expression. I am having two related problems in the Expression class:
public Double evaluate() {
Stack<Operator> operatorStack = new Stack<Operator>();
Stack<Operand> operandStack = new Stack<Operand>();
while(mTokenQueue.isEmpty() == false) {
Token token = getTokenQueue().dequeue();
if(token instanceof Operand) {
operandStack.push((Operand) token);
} else if(token instanceof LeftParen) {
operatorStack.push((LeftParen) token);
} else if(token instanceof RightParen) {
while(!(operatorStack.peek() instanceof LeftParen)) {
this.topEval(operatorStack, operandStack);
}
operatorStack.pop();
} else {
Operator operator = (Operator) token;
while(keepEvaluating(operatorStack, operator) == true) {
this.topEval(operatorStack, operandStack);
}
operatorStack.push(operator);
}
}
while(!operatorStack.isEmpty()) {
this.topEval(operatorStack, operandStack);
}
return operandStack.pop().getValue();
}
In this method, I get the following exception:
The method push(VectorOperators.Operator) in the type Stack<VectorOperators.Operator> is not applicable for the arguments (LeftParen)
Also, in the following code:
private boolean keepEvaluating(Stack<Operator> pOperatorStack, Operator pOperator) {
if (pOperatorStack.isEmpty()) {
return false;
} else {
return pOperatorStack.peek().stackPrecedence() >= pOperator.precedence();
}
}
I get a warning that both the stackPrecedence and the precedence methods are undefined for the type VectorOperators.Operator
Now, I have a class called Operator in the program, and LeftParen is definitely coded as a subclass of Operator, but for some reason, the push method seems to think it is referencing VertorOperators.Operator instead of my Operator class. Here is the push method in the Stack class:
public void push(E pData) {
getList().prepend(pData);
}
Thank you for your time!