JAVA: Write a method public int numberOfRoots() that returns number of roots, wh
ID: 3890616 • Letter: J
Question
JAVA:
Write a method public int numberOfRoots() that returns number of roots, where 3 means infite number of roots.
This is what I have so far , but I do not think it's correct,
public int numberOfRoots()
{
if(a==0 && b==0 && c==0)
return 3;
else if((a==0 && b==0) || b*b - 4*a*c < 0)
return 0;
else if(a == 0 || (b*b - 4*a*c == 0))
return 1;
return 2;
}
when a, b, and c are = 0, you get infinite roots correct?
but how do you mathematically get 0 roots, or 1 root, or 2 roots? what is the if-else statement conditions? Can someone explain it to me?
Explanation / Answer
public int numberOfRoots()
{
d = b * b - 4 * a * c;
if(d > 0)
{
System.out.println("Roots are real and unequal");
root1 = ( - b + Math.sqrt(d))/(2*a);
root2 = (-b - Math.sqrt(d))/(2*a);
System.out.println("First root is:"+root1);
System.out.println("Second root is:"+root2);
}
else if(d == 0)
{
System.out.println("Roots are real and equal");
root1 = (-b+Math.sqrt(d))/(2*a);
System.out.println("Root:"+root1);
return root1;
}
else
{
System.out.println("Roots are imaginary");
}
|
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.