Write a method called quadratic that solves quadratic equations and prints their
ID: 642778 • Letter: W
Question
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 a x2 + b x + c = 0.The formula for solving a quadratic equation is:
the code i came up with works except it wants a return in int not double:
public double quadratic(int a, int b, int c) {
int root1 = (-b + Math.sqrt(Math.pow(b,2) - 4 * a * c) / 2 * a);
return(root1);
}
Explanation / Answer
//Note : Cast the value from double to int
//Loss of precision (decimal values of a result value).
//There are two roots for a quadratic equation
//Returns the first root value
public int quadraticRoot1(int a, int b, int c)
{
// cast the final double value to int. There may be loss of precision.
int root1 = (int)(-b + Math.sqrt(Math.pow(b,2) - 4 * a * c)) / 2 * a;
return(root1);
}
//Returns the second root value
public int quadraticRoot2(int a, int b, int c)
{
// cast the final double value to int. There may be loss of precision.
int root2 = (int)(-b - Math.sqrt(Math.pow(b,2) - 4 * a * c)) / 2 * a;
return(root2);
}
---------------------------------------------------------------------------------------------------------
//Testing the methods using java program
//Java program that tests the method quadraticRoot1
//and quadraticRoot2 for three values a,b and c
//QuadraticEquation.java
public class QuadraticEquation
{
public static void main(String[] args)
{
//set values for a,b and c of a equation
int a=1;
int b=-3;
int c=-4;
//Note : roots depends on the descrimination value
//Descrimination = pefect square, two real solutions exists
//Descrimination = 0, One solution exists
//Descrimination > 0, Two real solutions exists
//Descrimination < 0, Two imaginary solutions exists
//Descrimination = Positive but not a perfect squre,
//Two irrational solutions exists
//Calling two static methods quadraticRoot1 and quadraticRoot2 with a,b and c values as
//arguments and returns the root values
System.out.println("Root 1 : "+quadraticRoot1(a, b, c));
System.out.println("Root 2 : "+quadraticRoot2(a, b, c));
}
//Returns the first root value
public static int quadraticRoot1(int a, int b, int c)
{
// cast the final double value to int. There may be loss of precision.
int root1 = (int)(-b + Math.sqrt(Math.pow(b,2) - 4 * a * c)) / 2 * a;
return(root1);
}
//Returns the second root value
public static int quadraticRoot2(int a, int b, int c)
{
// cast the final double value to int. There may be loss of precision.
int root2 = (int)(-b - Math.sqrt(Math.pow(b,2) - 4 * a * c)) / 2 * a;
return(root2);
}
}
----------------------------------------------------------------------------------------------------
Sample output:
Root 1 : 4
Root 2 : -1
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.