Results 1 to 7 of 7
  1. #1
    Khanak is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Solving a quadratic equation using java

    Solving a quadratic equation
    Write two methods quadraticEquationRoot1 which takes as input 3 doubles, representing a,b,c and returns the larger of the two roots and quadraticEquationRoot 2 which takes as input 3 doubles, representing a,b, and c (in that order) and returns the smaller of the two roots.

    Note that if there is a double root (because the square root term is 0), your two methods will return the same thing. Finally note that you may assume that the numbers a,b,c are chosen so that the square root is never the square root of a negative number.
    I have the following but I don’t know to the write the second method and how to add a “return”.


    I have below the first method. I am not sure how to write the second method.


    I am not allowed to use Scanner or have print statements *

    double quadraticEquationRoot1(int a, int b, int c)
    {
    if(Math.sqrt(Math.pow(b, 2) - 4*a*c) == 0)
    {
    return -b/(2*a);
    }else
    {
    int root1, root2;
    root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    return Math.max(root1, root2);
    }

    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Solving a quadratic equation using java

    The second method could use Math.min(), I suppose. (Once you have them doing what you want you might consider seeing if they can be simplified)

    What do you mean by "add a return"? The code you posted has return statements.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Solving a quadratic equation using java


  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,375
    Rep Power
    5

    Default Re: Solving a quadratic equation using java

    Due to limitations of floating point math in software you may want to check out Quadratic equation - Wikipedia, the free encyclopedia


    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Solving a quadratic equation using java

    Quote Originally Posted by jim829 View Post
    Due to limitations of floating point math in software you may want to check out Quadratic equation - Wikipedia, the free encyclopedia
    That tip is written in invisible ink, I'm sure; I tell people the same, all the time but all people do is use that same old formula over and over again (preferably using ints ;-), while it is a terrible formula, numerically speaking that is ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,375
    Rep Power
    5

    Default Re: Solving a quadratic equation using java

    It was because of your post within the last few months that I remembered it. I couldn't find the post in which you stated the same thing so I just repeated it and provided a reference.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Solving a quadratic equation using java

    I you're interested in this stuff. google for "Jenkins Traub"; that algorithm numerically finds all roots of any polynomial, smallest to largest and is numerically very stable. There exists a faster version of this algorithm for real values only (the general case finds complex roots).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Quadratic equation help please.
    By proPAGANda in forum New To Java
    Replies: 3
    Last Post: 10-09-2012, 01:36 PM
  2. Replies: 4
    Last Post: 07-08-2011, 06:35 AM
  3. Quadratic Equation Code Question
    By meangrant in forum New To Java
    Replies: 5
    Last Post: 10-31-2010, 03:22 AM
  4. Quadratic Equation
    By jpnym15 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 03:29 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

Tags for this Thread

Posting Permissions

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