Tutorial 11 Lecturer: MacDonald Tutorial: Mon@10:10am 971124 =========== CSC 108, Fall 1997 Tutorial notes, T11 ======================================================================= Topics ====== - First field questions about the project - Give simple examples of: * two-dimensional arrays (ch 6 - Page 228) - arrays of primitive types - arrays of objects * state of Relationships using object diagrams especially classes containing arrays of objects (ch 11.3 - Page 420) * tracing programs. (create a table of values at some point in the code) related to finding loop invariants and proving program correctness. TWO-DIMENSIONAL ARRAYS - arrays are objects but there is special java syntax to create them. - you can have arrays of primitive types, and arrays of objects. - you have to instantiate each array element if you have an array of objects. Arrays of primitive types: int[][] bins; // creates one bins reference space bins = new int[3][4]; // creates 3 x 4 = 12 spaces for type int bins[0][1] = 0; // value at bins reference (row, column) = (0,1) bins[2][2] = 5; // value at bins reference (row, column) = (2,3) System.out.println(bins.length) // number of rows is 3 System.out.println(bins[row].length) // number of columns in row Note: the number of entries in each row may vary, although it is constant in this example. TWO-DIMENSIONAL ARRAY EXAMPLE (pg 229) Go over example on page 229 especially why it says if (column < table[row].length) // Creates and prints a multi-dimensional array. class Multi_Array_Test { public static void main (String[] args) { Multi_Array chart = new Multi_Array (); // Print each element in the array chart.print; System.out.println(); // Prints sum of each column for (int column=0; column<4; column++) { System.out.println("Sum of column "+column+": "+ chart.sum_column(column)); } } } // Stores a 2D array of integers. class Multi_Array { int[][] table = { {28,84,47,72}, {69,26}, {91,40,28}, {42,34,37}, {13,26,57,35} }; public void print () { for (int row=0; row | fields | methods | | | | | `------------------' | | | | ,------------------. | 1 | o--|----|---> | fields | methods | | | | | `------------------' | | | | ,------------------. | 2 | o--|----|---> | fields | methods | | . | | | `------------------' | . | | | | . | | | | | | | ,------------------. | 9 | o--|----|---> | fields | methods | | | | | `------------------' `-----------------' YOUR PROJECT You should really make these object diagrams to help you to understand your project too. For example, in the ColorCylinder class there is an object 'c' that is a Color Set object. You are to change 'c' so that it is an array of 11 different color sets (ColorSet), where each color set is a different shade. ColorSet[] c = new ColorSet[11]; TRACING PROGRAMS INPUT: 3 1 5 10 -3 2 -1 public static void main (String[] args) { DataInputStream in = new DataInputStream(System.in); int num = Integer.parseInt(in.readLine()); int num1 = num; while (num > -1) { num = Integer.parseInt(in.readLine()); if (num > -1) { if (num1 > num) { num1 = num; } } // * trace here as if it was System.out.println(num1+" "+num); } System.out.println("Answer is "+num1); } num1 num 3 3 1 1 1 5 1 10 1 -3 Answer is 1 Show how to trace the program at "*" but don't tell them that at each iteration num1 is the minimum number (greater than -1) seen so far (the loop invariant). Ask them what the program does and then ask them to choose a better name for num1. When they know what the program does, ask how they could improve the program. TRACING PROGRAMS Ask how they would change the program, to find the maximum number greater than -1. INPUT: 3 1 5 10 -3 2 -1 public static void main (String[] args) { DataInputStream in = new DataInputStream(System.in); int num = Integer.parseInt(in.readLine()); int max = num; while (num > -1) { num = Integer.parseInt(in.readLine()); if (num > -1) { if (max < num) { max = num; } } // * trace here as if it was System.out.println(max+" "+num); } System.out.println("Answer is "+max); } num1 num 3 3 3 1 5 5 10 10 10 -3 Answer is 10 TRACING PROGRAMS Trace the iterative fibonacci program below. Given any n > 2 find this program will find f(n). Formula for fibonacci: f(n) = f(n-1) + f(n-2) f(1) = 1 f(2) = 1 public static void main (String[] args) { int fn=0; int fnm2=1; int fnm1=1; for (int i=3;i <= n;i++) { fn = fnm1 + fnm2; // * trace here (System.out.println(fnm2,fnm1,fn,i) fnm2 = fnm1; fnm1 = fn; } System.out.println("f("+n+")="+fn); } If n=6, trace the above program: f(n-2) f(n-1) f(n) fnm2 fnm1 fn i n ---------------------------------- 1 1 2 3 6 1 2 3 4 2 3 5 5 3 5 8 6 OUTPUT: f(6)=8