- #1
jsmith0476
- 6
- 0
Homework Statement
I need to write a method for the PostfixConversion class:
public static boolean precedence(char first, char second)
The precedence method determines the precedence between two operators. (here, they are either '+', '-', '*', or '/') If the first operator is of higher or equal precedence than the second operator, it returns the value true; otherwise, it returns the value false. (for instance, if the new operator is '+' or '-', then all operators (+,-,*,/) have precedence greater than or equals to the operator. If the new operator is '*' or '/', then only the operators '*' and '/' have precedence greater than or equals to the operator.)
Homework Equations
The Attempt at a Solution
public static boolean precedence(char first, char second)
{
int char1Index=0;
int char2Index=0;
String precedence = "-+*/";
for(int i=0; i<precedence.length(); i++)
{
if(first==precedence.charAt(i)) char1Index=i;
if(second==precedence.charAt(i)) char2Index=i;
}
if(((char1Index==0)||(char1Index==1)) && (char2Index>1)) return false;
else return true;
}
I feel like something is messed up. Let me know if I am making any mistakes. I didn't really understand what I was supposed to be doing. This was my best guess.