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..
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.
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).
Yes, you are right, I just missed the word Num. Well, maybe he can add it as an extra :P
Never calculate the roots of a quadratic equation like that; it is numerically instable; better read this link for a numerical more stable method.
Originally Posted by sky
Thanks for the link, I didn't know that.
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...:)
Read the link I supplied in my previous reply.