Java Reference
In-Depth Information
2
ax
++=
bx
c
0
where the coefficients a , b , and c are constants. The solution a quadratic
equation can be attempted by applying the standard quadratic formula:
2
−± −
bbc
4
x
=
2
a
In the quadratic equation the term
2
bc
is called the discriminant. The value of the discriminant is used to deter-
mine if the solution set has two equal real roots, two different real roots, or
no real roots. The following programattempts to find the real roots of a qua-
dratic equation (if they exist) by solving the quadratic formula.
// Java for Engineers
// Filename: QuadSolv
// Reference: Chapter 24
// Description:
//
Applying the quadratic formula
// Requires:
//
Keyin class in current directory
import java.lang.*;
strictfp class QuadSolv
{
public static void main(String[] args)
{
double a, b, c, discr, root1, root2;
// Apllying the quadratic formula
// Obtain sides from user
System.out.println(“Applying the quadratic formula”);
a = Keyin.inDouble(“Enter a: ”);
b = Keyin.inDouble(“Enter b: ”);
c = Keyin.inDouble(“Enter c: ”);
// Solve the discriminant (SQRT (b^2 - 4ac)
discr = Math.sqrt((b * b) - (4*a*c));
System.out.println(“Discriminant=”+discr);
// Determine number of roots
Search WWH ::




Custom Search