0

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!

  • 1
    "but for some reason, the push method seems to think it is referencing VertorOperators.Operator instead of my Operator class" <- Then you should check your imports, you probably have not imported your own Operator class correctly. – OH GOD SPIDERS Apr 27 '22 at 13:47
  • I took a little, and my problem is that the Expression class imports VectorOperators.Operator, which overrides referencing the Operator class of the program itself. Obious solutions: 1. Delete the import. 2. Call my Operator class something else. The problem with both of these solutions is that my professor wrote that code, and naming the Operator class was in the instructions. Is there a way to override that reference so that my class is referenced in that specific instance instead of VectorOperators.Operator? – Bmharter Apr 28 '22 at 00:02
  • If you have 2 classes with the same name then you can always distinguish them in code by using the full qualified name (package name + class name). See https://stackoverflow.com/questions/2079823/importing-two-classes-with-same-name-how-to-handle – OH GOD SPIDERS Apr 28 '22 at 08:29

0 Answers0