Skeleton Square Root Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

What is 0.00000000000001 in scientific notation?

Answer:

1.0E-14

Skeleton Square Root Program

Here is a skeleton of the program, with the loop condition translated from math into Java. The absolute value of a double is calculated by Math.abs(double).

class  SquareRoot
{
  public static void main( String[] args ) 
  {
    final double smallValue = 1.0E-14 ;
    double N     = 3.00 ;
    double guess = 1.00 ;

    while ( Math.abs( N/(guess*guess) - 1.0 ) > smallValue )
    {

       // calculate a new value for the guess
       
       
    }

    System.out.println("The square root of " + N + " is " + guess ) ;
  }

}

The formula for updating the guess is:

newGuess = N/(2*oldGuess) + oldGuess/2

In an assignment statement, first the value on the right of the = is calculated. Then that value is copied to the variable on the left. This allows you to complete the program using just one variable, guess.

QUESTION 16:

Complete the program by filling in the loop body. You should be able to type in your answer into the text area. (But nothing else will happen.)