Today is Thursday July 11, 2013.

These are the notes as written in class (lecture). 

http://en.wikipedia.org/wiki/Magic_square

int n;

int[] n; 

n = new int[10]; 

int n[]; 

n = new int[20];

int[][] n; // declaration of a two dimensional array

int[] n[]; 

int n[][]; 

n = new int[3][4]; // 3 rows and 4 columns

n = new int[4][]; // is this correct? 

n = new int[][5]; // is this correct? 

Let's make some tests in DrJava:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int[][] n 
> n
null
> n = new int[4][]
{ null, null, null, null }
> n
{ null, null, null, null }
> n = new int[4][5]
{ { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }
Invalid top level statement
> n = new int[][];
Invalid top level statement
> n = new int[][7];
Invalid top level statement


Now the minute paper for today (part one): 

import java.util.*;
class SixtyOne {
  public static void main(String[] args) {
    int[][] a = { {1, 2, 3}, {4, 5, 6} };
    int[] b = {7, 8, 9};
    for (int[] row : a)
      System.out.println(Arrays.toString( row ));
    System.out.println("-----------------------------------");   
    // what to do here
    // a = Arrays.copyOf(a, a.length + 1);
    int[][] c = new int[a.length + 1][];
    System.out.println(Arrays.toString(c));
    for (int i = 0; i < a.length; i++) {
      c[i] = a[i];
    }
    c[c.length-1] = b;
    System.out.println(Arrays.toString(c));
    for (int[] row : c)
      System.out.println(Arrays.toString( row ));
    // a[a.length-1] = b;
    a = c;
    System.out.println("-----------------------------------");
    for (int[] row : a)
      System.out.println(Arrays.toString( row ));
  }
}

The second part of the minute paper: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> run Two
[]
Enter:  [DrJava Input Box]
[[16, 3, 2, 13]]
Enter:  [DrJava Input Box]
[[16, 3, 2, 13], [5, 10, 11, 8]]
Enter:  [DrJava Input Box]
[[16, 3, 2, 13], [5, 10, 11, 8], [9, 6, 7, 12]]
Enter:  [DrJava Input Box]
[[16, 3, 2, 13], [5, 10, 11, 8], [9, 6, 7, 12], [4, 15, 14, 1]]
Enter:  [DrJava Input Box]
34
34
34
34


This almost solves P6.18 and here's the code:

import java.util.*; 

class Two {
  public static void main(String[] args) {
    ArrayList<ArrayList<Integer>> matrix = new ArrayList<ArrayList<Integer>>(); 
    System.out.println( matrix ); 
    Scanner a = new Scanner(System.in);  
    System.out.print("Enter: "); 
    String line = a.nextLine(); 
    while (! line.equals("bye")) {
      ArrayList<Integer> row = new ArrayList<Integer>(); // new row every iteration 
      Scanner b = new Scanner(line); 
      while (b.hasNext()) {
        row.add(Integer.parseInt(b.next()));
      }
      matrix.add(row);
      System.out.println( matrix ); 
      System.out.print("Enter: "); 
      line = a.nextLine(); 
    }
    for (int col = 0; col < matrix.get(0).size(); col++) {  
      int sum = 0; 
      for (int row = 0; row < matrix.size(); row++) {
        sum = sum + matrix.get(row).get(col);   
      }
      System.out.println( sum ); 
    }
  }
}

In solving P6.19 use whatever approach is easier for you (int[][] or the one above). 

--