INTRODUCTION TO COMPUTER SCIENCE
Robert Sedgewick and Kevin Wayne


This is the syntax highlighted version of Quadratic.java.

/*************************************************************************
 *  Compilation:  javac Quadratic.java
 *  Execution:    java Quadatic b c
 *  
 *  Solves for the roots of x*x + b*x + c.
 *  Assumes both roots are real valued.
 *
 *  % java Quadratic -3.0 2.0
 *  2.0
 *  1.0
 *
 *  % java Quadratic -1.0 -1.0
 *  1.618033988749895
 *  -0.6180339887498949
 *
 *  Remark:  1.6180339... is the golden ratio.
 *
 *  % java Quadratic 1.0 1.0
 *  NaN
 *  NaN
 *
 *
 *************************************************************************/

public class Quadratic { 

   public static void main(String[] args) { 
       double b = Double.parseDouble(args[0]);
       double c = Double.parseDouble(args[1]);

       double discriminant =  Math.sqrt(b*b - 4.0*c);

       double root1 = (-b + discriminant) / 2.0;
       double root2 = (-b - discriminant) / 2.0;

       System.out.println(root1);
       System.out.println(root2);
   }
}


Last updated: Wed Feb 11 18:07:23 EST 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.