pt.tumba.cage
Class MathEvaluator

java.lang.Object
  extended by pt.tumba.cage.MathEvaluator

public class MathEvaluator
extends java.lang.Object

A Mathematic expression evaluator. Currently, the following functions are supported:

  • +, -, *, /, ^, %,
  • cos, sin, tan, acos, asin, atan,
  • sqrt, sqr, log,
  • min, max, ceil, floor, abs, neg, rndr. When the method getValue() is called, a Double object is returned. If it returns null, an error occured while parsing the expression.
     Sample:
     MathEvaluator m = new MathEvaluator("-5-6/(-2) + sqr(15+x)");
     m.addVariable("x", 15.1d);
     System.out.println( m.getValue() );
     

    Author:
    Bruno Martins

    Constructor Summary
    MathEvaluator()
              Constructor for creating an empty MathEvaluator.
    MathEvaluator(java.lang.String s)
              Constructor for Creating a MathEvaluator and assigning a math expression string.
     
    Method Summary
     void addVariable(java.lang.String v, double val)
              Adds a variable and its value in the MathEvaluator.
     void addVariable(java.lang.String v, java.lang.Double val)
              Adds a variable and its value in the MathEvaluator.
     java.lang.Double getValue()
              Evaluates and returns the value of the expression.
     java.lang.Double getVariable(java.lang.String s)
              Gets the variable's value that was assigned previously.
    static void main(java.lang.String[] args)
              Main method, used to run the program in command line.
     void reset()
              Resets the evaluator.
     void setExpression(java.lang.String s)
              Sets the expression to evaluate.
     
    Methods inherited from class java.lang.Object
    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Constructor Detail

    MathEvaluator

    public MathEvaluator()
    Constructor for creating an empty MathEvaluator. You need to use setExpression(String s) to assign a math expression string to it.


    MathEvaluator

    public MathEvaluator(java.lang.String s)
    Constructor for Creating a MathEvaluator and assigning a math expression string.

    Parameters:
    s - A String with the math expression.
    Method Detail

    main

    public static void main(java.lang.String[] args)
    Main method, used to run the program in command line. Usage: java MathEvaluator.main [your math expression].

    Parameters:
    args[] - The command line input, tokenized.

    addVariable

    public void addVariable(java.lang.String v,
                            double val)
    Adds a variable and its value in the MathEvaluator.

    Parameters:
    v - A String with the variable name.
    val - the value for the variable.

    addVariable

    public void addVariable(java.lang.String v,
                            java.lang.Double val)
    Adds a variable and its value in the MathEvaluator.

    Parameters:
    v - A String with the variable name.
    val - the value for the variable.

    getValue

    public java.lang.Double getValue()
    Evaluates and returns the value of the expression.

    Returns:
    Double the numeric value of the expression.

    getVariable

    public java.lang.Double getVariable(java.lang.String s)
    Gets the variable's value that was assigned previously.

    Parameters:
    v - A String with the variable name.
    Returns:
    Double The value for the variable.

    reset

    public void reset()
    Resets the evaluator.


    setExpression

    public void setExpression(java.lang.String s)
    Sets the expression to evaluate.

    Parameters:
    s - A String with the expression to evaluate.