I am having trouble with the subject of this post. I currently have the followin
ID: 3644789 • Letter: I
Question
I am having trouble with the subject of this post. I currently have the following for this method...public static void quadratic(int a, int b, int c) {
int root1 = ((-b) + Math.sqrt(Math.pow(b,2) - 4*a*c)) / (2*a);
int root2 = ((-b) - Math.sqrt(Math.pow(b,2) - 4*a*c)) / (2*a);
System.out.println("The roots of the quadratic equation with coefficients (a,b,c) = (" + a + ", " + b + ", " + c + ") are " + root1 + " and " + root2 + ".");
}
Unfortunately, I get a precision error saying that the compiler found a double and requires
an int (the arrow is pointing to my division symbol). However, when I change the root1
and root2 types to double, then the method works. Unfortunately, I HAVE to have my
output as integers. Can someone please help with this?
Explanation / Answer
try type casting double into int int root1 = (int)(((-b) + Math.sqrt(Math.pow(b,2) - 4*a*c)) / (2*a)); int root2 = (int)(((-b) - Math.sqrt(Math.pow(b,2) - 4*a*c)) / (2*a));
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.