Results 1 to 8 of 8
  1. #1
    c3jcarmy is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default Quadratic Expression

    Hi guys,
    I have an assignment of Quadratic Expression and my professor wants me to have these methods:
    get() and set() for three variables

    add(QuadraticExpression) : QuadraticExpression
    substract(QuadraticExpression) : QuadraticExpression
    getRootOne() : double
    getRootTwo() : double
    toString() : String
    getNumRealRoots() : int

    I know how to do all of them except getNumRealRoots() : int
    What is this method for? I need help for the logic..

    Thanks..:)

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    With that name I suppose that he want you to get the roots of the expresion, but I'm surprised that it should return an integer. Are you sure about that? The formula for getting the roots is very simple, the only thing you have to do is compute the solution of the equation a*x^2+b*x+c=0.

    roots = [-b +- sqrt(b^2-4a*c)]/2*a

    As you can see, the real roots can be represented as floats, but not always as integers.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Sky: It's not supposed to return the real roots but to give the count of them. With quadratic equations, that number can be 2, 1, or 0, and is determined by the results of:

    (b * b - 4 * a*c)

    If this is 0, then there is 1 real root, if > 0, then 2 real roots, if < 0, then no real roots (all roots in this situation are complex).
    Last edited by Fubarable; 11-28-2009 at 05:26 PM.

  4. #4
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Yes, you are right, I just missed the word Num. Well, maybe he can add it as an extra :P

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sky View Post
    roots = [-b +- sqrt(b^2-4a*c)]/2*a
    Never calculate the roots of a quadratic equation like that; it is numerically instable; better read this link for a numerical more stable method.

    kind regards,

    Jos

  6. #6
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Thanks for the link, I didn't know that.

  7. #7
    c3jcarmy is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    So..
    This getNumRealRoots() only checks if the result of Math.sqrt(b^2 - 4*a*c)?
    It is not returning any value that I can use in the calculation of the getRootOne() and getRootTwo()?
    Thanks for the expert's help...:)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Read the link I supplied in my previous reply.

    kind regards,

    Jos

Similar Threads

  1. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM
  2. program for a quadratic formula
    By bbtgirl in forum New To Java
    Replies: 9
    Last Post: 01-30-2009, 06:06 PM
  3. Quadratic Equation
    By jpnym15 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 03:29 AM
  4. Quadratic GUI
    By ryn21 in forum New To Java
    Replies: 1
    Last Post: 10-30-2008, 05:58 AM
  5. Help with quadratic equation in java
    By paul in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:23 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
  •