Question: ublic class Expression { private ArrayList tokenList; // Constructor /** * The constructor takes in an expression as a string * and tokenizes it (breaks





ublic class Expression {
private ArrayList
// Constructor
/**
* The constructor takes in an expression as a string
* and tokenizes it (breaks it up into meaningful units)
* These tokens are then stored in an array list 'tokenList'.
*/
Expression(String expressionString) throws IllegalArgumentException{
tokenList = new ArrayList
StringBuilder token = new StringBuilder();
//ADD YOUR CODE BELOW HERE
token.append(expressionString);
//ADD YOUR CODE ABOVE HERE
}
/**
* This method evaluates the expression and returns the value of the expression
* Evaluation is done using 2 stack ADTs, operatorStack to store operators
* and valueStack to store values and intermediate results.
* - You must fill in code to evaluate an expression using 2 stacks
*/
public Integer eval(){
Stack
Stack
//ADD YOUR CODE BELOW HERE
//..
//..
//ADD YOUR CODE ABOVE HERE
return null; // DELETE THIS LINE
}
//Helper methods
/**
* Helper method to test if a string is an integer
* Returns true for strings of integers like "456"
* and false for string of non-integers like "+"
* - DO NOT EDIT THIS METHOD
*/
private boolean isInteger(String element){
try{
Integer.valueOf(element);
}catch(NumberFormatException e){
return false;
}
return true;
}
/**
* Method to help print out the expression stored as a list in tokenList.
* - DO NOT EDIT THIS METHOD
*/
@Override
public String toString(){
String s = new String();
for (String t : tokenList )
s = s + "~"+ t;
return s;
}
public static void main(String[] args) {
Expression e = new Expression("(25+34)");
System.out.println(e);
}
}
Note that a valid expression is always contained within brackets. Thus "(3+1)" is a valid expression but "3+1" is not. This requirement on the brackets ensures that there is a unique order for evaluating the (sub)expressions in the case that an expression has multiple operators. We will return to this issue later in the course when we discuss expression trees In your solution, you may assume that each expression has been parenthesized with proper nest- ing. Therefore, you don't need to worry about operator precedence issues nor checking for balanced parenthesis. Table 1 below lists some valid expressions and their respective values Expression Evaluated Value 2/3) 3 18 10 Table 1: Example expressions with corresponding values Note that a valid expression is always contained within brackets. Thus "(3+1)" is a valid expression but "3+1" is not. This requirement on the brackets ensures that there is a unique order for evaluating the (sub)expressions in the case that an expression has multiple operators. We will return to this issue later in the course when we discuss expression trees In your solution, you may assume that each expression has been parenthesized with proper nest- ing. Therefore, you don't need to worry about operator precedence issues nor checking for balanced parenthesis. Table 1 below lists some valid expressions and their respective values Expression Evaluated Value 2/3) 3 18 10 Table 1: Example expressions with corresponding values
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
