freegraph
Class ExpressionEvaluator

java.lang.Object
  |
  +--freegraph.ExpressionEvaluator
All Implemented Interfaces:
GraphPlaneItem

public class ExpressionEvaluator
extends java.lang.Object
implements GraphPlaneItem

ExpressionEvaluator takes an expression as a String and evaluates the expression. Values of single character variables may be set. The ExpressionEvaluator uses and ExpressionStack to convert the expression to postfix for evaluation.

ExpressionEvaluator implements GraphPlaneItem to draw the graph of function on GraphPlane.

See Also:
GraphPlane, ExpressionStack

Constructor Summary
ExpressionEvaluator()
          constructs a new ExpressionEvaluator with an empty expression
ExpressionEvaluator(java.lang.String expression)
          constructs a new ExpressionEvaluator with expression
 
Method Summary
 void drawItem(GraphPlane graphPlane, java.awt.Graphics g, char variableChar)
          called by GraphPlane to draw this GraphPlaneItem.
 double evaluate()
          evaluates the expression using the current variable values and returns the result (ExpressionEvaluator does some down casting on the ExpressionItems when evaluating expressions.
 java.awt.Color getColor()
          returns the color of the expression.
 java.lang.String getExpression()
          returns the expression as the String it was set with.
 VariableValues getVariables()
          returns the VariableValues object representing the variables for this expression evaluator.
 void setColor(java.awt.Color newColor)
          sets the color of the expression.
 void setExpression(java.lang.String expression)
          sets the expression for the evaluator.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ExpressionEvaluator

public ExpressionEvaluator()
constructs a new ExpressionEvaluator with an empty expression


ExpressionEvaluator

public ExpressionEvaluator(java.lang.String expression)
                    throws ExpressionException
constructs a new ExpressionEvaluator with expression

Parameters:
expression - the expression to evaluate
Method Detail

setExpression

public void setExpression(java.lang.String expression)
                   throws ExpressionException
sets the expression for the evaluator. The expression is in the form of an infix expression with single character variables. The expression is parsed and converted to postfix using ExpressionStack.

ExpressionException

getExpression

public java.lang.String getExpression()
returns the expression as the String it was set with.


getColor

public java.awt.Color getColor()
returns the color of the expression. This color is used when the expression is drawn as a GraphPlaneItem


setColor

public void setColor(java.awt.Color newColor)
sets the color of the expression. This color is used when the expression is drawn as a GraphPlaneItem


getVariables

public VariableValues getVariables()
returns the VariableValues object representing the variables for this expression evaluator. These values will be used when the expression is evaluated. Use the VariableValues object returned by this method to set the values of variables for this ExpressionEvaluator

Returns:
the variable values used by the ExpressionEvaluator

evaluate

public double evaluate()
evaluates the expression using the current variable values and returns the result (ExpressionEvaluator does some down casting on the ExpressionItems when evaluating expressions. Perhaps there is a design which would avoid this; however, it works fine in this case.)

Returns:
the value of the expression

drawItem

public void drawItem(GraphPlane graphPlane,
                     java.awt.Graphics g,
                     char variableChar)
called by GraphPlane to draw this GraphPlaneItem. Draws the graph of the expression on the graph plane.

Specified by:
drawItem in interface GraphPlaneItem
Parameters:
graphPlane - the GraphPlane on which to draw the expression
g - the Graphics object for graphPlane
variableChar - the varible in the expression for the X axis (typically "x")

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object