Class MathExpressionManager

java.lang.Object
util.MathExpressionManager

public class MathExpressionManager extends Object
Objects of this class attempt to optimize and then evaluate a math expression. If the expression's MathExpression object is present in the functions database (actually an ArrayList of MathExpression objects) then it just retrieves the scanned and validated code and solves it. Else,it must go through all the steps and then solve it. This happens when: 1. The expression is a new one. 2. The expression has been computed before but to limit memory usage, it has been deleted from memory.
  • Field Details

    • functions

      private ArrayList<MathExpression> functions
    • maxSize

      private int maxSize
      Determines the maximum number of MathExpression objects that objects of this class use to optimize an evaluation of an expression.
  • Constructor Details

    • MathExpressionManager

      public MathExpressionManager()
  • Method Details

    • getFunctions

      public ArrayList<MathExpression> getFunctions()
      Returns:
      all function objects stored by this MathExpressionManager object
    • setFunctions

      public void setFunctions(ArrayList<MathExpression> functions)
      Parameters:
      functions - the store of MathExpression objects that this MathExpressionManager object will store
    • getMaxSize

      public int getMaxSize()
    • setMaxSize

      public void setMaxSize(int maxSize)
    • getVariableManager

      public VariableManager getVariableManager()
      A workspace uses a MathExpressionManager to manage and optimize functions used in calculations. Each of these functions have different VariableManager objects but all these objects are actually accessing, storing creating and modifying their variables and constants from one single Variable database( actually an ArrayList of Variable objects ) in the program. This method hence allows an object of this class to return a reference to any of the VariableManager objects associated with the MathExpression objects it manages,since it knows that the destination database(ArrayList) is the same,i.e all the MathExpression objects are only accessing the same set and copy of variables and constants.
      Returns:
      the VariableManager object associated with the first MathExpression object stored by objects of this class.
    • canOptimizeFunction

      public boolean canOptimizeFunction(String expression)
      checks if the MathExpression object passed to it as an argument can be optimized or not
      Parameters:
      expression - : The expression to evaluate
      Returns:
      true if the MathExpression is optimizable
    • storeFunction

      public void storeFunction(MathExpression function)
      stores a MathExpression in objects of this class. The storage is done in such a way that the object is inserted from the front.
      Parameters:
      function - the new MathExpression to store
    • contains

      public boolean contains(MathExpression func)
      Parameters:
      func - the MathExpression object to search for
      Returns:
      true if the MathExpression object is found
    • getFunction

      public MathExpression getFunction(MathExpression function)
    • getFunctionAt

      public MathExpression getFunctionAt(int index)
      Parameters:
      index - the location from which we wish to retrieve the MathExpression object
      Returns:
      the MathExpression object stored at the specified index.
    • getFunctionByExpression

      public MathExpression getFunctionByExpression(String expression) throws NullPointerException
      Parameters:
      expression - the String representation of the MathExpression object
      Returns:
      the MathExpression object in the store that goes by that name or null if none is found.
      Throws:
      NullPointerException
    • getFunctionByScanner

      public MathExpression getFunctionByScanner(ArrayList<String> scanner)
      Parameters:
      scanner - the scanner object to search for in the store
      Returns:
      the MathExpression object in the store that possesses that scanned form
    • removeFunctionAt

      public void removeFunctionAt(int index)
      removes the MathExpression object at the specified location
      Parameters:
      index - the location from which the MathExpression is to be removed
    • removeFunctionByName

      public void removeFunctionByName(String funcName)
      Removes the MathExpression object in the store that goes by that name
      Parameters:
      funcName - the String representation of the MathExpression object
    • removeFunctionByScanner

      public void removeFunctionByScanner(ArrayList<String> scanner)
      removes the MathExpression object that has that scanner
      Parameters:
      scanner - the scanner object to search for in the store
    • optimizeFunction

      public MathExpression optimizeFunction(String name)
      Parameters:
      name - the name or String format of the MathExpression object to be optimized
      Returns:
      the optimized MathExpression object such that it is given its attributes already processed and ready for use
    • isEmpty

      public boolean isEmpty()
      Returns:
      true if this object has no MathExpression objects to manage.
    • createFunction

      public MathExpression createFunction(String expr)
      Parameters:
      expr - The expression to be evaluated by the MathExpression to be created.
    • solve

      public String solve(String expr) throws NullPointerException
      Takes a math expression, optimizes it if possible, and then solves it. Else if the expression cannot be optimized, it has to interpret and then evaluate it. It then stores the expression.
      Parameters:
      expr - The expression to evaluate.
      Returns:
      the result.
      Throws:
      NullPointerException
    • analyzeSolution

      public static String analyzeSolution(MathExpression expr)