Java tutorial
/* 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"); } }