Today: static methods 

Thursday: arrays (unidimensional) 

Chapters 5, 6. 

Next week: chapter 8

Today I will use DrJava

Method: recipe 

A recipe has a name, ingredients

A method can be public or not
                static or not
             have a return type or not (void)
         has name
             zero or more arguments.

Have we ever seen a method? 

(a) Have we ever defined a method? 

Yes, main. 

public class One {
  public static void main(String[] args) {
    System.out.println("Hi there. One's main here.");     
  } 
}

(b) Have we ever invoked methods? 

Erin says: directly or indirectly?

Indirectly: main, all the time. 


public class Two {
  public static void main(String[] args) {
    System.out.println("Hello! This is Two's main."); 
    One.main();     
  } 
}


We went on a tangent and wrote the following 
experiment: 

public class One {
  public static void main(String[] args) {
    System.out.println("Hi there. One's main here...");     
  } 
  public static void main() {
    System.out.println("Is this a shock? Is it?"); 
  }
}

public class Two {
  public static void main(String[] args) {
    System.out.println("Hello! This is Two's main."); 
    One.main();
    One.main(args);
    One.main(null); 
  } 
}

At this point Brad had the following question:

public class Two {
  public static void main(String[] args) {
    System.out.println("Hello! This is Two's main."); 
    One.main(5);
    // One.main(args);
    One.main(null); 
  } 
}

public class One {
  public static void main(String[] args) {
    System.out.println("Hi there. One's main here...");     
  } 
  public static void main(int number) {
    System.out.println("I received " + number); 
  }
}

Come back to the original question and ask
when did we directly invoke useful methods 
(and what methods were they?) Static methods.

System.out.println(...) but not an static method

Scanner duke; // import java.util.Scanner;
duke = new Scanner(System.in); 
...
String line = duke.nextLine(); not a static method

... line.substring(3, 5) ... also not static 

int ... = Math.max(..., ...); 

Other examples: 
  Integer.parseInt(...),
  Double.parseDouble(...), 
  Math.pow(..., ...), 
  Math.sqrt(...), 
  
Let's define the sqrt method in Math. 

We write this as an example of syntax:

public class Three {
  public static void main(String[] args) {
     System.out.println( Three.halve( 3.2 ) );
     System.out.println( Three.halve( -4 ) );
     
  }
  public static double halve(double number) {
    double answer;
    answer = number / 2;
    return answer; 
  }
}

You need to define a sqrt like halve above

Let's decide how we do it by hand. 

Let's work out an example: 6

How do we calculate the square root of 6? 

Note: we need to calculate the square root
with two decimals 

number   counter

  6
           1        
                     d = number - counter * counter
                     if d == 0 stop
                     else if d > 0 ...
                     else // if d < 0 
                       ...
                     modify counter to get closer 

------------

Ian: 

  6

           1        1 * 1 == 1 < 6
           2        2 * 2 == 4 < 6
           3        3 * 3 == 9 > 6

  number   guess

           real answer above 0 

   6       6 * 6 > 6    real answer below 6
   
           answer in (0, 6)

           6 / 2 and try again 3 * 3 > 6

           answer in (0, 3)

           3 / 2 is 1.5 and try again 1.5 * 1.5 == 2.25 < 6

           answer in (1.5, 3)