Write a method for solving a quadratic equation using the following header: publ
ID: 3555376 • Letter: W
Question
Write a method for solving a quadratic equation using the following header:
public static int solveQuadratic(double[] eqn, double[] roots)
The coefficients of a quadratic equation ax2 + bx + c = 0 are passed to the array eqn and the real roots are stored in roots. The method returns the number of real roots. See Programming Exercise 3.1 on how to solve a quadratic equation.
Write a program that prompts the user to enter values for a, b, and c and displays the number of real roots and all real roots.
Here codes, I tried to run it into live lab but I got it wrong. I don't see whats wrong with it.
import java.util.Scanner;
public class quadraticequation{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Use "print" so that the input can follow the prompt
System.out.print("Enter a value for a: ");
double a = input.nextDouble();
System.out.print("Enter a value for b: ");
double b = input.nextDouble();
System.out.print("Enter a value for c: ");
double c = input.nextDouble();
input.close();
// solveQuadratic function call following this assignment.
double [] roots = new double[2];
solveQuadratic(new double[] { a, b, c }, roots ) ;
System.out.printf("Roots: [%.2f, %.2f] ", roots[0], roots[1]);
System.out.printf("Sum of roots: %.2f ", roots[0] + roots[1]);
}
public static int solveQuadratic(double[] eqn, double[] roots) {
// Set the variables a, b, and c to values in the array
double a = eqn[0],
b = eqn[1],
c = eqn[2];
double discriminant = Math.pow(b, 2) - 4 * a * c;
roots[0] = (-b + Math.sqrt(discriminant )) / (2 * a);
roots[1] = (-b - Math.sqrt(discriminant )) / (2 * a);
return 0;
}
}
Explanation / Answer
import java.util.Scanner;
public class quadraticequation{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Use "print" so that the input can follow the prompt
System.out.print("Enter a value for a: ");
double a = input.nextDouble();
System.out.print("Enter a value for b: ");
double b = input.nextDouble();
System.out.print("Enter a value for c: ");
double c = input.nextDouble();
input.close();
// solveQuadratic function call following this assignment.
double [] roots = new double[2];
if(solveQuadratic(new double[] { a, b, c }, roots ) >=1)
{
System.out.printf(" Roots: [%.2f, %.2f] ", roots[0], roots[1]);
System.out.printf(" Sum of roots: %.2f ", roots[0] + roots[1]);
}
else
System.out.println(" No real roots available");
}
public static int solveQuadratic(double[] eqn, double[] roots) {
// Set the variables a, b, and c to values in the array
double a = eqn[0],b = eqn[1],c = eqn[2];
//double discriminant = Math.pow(b, 2) - 4 * a * c;
if((b*b -4*a*c)>0)
{
roots[0] = (-b + Math.sqrt(b*b -4*a*c)) / (2 * a);
roots[1] = (-b - Math.sqrt(b*b -4*a*c)) / (2 * a);
return 2;
}
else if(b*b == 4*a*c)
{
roots[0] = (-b ) / (2 * a);
roots[1] = -b/(2*a);
return 1;
}
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.