These are the notes for Mon Feb 3 as taken in class. 

Java programs are made out of classes. 

Let's write a method that receives one input (a number)
and returns the square root of the number. You can't use
the Java Math.sqrt(...) method because that's the method
we are actually trying to write. 

1. Define a class, an empty one. 

2. Define a main

3. Define a stub for sqrt

4. The definition of sqrt is based on the following observations:

input is positive

the square root of input is in the range [0, input]

the square root function is monotonus (increasing)

so I can check in the middle the interval 

checking means multiplying your guess by itself and 

making sure it's close enough (10^-12) to the input 

I try in the middle because I like gambling

if I am not lucky I know precisely which side the answe lies

if the guess is too big it's in the lower half

if it's too low it's in the upper half

now keep working on the half of interest

so when do I stop? 

(a) Math.abs(guess * guess - input) <= 0.00000001

So it's missing is a visualization of the interval you're searching in

double low = 0, high = input;

double guess = (low + high) / 2; 

while( ! Math.abs(guess * guess - input) <= 0.00000001 ) {
  if (guess * guess > input) {
    high = guess;     
  } else {
    low = guess; 
  } 

  guess = (low + high) / 2;
}

return guess; 

What's another way to control the loop?

Math.abs( low - high ) < 0.000000001

So try writing the code now:

class Math {
  public static void main(String[] args) {
    System.out.println( java.lang.Math.sqrt( -2 ) ); 
    double value;
    value = Math.sqrt( 2 ); 
    System.out.println( value ); 
    System.out.println( Math.sqrt(16) + " " + java.lang.Math.sqrt(16));
    System.out.println( Math.sqrt(6) + " " + java.lang.Math.sqrt(6));
    System.out.println( sqrt(3) + " " + java.lang.Math.sqrt(3)); 
  }
  public static double sqrt(double input) {
    if (input < 0) input = -1 * input; 
    
    return 3.241592;   
  }
}

Vincent Bailey says: what if input is 0 < input < 1? 




class Monday {
  public static void main(String[] args) {
    System.out.println ( Monday.what(3) - 5 ); // -1
    int u = Monday.what( Monday.what ( Monday.what( 2 ) ) );
    System.out.println( u ); 
  } 
  public static int what(int n) {
    int answer = 0;
    answer = n + 1;
    return answer;
  }
}



class Math {
  public static void main(String[] args) {
    int input = 2;
    double answer = Math.sqrt( input ); 
    System.out.println( answer + " " + java.lang.Math.sqrt( input) );
    System.out.println( Math.sqrt( 6 ) + " " + java.lang.Math.sqrt( 6 ) );
  }
  public static double sqrt(int n) {
    double guess = n;
    
    while( bad(guess, n) ) { 
       guess = 0.5 * (guess + n / guess);    
    }
    
    return guess; 
  }
  public static boolean bad(double guess, double number) {
    return java.lang.Math.abs( guess * guess - number ) > 0.0000000001;
  }
}

There was another approach too:

class Math {
  public static void main(String[] args) {
    int input = 2;
    double answer = Math.sqrt( input ); 
    System.out.println( answer + " " + java.lang.Math.sqrt( input) );
    // System.out.println( Math.sqrt( 6 ) + " " + java.lang.Math.sqrt( 6 ) );
  }
  public static double sqrt(int n) {
    double guess = 0;
    for ( ; guess * guess < n; guess = guess + 0.00000001) {
      // System.out.println ( guess ); 
    }
    return guess; 
  }
}

This method is not as fast as the one above.

--