Source Code : Applying the quadratic formula
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Applying the quadratic formula
/*
Java Programming for Engineers
Julio Sanchez
Maria P. Canton
ISBN: 0849308100
Publisher: CRC Press
*/
// Java for Engineers
//Filename: QuadSolv
//Reference: Chapter 24
//Description:
// Applying the quadratic formula
//Requires:
// Keyin class in current directory
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 = 1d;
b = 2d;
c = 3d;
// Solve the discriminant (SQRT (b^2 - 4ac)
discr = Math.sqrt((b * b) - (4 * a * c));
System.out.println("Discriminant = " + discr);
// Determine number of roots
// if discr > 0 equation has 2 real roots
// if discr == 0 equation has a repeated real root
// if discr < 0 equation has imaginary roots
// if discr is NaN equation has no roots
// Test for NaN
if(Double.isNaN(discr))
System.out.println("Equation has no roots");
if(discr > 0)
{
System.out.println("Equation has 2 roots");
root1 = (-b + discr)/2 * a;
root2 = (-b - discr)/2 * a;
System.out.println("First root = " + root1);
System.out.println("Second roor = " + root2);
}
if(discr == 0)
{
System.out.println("Equation has 1 root");
root1 = (-b + discr)/2 * a;
System.out.println("Root = " + root1);
}
if(discr < 0)
System.out.println("Equation has imaginary roots");
}
}
Thank with us