/* * T01n01.java: This is a straight-forward program that computes * the roots of a quadratic equation (ax^2 + bx + c = 0) given the * coefficients a, b, and c. * * As this is used as a first Java example for students new to Java, * it doesn't contain error handling. */ import java.util.*; // Gives easy access to Java API's "util" package public class T01n01 { final static double INCHES_TO_METERS = 0.0254; // 1 in. = 0.0254 meters final static double POUNDS_TO_KG = 0.4536; // 1 lb. ~= 0.4536 kilograms public static void main (String [] args) { double a, // coefficient of the x^2 term b, // coefficient of the x^1 term c, // coefficient of the x^0 term discriminant, // b^2 -4ac root1, root2; // the roots of the quadratic equation Scanner keyboard = new Scanner (System.in); System.out.println("\nThis program will find the roots of the" + " quadratic equation\nax^2 + bx + c = 0," + " assuming that a is not zero and that" + " the discriminant\nis not negative."); System.out.print("\nEnter the value of a in the equation: "); a = keyboard.nextDouble(); System.out.print("Enter the value of b in the equation: "); b = keyboard.nextDouble(); System.out.print("Enter the value of c in the equation: "); c = keyboard.nextDouble(); discriminant = Math.sqrt(b * b - 4 * a * c); root1 = (-b + discriminant) / (2*a); root2 = (-b - discriminant) / (2*a); System.out.println("\nThe roots of the quadratic equation\n" + "(" + a + ")x^2 + (" + b + ")x + (" + c + ")\n" + "are " + root1 + " and " + root2 + ".\n"); } // main } // class T01n01