this below is the code :
public String evaluate() {
if (this.data.equals("+")) {
return String.valueOf(Double.parseDouble(this.children.get(0).evaluate()) + Double.parseDouble(this.children.get(1).evaluate()));
}
else if (this.data.equals("-")) {
return String.valueOf(Double.parseDouble(this.children.get(0).evaluate()) - Double.parseDouble(this.children.get(1).evaluate()));
}
else if (this.data.equals("*")) {
return String.valueOf(Double.parseDouble(this.children.get(0).evaluate()) * Double.parseDouble(this.children.get(1).evaluate()));
}
else {
return String.valueOf(Double.parseDouble(this.children.get(0).evaluate()) / Double.parseDouble(this.children.get(1).evaluate()));
}
}
the this.data value hold's the operand in form of String. Is there a way to eliminate the if-else altogether, decrease the lines of the code.? No to using switch case also.
Is there a way to convert string to operand in java.?
Aucun commentaire:
Enregistrer un commentaire