Write a method called quadratic that solves quadratic equations and prints their roots. Recall that a quadratic equation is a polynomial equation in terms of a variable x of the form ax2 + b + c = 0.
Your method should accept the coefficients a, b, and c as parameters and should print the roots of the equation. You may assume that the equation has two real roots, though mathematically this is not always the case.
I am so confused at what to do. I think I have to start:
public static void quadratic (int a, int b, int c)
What would be my next step? It is confusing me because it's only a method and I've seen so many examples of an actual program.
Method for quadratic equation
Page 1 of 14 Replies - 17628 Views - Last Post: 26 November 2010 - 07:57 AM
Replies To: Method for quadratic equation
#2
Re: Method for quadratic equation
Posted 25 November 2010 - 08:40 PM
I would break it up into parts. Solve each part of the quadratic equation separately(http://en.wikipedia.org/wiki/Quadratic_equation)
For example, do the b^2-4ac and store the answer in a variable X. Then take the square root of X..and so on.
For example, do the b^2-4ac and store the answer in a variable X. Then take the square root of X..and so on.
#3
Re: Method for quadratic equation
Posted 25 November 2010 - 08:41 PM
Well, remember that the quadratic formula is relatively simple to use. It looks like this:
(( -b ) +- sqrt(b^2 - 4*a*c))/(2*a)
Where the +- is Plus and minus. So, make two equations, one with the plus and one with the minus and send a b and c through it to get the roots.
(( -b ) +- sqrt(b^2 - 4*a*c))/(2*a)
Where the +- is Plus and minus. So, make two equations, one with the plus and one with the minus and send a b and c through it to get the roots.
This post has been edited by Dogstopper: 25 November 2010 - 08:42 PM
#4 Guest_Rachel*
Re: Method for quadratic equation
Posted 25 November 2010 - 09:29 PM
Dogstopper, on 25 November 2010 - 07:41 PM, said:
Well, remember that the quadratic formula is relatively simple to use. It looks like this:
(( -b ) +- sqrt(b^2 - 4*a*c))/(2*a)
Where the +- is Plus and minus. So, make two equations, one with the plus and one with the minus and send a b and c through it to get the roots.
(( -b ) +- sqrt(b^2 - 4*a*c))/(2*a)
Where the +- is Plus and minus. So, make two equations, one with the plus and one with the minus and send a b and c through it to get the roots.
Is this what I'm looking for?
public static void quadratic(int a, int b, int c){
System.out.println((( -b ) + Math.sqrt(b^2 - 4*a*c))/(2*a));
System.out.println((( -b ) - Math.sqrt(b^2 - 4*a*c))/(2*a));
}
#5
Re: Method for quadratic equation
Posted 26 November 2010 - 07:57 AM
Very close. Instead of
Also, you can't do b^2. Instead use the pow method.
You might also want to put in checks to make sure that Math.pow(b, 2) - 4 * a * c is greater than -1.
-buse
-1*b
Also, you can't do b^2. Instead use the pow method.
Math.pow(b, 2)
You might also want to put in checks to make sure that Math.pow(b, 2) - 4 * a * c is greater than -1.
This post has been edited by Dogstopper: 26 November 2010 - 07:58 AM
Page 1 of 1

New Topic/Question
Reply
MultiQuote







|