Package util
Class MathExpressionManager
java.lang.Object
util.MathExpressionManager
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 Summary
FieldsModifier and TypeFieldDescriptionprivate int
Whether to compute trigonometric functions in degrees, rads or grads.private ArrayList<MathExpression>
private int
Determines the maximum number of MathExpression objects that objects of this class use to optimize an evaluation of an expression. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
boolean
canOptimizeFunction
(String expression) checks if the MathExpression object passed to it as an argument can be optimized or notboolean
contains
(MathExpression func) createFunction
(String expr) int
getFunction
(MathExpression function) getFunctionAt
(int index) getFunctionByExpression
(String expression) getFunctionByScanner
(ArrayList<String> scanner) int
A workspace uses a MathExpressionManager to manage and optimize functions used in calculations.boolean
isEmpty()
optimizeFunction
(String name) void
removeFunctionAt
(int index) removes the MathExpression object at the specified locationvoid
removeFunctionByName
(String funcName) Removes the MathExpression object in the store that goes by that namevoid
removeFunctionByScanner
(ArrayList<String> scanner) removes the MathExpression object that has that scannervoid
setDrgStatus
(int drgStatus) void
setFunctions
(ArrayList<MathExpression> functions) void
setMaxSize
(int maxSize) Takes a math expression, optimizes it if possible, and then solves it.void
storeFunction
(MathExpression function) stores a MathExpression in objects of this class.
-
Field Details
-
functions
-
maxSize
private int maxSizeDetermines the maximum number of MathExpression objects that objects of this class use to optimize an evaluation of an expression. -
drgStatus
private int drgStatusWhether to compute trigonometric functions in degrees, rads or grads. Set to 0 for degrees, set to 1 for rads and set to 2 for grads.
-
-
Constructor Details
-
MathExpressionManager
public MathExpressionManager()
-
-
Method Details
-
getFunctions
- Returns:
- all function objects stored by this MathExpressionManager object
-
setFunctions
- Parameters:
functions
- the store of MathExpression objects that this MathExpressionManager object will store
-
getMaxSize
public int getMaxSize() -
setMaxSize
public void setMaxSize(int maxSize) -
setDrgStatus
public void setDrgStatus(int drgStatus) -
getDrgStatus
public int getDrgStatus() -
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
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
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
- Parameters:
func
- the MathExpression object to search for- Returns:
- true if the MathExpression object is found
-
getFunction
-
getFunctionAt
- Parameters:
index
- the location from which we wish to retrieve the MathExpression object- Returns:
- the MathExpression object stored at the specified index.
-
getFunctionByExpression
- 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
- 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
Removes the MathExpression object in the store that goes by that name- Parameters:
funcName
- the String representation of the MathExpression object
-
removeFunctionByScanner
removes the MathExpression object that has that scanner- Parameters:
scanner
- the scanner object to search for in the store
-
optimizeFunction
- 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
- Parameters:
expr
- The expression to be evaluated by the MathExpression to be created.
-
solve
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
-