Now an exercise. 

Tomorrow come with a summary on search, insert, remove. 

And use it when I ask you to. 

Exercise for today:

start with empty BST then add 5
                          add 7
                          add 3 
                          add 4 (up to here one drawing)
                          remove 3 (redraw tree please)
                          add 6
                          add 2
                          remove 4 (new drawing please) 


Now please build a magic square of size 3:

  
  (a) draw 3x3 matrix 
  (b) goal is to put numbers 1..3^2 in it 
  (c) start with the middle of the bottom row and put 1 in there 
  (d) then keep putting the next numbers 2, 3, 4, ... as follows:
          go down and to the right wrapping around if needed
          if you find an empty cell put the number there 
          if not go up one level and put the number there 


 4 9 2
 3 5 7
 8 1 6

In general size is n the numbers 1 .. n^2 so the sum of all numbers
is n^2 * (n^2 + 1) / 2 so per row (or column) the sum should 1/n of
that which means for the 3x3 I expect 3 * (9 + 1) / 2 == 15

 1. The Cass Extension Mechanism 

 2. Introducing ArrayLists

int a; 

int[] b;

int b[]; // I like this better because it tells me that b[i] is an int 

int[] c[]; // c[i] is an array

int[][] c; // same thing

int c[][]; // same thing and I like this best 

c = new int[3][4]; // allocates a matrix with 3 rows and 4 columns

int[][] d = {{1, 2, 3}, {4, 5, 6}}; // one way to declare and allocate a 2x3 matrix 

-- 

Unidimensional arrays --> Multi-dimensional arrays. 

import java.util.Arrays; 

public class Example {
  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println("No data, thank you."); 
    } else {
      int[] n = new int[args.length];
      for (int i = 0; i < n.length; i++) {
        n[i] = Integer.parseInt(args[i]);  
      }
      System.out.println( Arrays.toString( n ) ); 
      int max = n[0], min = n[0], sum = n[0], count = n.length; 
      for (int i = 1; i < count; i++) {
        if (n[i] > max) max = n[i]; 
        if (n[i] < min) min = n[i]; 
        sum += n[i];
      }
      System.out.println("Max: " + max);
      System.out.println("Min: " + min);
      System.out.println("Sum: " + sum);
      System.out.println("Count: " + count);
      System.out.println("Average: " + (double)sum/count);
    }
  }
}

--

import java.util.Scanner; 

public class Example {
  public static void main(String[] args) {
    Scanner read = new Scanner(System.in); 
    System.out.print("Enter: ");
    String answer = read.nextLine(); 
    while (! answer.trim().equalsIgnoreCase("bye")) {
      int number = Integer.parseInt(answer); 
      System.out.println("Square root of " + number + " is " + Math.sqrt(number)); 
      System.out.print("Enter: ");
      answer = read.nextLine(); 
    }
    System.out.println("Thanks, see you later."); 
  }
}

--

Welcome to DrJava.  Working directory is C:\Users\dgerman.ADS\Desktop
> int[] n = {5, 2, 3}
> n
{ 5, 2, 3 }
> n.length
3
> import java.util.Arrays
> Arrays.copyOf(n, n.length + 1)
{ 5, 2, 3, 0 }
> n
{ 5, 2, 3 }
> n = Arrays.copyOf(n, n.length + 1)
{ 5, 2, 3, 0 }
> n
{ 5, 2, 3, 0 }
> n[n.length-1] = 1
1
> n
{ 5, 2, 3, 1 }
>

--

import java.util.Scanner; 
import java.util.Arrays;

public class Example {
  public static void main(String[] args) {
    Scanner read = new Scanner(System.in); 
    int[] ben = new int[0]; 
    System.out.println(Arrays.toString(ben)); 
    System.out.print("Enter: ");
    String answer = read.nextLine(); 
    while (! answer.trim().equalsIgnoreCase("bye")) {
      int number = Integer.parseInt(answer); 
      ben = Arrays.copyOf(ben, ben.length + 1); 
      ben[ben.length-1] = number;
      Arrays.sort(ben); 
      System.out.println(Arrays.toString(ben)); 
      System.out.print("Enter: ");
      answer = read.nextLine(); 
    }
    System.out.println("Thanks, see you later."); 
  }
}

--

Welcome to DrJava.  Working directory is C:\Users\dgerman.ADS\Desktop
> ArrayList<String> names = new ArrayList<String>()
Static Error: Undefined class 'ArrayList'
> import java.util.ArrayList; // auto-import
ArrayList<String> names = new ArrayList<String>()
> names
[]
> names.add("Dustin");
> names
[Dustin]
> names.add("Emma")
true
> names
[Dustin, Emma]
> names.add("Ben")
true
> names.size()
3


--

Welcome to DrJava.  Working directory is C:\Users\dgerman.ADS\Desktop
> import java.util.ArrayList; // auto-import
ArrayList<int> names = new ArrayList<int>()
Static Error: Type has incompatible bounds
> import java.util.ArrayList; // auto-import
ArrayList<Integer> names = new ArrayList<Integer>()
> names
[]
> names.add(3)
true
> names.add(-1)
true
> names
[3, -1]
> names.add(4)
true
> names.add(2)
true
> names
[3, -1, 4, 2]
> names.remove(0)
3
> names
[-1, 4, 2]
> names.remove(1)
4
> names
[-1, 2]


--

Next Tue assignment is to provide your own versions of copyOf, toString, sort.

The lab will be to implement magic squares with array list. 

Tomorrow: come ready (with notes) to search, insert, remove into BSTs.

--