public class ExpressionTree extends Object
Constructor and Description |
---|
ExpressionTree(String literal)
Constructs an expression tree representing a single literal.
|
ExpressionTree(String operator,
ExpressionTree left,
ExpressionTree right)
Constructs an expression tree using the given operator at the
root, and the given expression trees as subexpressions.
|
Modifier and Type | Method and Description |
---|---|
double |
evaluate()
Returns the numeric result of the expression.
|
static void |
main(String[] args) |
static ExpressionTree |
parseExpression(String raw)
Returns an expression tree corresponding to a fully-parenthesized
string, such as "(((3+1)*4)/((9-5)+2))
|
String |
toString()
Produce a parenthesized string representation of the expression.
|
public ExpressionTree(String operator, ExpressionTree left, ExpressionTree right)
operator
- presumed to be "+", "-", "*", or "/"left
- representation of the left subexpressionright
- representation of the right subexpressionIllegalArgumentExpression
- if operator is not "+", "-", "*", or "/"public ExpressionTree(String literal)
literal
- a valid numeric stringNumberFormatException
- if literal invalidpublic double evaluate()
public String toString()
public static ExpressionTree parseExpression(String raw)
public static void main(String[] args)