I made a class Operator
and a method called compare
. However, when call this method in my program, I always got error message
InfixToPostfix.java:17: error: cannot find symbol
System.out.println(compare(op,op1));
^
symbol: method compare(Operator,Operator)
location: class InfixToPostfix
I think I did not make any spell mistakes.
public class Operator extends Token {
protected String val; //Modified by Qinjianhong Yang, 11/18/16
public boolean isOperator() { return true; }
public boolean isOperand() { return false; }
// helper method, returns (assigns) precedence for operators
protected int getPrec()
{
//modified by Qinjianhong Yang, 11/17/2016
if(this.val.equals("+") || this.val.equals("-")){
return 1;
}
else return 2;
}
// handy for comparing 2 operators
public static int compare( Operator a, Operator b )
{
if( a.getPrec() == b.getPrec() )
return 0;
else if( a.getPrec() < b.getPrec() )
return -1;
else
return 1;
}
public String getVal() { return this.val; }
public Operator( String v ) { this.val = v; }
}
I call this function like this:
Operator op = new Operator("+");
Operator op1 = new Operator("*");
System.out.println(compare(op,op1));