CS 1063  Lab 9:  Designing and Implementing a Class using the while Loop

The  NewtonsSquareRoot Class


Objectives

Hand-in Requirements

All projects and laboratories will be submitted electronically through webCT. Zip up your entire project directory to submit as the source. (Right click on the project folder and follow the SentTo link.) The project directory should include the following:

Details

    To approximate the square root of a number n using Newton's method, you need to make an initial guess at the root and then refine this guess until it is "close enough."  Your first initial approximation should be root =  n/2.  A sequence of approximations is generated by computing the average of root and n/root.  

Use the constant
  private static final double EPSILON = .00001;

Y
our loop for findSquareRoot should look like this
        make the initial guess for root
    while(  absolute value of the difference of root squared and number >  EPSILON)
          calculate a new root
    return root

Your class should have a constructor that takes the number you want to find the square root of.  The usual accessor method(s) and a findSquareRoot that uses Newtons method described above to find and return the square root of the number.  Add a method setNumber that takes a new number that replaces the number in the instance field.  Supply a toString that returns a string containing the number and the square root of the number.

Your test class

Instantiate a NewtonsSquareRoot object using the number 1.
Print the square root of 1 using the toString method
Use a while loop to generate the numbers 2 through 100.  
 In the loop use setNumber and toString to print the square roots of all the numbers from 2 to 100 inclusive

Last  Modified:  November 1, 2004 at 3:01pm