We have seen values, operators, expressions, types, variables, 
assignment statements, conditionals, if statements, the dangling
else problem, loops. Today we explore methods and nested loops. 

Let's say we want to implement Math.sqrt(...)

(a) choose data representation
(b) examples
(c) name of function, contract (signature)
(d) purpose statement
(e) examples
(f) template
(g) flesh template into code
(h) check-expects

We want to do something similar. 

We are given n > 1 and assume n is 6. 

If you tell me: 1.5 I can tell you it's not Math.sqrt(6). 

I can calculate 1.5 * 1.5 and it's not 6 or even close. 

low                          high
                              6
 0 
+------------------------------+
            (0+6)/2                guess = (low + high) / 2 
+--------------+
              high 
    (0+3)/2                        guess = (low + high) / 2
      +--------+
     low 
                                   when do we stop? 

I should stop when I am close enough.

How do I describe "close enough"?

  Math.abs( guess * guess - n ) < 0.00000000001

Is there any other way to describe "good enough"? 

  high - low < 0.00000000000001

public class Monday {
  public static void main(String[] args) {
    String thing = args[0]; 
    double number = Double.parseDouble( thing ); 
    System.out.println( Math.sqrt( number ) ); 
  }
}

Here's how this works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Monday
java.lang.ArrayIndexOutOfBoundsException: 0
    at Monday.main(Monday.java:3)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
> java Monday 6
2.449489742783178
> java Monday 100
10.0
> java Monday 2
1.4142135623730951
> java Monday 3
1.7320508075688772
>

public class Monday {
  public static void main(String[] args) {
    String thing = args[0]; 
    double number = Double.parseDouble( thing ); 
    System.out.println( Monday.sqrt( number ) ); 
  }
  public static double sqrt(double number) {
    double low = 0, high = number; 
    while (high-low > 0.00000001) {
      double guess = (high + low) / 2; 
      if (guess * guess == number) break; 
      if (guess * guess > number) high = guess; 
      else // guess * guess < number
        low = guess;
    }
    return (high + low) / 2;  
  }
}

I want to produce a scalable pattern. 

public class Monday {
  public static void main(String[] args) {
    int size = Integer.parseInt(args[0]); 
    // System.out.println( size ); 
    for (int line = 0; line < size; line++) {
      for (int column = 0; column < size; column++) {
         if (line == 0 || column == size/2)
           System.out.print(" *"); 
         else 
           System.out.print("  "); 
      }
      System.out.println(); 
    }    
  }
}

Let's run it:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> java Monday 13
 * * * * * * * * * * * * *
             *            
             *            
             *            
             *            
             *            
             *            
             *            
             *            
             *            
             *            
             *            
             *            
> java Monday 5
 * * * * *
     *    
     *    
     *    
     *    
> java Monday 9
 * * * * * * * * *
         *        
         *        
         *        
         *        
         *        
         *        
         *        
         *        


Even sized patterns don't look as good (but only by a little). 

Here's another pattern

public class Monday {
  public static void main(String[] args) {
    int size = Integer.parseInt(args[0]); 
    // System.out.println( size ); 
    for (int line = 0; line < size; line++) {
      for (int column = 0; column < size; column++) {
         if (line == column || line + column == size-1)
           System.out.print(" *"); 
         else 
           System.out.print("  "); 
      }
      System.out.println(); 
    }    
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman
> java Monday 9
 *               *
   *           *  
     *       *    
       *   *      
         *        
       *   *      
     *       *    
   *           *  
 *               *
> java Monday 5
 *       *
   *   *  
     *    
   *   *  
 *       *
> java Monday 13
 *                       *
   *                   *  
     *               *    
       *           *      
         *       *        
           *   *          
             *            
           *   *          
         *       *        
       *           *      
     *               *    
   *                   *  
 *                       *
>

Again even sized patterns have a small problem. 

My last 

public class Monday {
  public static void main(String[] args) {
    int size = Integer.parseInt(args[0]); 
    // System.out.println( size ); 
    for (int line = 0; line < size; line++) {
      for (int column = 0; column < size; column++) {
         if (column == size/2 && line > size / 4 || 
             line == size/2 && column < 3 * size / 4 || 
             line + column == size/2 )
           System.out.print(" *"); 
         else 
           System.out.print("  "); 
      }
      System.out.println(); 
    }    
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman
> java Monday 13
             *            
           *              
         *                
       *                  
     *       *            
   *         *            
 * * * * * * * * *        
             *            
             *            
             *            
             *            
             *            
             *            
> java Monday 5
     *    
   *      
 * * *    
     *    
     *    
> java Monday 9
         *        
       *          
     *            
   *     *        
 * * * * * *      
         *        
         *        
         *        
         *        
> java Monday 11
           *          
         *            
       *              
     *     *          
   *       *          
 * * * * * * * *      
           *          
           *          
           *          
           *          
           *          
> java Monday 12
             *          
           *            
         *              
       *                
     *       *          
   *         *          
 * * * * * * * * *      
             *          
             *          
             *          
             *          
             *          
> java Monday 6
       *    
     *      
   *   *    
 * * * *    
       *    
       *    
> java Monday 7
       *      
     *        
   *   *      
 * * * * *    
       *      
       *      
       *      
>

As you can see I could polish this a bit more. 

--