Question: Need help with this java assignment. Comments appreciated. Part 1: Class Expression : This is an abstract class representing those features common all expressions regardless
Need help with this java assignment. Comments appreciated.
Part 1: Class Expression:
This is an abstract class representing those features common all expressions regardless of notation.
| Expression |
| - expression: String = null -tokens: ArrayList = null |
| + Expression( in expr: String): constructor + getExpression() : String + getTokens() : ArrayList # isOperand( in str: String): boolean # isOperator( in str: String): boolean # getOperandValue( in str: String) : double # evaluateSubExpression( in op1: double, in operator: String, in op2: double) : double + isLegal(): boolean [abstract]* + evaluate(): double [abstract] |
Interface for class Expression
public Expression( String str) : This constructor receives a String containing the expression text. It will then break the expressions into tokens (individual symbols separated by spaces) and store the resulting String array into the data member tokenList.
protected static boolean isOperand(String str): returns true if the String parameters is a valid operand (double number)
protected static double getOperandValue(String str): given a String containing a valid operand this method returns the value of the operand as type double.
protected static boolean isOperator(String str): returns true if the String parameter contains one of +, -, *, / or %.
public String getExpression(): returns the expression string.
public ArrayList getTokens(): returns the token list
protected static double evaluateSubExpression( double operand1, String operator, double operand2):This method evaluates a subexpression as:
operand1 operator operand2
and returns the result as type double.
public abstract boolean isLegal(): abstract method to be implement in each subclass.* public abstract double evaluate(): abstract method to be implement in each subclass.
| While it could also make sense to not let the user create an invalid expression in the first place, lets allow the creation of illegal expressions as an excuse to practice more with subclasses and super constructors This raises the question of when to check the legality of the expression. To check legality, we must scan through all the tokens. Should we do this just once and save the result? Or should we do it every time? You can decide what approach to take. If you want to check legality only once, my recommendation is to check if an expression is legal in the Postfix and Prefix constructors. Then set a global private final boolean flag. If you take this approach, isLegal() would not need to be abstract because it would do the same thing regardless of being prefix or postfix isLegal() would just return the boolean flag. Given: package project2;
import java.util.ArrayList;
public abstract class Expression { // Feel free to create other private/protected members, methods, or constructors.
protected String expression; protected ArrayList
public Expression(String expression){
}
public String getExpression() { }
public ArrayList }
protected static boolean isOperand(String token) { }
protected static boolean isOperator(String token) {
}
protected static double getOperandValue(String token) throws NumberFormatException {
}
protected static double evaluateSubExpression(double operand1, String operator, double operand2) {
}
public abstract boolean isLegal(); // See my comment on page 3 of instructions. public abstract double evaluate(); } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
