IntQuadratic.java


Below is the syntax highlighted version of IntQuadratic.java from §1.2 Built-in Types of Data.


/*************************************************************************
 *  Compilation:  javac IntQuadratic.java
 *  Execution:    java IntQuadatic b c
 *  
 *  Reads in two *integer* command line parameters b and c and solves
 *  for the roots of x*x + b*x + c. Assumes both roots are real valued.
 *  Illustrates automatic type conversion.
 *
 *  % java IntQuadratic -3 2
 *  2.0
 *  1.0
 *
 *  % java IntQuadratic -1 -1
 *  1.618033988749895
 *  -0.6180339887498949
 *
 *  Remark:  1.6180339... is the golden ratio.
 *
 *  % java IntQuadratic 1 1
 *  NaN
 *  NaN
 *
 *************************************************************************/

public class IntQuadratic { 

   public static void main(String[] args) { 
       int b = Integer.parseInt(args[0]);
       int c = Integer.parseInt(args[1]);

       double sqroot =  Math.sqrt(b*b - 4*c);

       double root1 = (-b + sqroot) / 2;
       double root2 = (-b - sqroot) / 2;

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


Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Mon Jan 31 13:10:13 EST 2011.