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




public class Expression {
private ArrayList tokenList;
// 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 operatorStack = new Stack();
Stack valueStack = new 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);
}
}
//I've done the parsing part (filling the code in the constructor). Just need help for the evaluation part!
Assignment Your task in this assignment is to evaluate arithmetic expressions and calculate their values. The statements are written in a programming language whose syntax is described below. You will use Java to write a parser and an evaluator for expressions written in this language. For simplicity you will only deal with input expressions that are composed of positive integers and the operators that are listed below. +addition increment subtraction multiplication teger division [ ] k: absolute value You may assume that valucs of exprcssions are of typcint in Java and are within the range specificd by that type. Since the subtraction operator is included in the list below, the value of an expression can be either positive or negative. Increment or decrement operations on constants are not allowed in Java. But our language does allow it. In particular, our language has the pre-increment operation which increments values of Assignment Your task in this assignment is to evaluate arithmetic expressions and calculate their values. The statements are written in a programming language whose syntax is described below. You will use Java to write a parser and an evaluator for expressions written in this language. For simplicity you will only deal with input expressions that are composed of positive integers and the operators that are listed below. +addition increment subtraction multiplication teger division [ ] k: absolute value You may assume that valucs of exprcssions are of typcint in Java and are within the range specificd by that type. Since the subtraction operator is included in the list below, the value of an expression can be either positive or negative. Increment or decrement operations on constants are not allowed in Java. But our language does allow it. In particular, our language has the pre-increment operation which increments values of
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
