A Java Course Outline
Using the Java By Dissection book
by Ira Pohl and Charlie McDowell

 



Solution to Homework 3 - Find root (bisection method)

Excellent program, good use of variable names, nice output.

//William Longstreth 
//The purpose of this program is to find where the function
//F(x)=sin(x)-cos(x)
//is equal to zero


class Homework3{
   public static void main(String args[]){
      double x = 0.0, bound1 = 0.0, bound2 = 2.0, decimal = 0.001, residual;
      while( bound2 - bound1 > decimal ){
         x = (bound1 + bound2) / 2.0;
         residual = Math.sin(x) - Math.cos(x);
         if ( residual > 0 )
            bound2 = x;
         else
            bound1 = x;
      }
      System.out.println( "the function F(x)= sin(x) - cos(x)" );
      System.out.println( "equals zero when x=" + x );
  }
}
      

Feel free to report any site problems to the Webmaster, Debra Dolsberry.