Results 1 to 4 of 4

Thread: Formula class

  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default Formula class

    I want to make a class with the following (pseudo)-API:

    __________________________________________________ _______________

    class: Formula

    public Formula(String formulaInStringForm)

    This constructor takes in a String-representation 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 pseudo-code:

    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 X-input 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 end-result popping out at the top.

    Am I on the good track?

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    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);
      }
    }

  3. #3
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    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.

  4. #4
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by imorio View Post
    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.
    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

  1. changing TF formula
    By o.imen in forum Lucene
    Replies: 0
    Last Post: 09-16-2010, 01:20 PM
  2. Formula Builder
    By rbs100 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2009, 06:57 PM
  3. program for a quadratic formula
    By bbtgirl in forum New To Java
    Replies: 9
    Last Post: 01-30-2009, 06:06 PM
  4. create a formula from a string
    By lemon3 in forum New To Java
    Replies: 4
    Last Post: 07-18-2008, 09:24 PM
  5. What is the formula?
    By yuchuang in forum New To Java
    Replies: 3
    Last Post: 04-30-2007, 10:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •