Results 1 to 4 of 4
Thread: Formula class
 02232011, 08:44 PM #1Senior Member
 Join Date
 Aug 2010
 Posts
 127
 Rep Power
 0
Formula class
I want to make a class with the following (pseudo)API:
__________________________________________________ _______________
class: Formula
public Formula(String formulaInStringForm)
This constructor takes in a Stringrepresentation of a formula. The formula may contain the following operators:
(...)
__________
...^...
__________
.../...
...*...
__________
...+...
......
Furthermore the formula may contain the variabele X. X may appear multiple times in the formula, but the variabele must always be named X. The String is read from left to right, the operators are applied in the order of the 4 groups given above. When the String does not represent a legal formula, the behaviour of the constructor is left undefined.
public double getResult(double X)
This methode return f(X), where f is the function given to the constructor.
__________________________________________________ _______________
This class would be very useful in many application, because it gives the user of the program not only the possibility to specify values, but to specify an entire formula. The question is, how do you write such a class?
My first question is, is there a class in the standard library that already does this?
If not, my second question is are there convenient ways to implement such a class?
Implementing reading the bracket operator isn't difficult using recursion. In pseudocode:
when(there is another bracket left in the String)
{
find the matching closing bracket
new Formula(formulaInStringForm.subString(position first bracket+1, position last bracket);
}
However implementing the other operators isn't so easy. My thoughts were to make Objects that excecute these operations, an object that add's 2 numbers for example). All those objects could be put in order into some kind of tree structere. When an Xinput is given, it is send into each of the leaves. The result of 2 leaves are send to the node above them resulting in 1 result. So the whole tree is traversed with the endresult popping out at the top.
Am I on the good track?
 02232011, 09:15 PM #2Senior Member
 Join Date
 Feb 2011
 Posts
 118
 Rep Power
 0
Whew. This isn't a complete solution, but you should wind up with a recursive method like this:
Java Code:public double parse(String formula) { formula = formula.trim(); if (formula.startsWith("(") && formula.endsWith(")")) { // strip off the parentheses return parse(formula.substring(1, (formula.length()  1))); } else if { // handle multiplication by splitting formula on the "*" character String[] operands = formula.split("*"); double product = 1.0; for (String op in operands) { product *= parse(op); } return product; } else if { // etc. } else { return Double.parseDouble(formula); } }
 02232011, 09:25 PM #3Senior Member
 Join Date
 Aug 2010
 Posts
 127
 Rep Power
 0
I think you misunderstood me. The class should the a formula only once as a String and then be able to fill in many X values, one after another, throught a methode. So not:
public double Formula (String formula)
but:
public Formula(String formula)
public double fillIntoFormula(double X)
Also, this isn't about getting the exact implementation, I just want to come up with a concept that is workable without missing more convenient solutions. The idea of a tree consisting of operator objects could work, I think, but would be complex. I want to know if anyone esle has a better/easier solution.
 02232011, 10:38 PM #4Senior Member
 Join Date
 Feb 2011
 Posts
 118
 Rep Power
 0
Ah, right. I forgot about the x business. Put parse() in your Formula class. Assign the formula string to an instance variable. Then implement fillIntoFormula() to call parse() recursively, but change the final else of the big if/else statement to return the value of X.
Trust me, the recursive method is the way to go, not a whole tree of operator objects, as that'd be complete overkill. Besides, if you trace out the recursive calls, you'll see that they'll form a tree.
Similar Threads

changing TF formula
By o.imen in forum LuceneReplies: 0Last Post: 09162010, 01:20 PM 
Formula Builder
By rbs100 in forum Advanced JavaReplies: 1Last Post: 07032009, 06:57 PM 
program for a quadratic formula
By bbtgirl in forum New To JavaReplies: 9Last Post: 01302009, 07:06 PM 
create a formula from a string
By lemon3 in forum New To JavaReplies: 4Last Post: 07182008, 09:24 PM 
What is the formula?
By yuchuang in forum New To JavaReplies: 3Last Post: 04302007, 10:00 PM
Bookmarks