Howdy. 

Donald Knuth. 

This is a good introductory book: 

http://introcs.cs.princeton.edu/java/home/

This is a good overview of the material for the exam:

http://introcs.cs.princeton.edu/java/home/chapter1.pdf

This is a progression of books of the kind Knuth would like: 

http://algs4.cs.princeton.edu/home/

http://aofa.cs.princeton.edu/home/

http://ac.cs.princeton.edu/home/

We are going to continue to refer to these books:

http://www.cs.indiana.edu/classes/c212-dgerman/fall2015/secure/index.html

Today (as in this week's lab):

(a) more about loops

(b) (static) methods 

Reminder from C211: 

(b) design recipe says that for every function (method) we take these steps: 

1. choose data representation 
2. give some examples
3. name your function
4. write signature
5. write purpose statement
6. examples
7. write header, choose a template
8. write the function 
9. test (check-expects) 

Let's think of Math.sqrt(...)  

Can we define that function? 

What do we do? 

0                                6
---------------------------------+
0               6/2
+----------------+
I try 3 and it's too big so ... 

       3/2       3
        +--------+
            2.25
             +---+

So here's how the program would work:

We first discuss command line arguments

class Thursday {
  public static void main(String[] args) {
    System.out.println( args[0] ); 
    System.out.println( args[1] ); 
    System.out.println( args[2] ); 
    
  }
}

We give some examples of how those work

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Thursday "Thomas" "Adrian" "Andrew"
Thomas
Adrian
Andrew
> java Thursday "Thomas" "Adrian" 
Thomas
Adrian
java.lang.ArrayIndexOutOfBoundsException: 2
    at Thursday.main(Thursday.java:5)
> java Thursday "Thomas" "Adrian" "Andrew" "Trevor"
Thomas
Adrian
Andrew


Understand that command line arguments are Strings:

class Thursday {
  public static void main(String[] args) {
    double n = Double.parseDouble( args[0] ); 
    System.out.println("The square root of " + n + " is " + Math.sqrt(n));
  }
}

This is how this works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Thursday
java.lang.ArrayIndexOutOfBoundsException: 0
    at Thursday.main(Thursday.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)
> run Thursday 6
The square root of 6.0 is 2.449489742783178
> run Thursday 15
The square root of 15.0 is 3.872983346207417
> run Thursday 10
The square root of 10.0 is 3.1622776601683795
> run Thursday 2
The square root of 2.0 is 1.4142135623730951
> run Thursday 3
The square root of 3.0 is 1.7320508075688772
> run Thursday 9
The square root of 9.0 is 3.0


Now I implement my method:

class Thursday {
  public static void main(String[] args) {
    double n = Double.parseDouble( args[0] ); 
    System.out.println("The square root of " + n + " is " + Thursday.sqrt(n));
  }
  public static double sqrt(double n) {
    
    return 3.14;
  }
}

The stub works as follows:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Thursday 9
The square root of 9.0 is 3.14
> java Thursday 6
The square root of 6.0 is 3.14
> java Thursday 10
The square root of 10.0 is 3.14


Now I finish the job: 

class Thursday {
  public static void main(String[] args) {
    double n = Double.parseDouble( args[0] ); 
    System.out.println("The square root of " + n + " is " + Thursday.sqrt(n));
  }
  public static double sqrt(double n) {
    double low = 0, high = n; 
    
    while (high - low > 0.00000001) {
      double guess = (high + low) / 2; 
      if (guess * guess > n) {
        high = guess; 
      } else if (guess * guess < n) {
        low = guess; 
      } else {
         break; // the loop ends 
      }
    }
    
    return (high + low) / 2;
  }
}

Let's test it:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Thursday 6
The square root of 6.0 is 2.449489745311439
> java Thursday 15
The square root of 15.0 is 3.8729833450634032
> java Thursday 3
The square root of 3.0 is 1.7320508072152734
> java Thursday 2
The square root of 2.0 is 1.414213564246893


My method works fine. 

You will work on some easier exercises of this kind in lab. 

Change of topic:

1. Write a number of asterisks on a line.

class Thursday {
  public static void main(String[] args) {
    System.out.println( "************************" ); 
  }
}

2. Write size lines like that where size comes from the user:

while (<COND>) {
  <BODY> 
}

int i = 9; 
while (i < 20) {
  System.out.println( i ); 
  i = i + 1; 
}

int i;
for (i = 9; i < 20; i = i + 1) {
  System.out.println( i );   
}

for (<INIT>; <COND>; <STEP>) {
  <BODY> 
}

<INIT>
while(<COND>) {
  <BODY>
  <STEP> 
}


class Thursday {
  public static void main(String[] args) {
    int size = Integer.parseInt(args[0]); 
    for (int line = 0; line < size; line = line + 1) {  
      System.out.println( "************************" ); 
    }
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Thursday 3
************************
************************
************************
> java Thursday 12
************************
************************
************************
************************
************************
************************
************************
************************
************************
************************
************************
************************


3. Let's make the size of the line be size too. 

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

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

4. Can I carve an "N" out of this block? 

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

Here's how this runs: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Thursday 5
 *       *
 * *     *
 *   *   *
 *     * *
 *       *
> java Thursday 13
 *                       *
 * *                     *
 *   *                   *
 *     *                 *
 *       *               *
 *         *             *
 *           *           *
 *             *         *
 *               *       *
 *                 *     *
 *                   *   *
 *                     * *
 *                       *
> java Thursday 25
 *                                               *
 * *                                             *
 *   *                                           *
 *     *                                         *
 *       *                                       *
 *         *                                     *
 *           *                                   *
 *             *                                 *
 *               *                               *
 *                 *                             *
 *                   *                           *
 *                     *                         *
 *                       *                       *
 *                         *                     *
 *                           *                   *
 *                             *                 *
 *                               *               *
 *                                 *             *
 *                                   *           *
 *                                     *         *
 *                                       *       *
 *                                         *     *
 *                                           *   *
 *                                             * *
 *                                               *
>

See you in lab. 

--