Results 1 to 7 of 7
 06032013, 12:50 AM #1Member
 Join Date
 May 2013
 Posts
 2
 Rep Power
 0
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);
}
}
 06032013, 01:57 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 19
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.
 06032013, 02:51 AM #3Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,638
 Rep Power
 13
Re: Solving a quadratic equation using java
Cross posted at Create a java program to solve quadratic eqations
 06032013, 03:58 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 15
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 06032013, 09:05 AM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
Re: Solving a quadratic equation using java
Build a wall around Donald Trump; I'll pay for it.
 06032013, 12:46 PM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 15
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 06032013, 03:20 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads

Quadratic equation help please.
By proPAGANda in forum New To JavaReplies: 3Last Post: 10092012, 01:36 PM 
Write a java program to display a gui of quadratic equation
By bestlita in forum New To JavaReplies: 4Last Post: 07082011, 06:35 AM 
Quadratic Equation Code Question
By meangrant in forum New To JavaReplies: 5Last Post: 10312010, 03:22 AM 
Quadratic Equation
By jpnym15 in forum New To JavaReplies: 4Last Post: 11122008, 03:29 AM 
Help with quadratic equation in java
By paul in forum New To JavaReplies: 1Last Post: 07252007, 08:23 PM
Bookmarks