130 likes | 252 Views
Review. Multi-Dimensional Array Searching and Sorting in arrays Inheritance Graphics. 2-D Array and Table. Int [][] e = new int[5][6]. 0. 1. 2. 3. 4. 5. 0. 1. 2. 3. 4. e[3][1]. # of Rows and Columns. int [][] e = new int[5][6]; Get No of rows e.length ?
E N D
Review Multi-Dimensional Array Searching and Sorting in arrays Inheritance Graphics
2-D Array and Table • Int [][] e = new int[5][6] 0 1 2 3 4 5 0 1 2 3 4 e[3][1]
# of Rows and Columns • int [][] e = new int[5][6]; • Get No of rows • e.length ? • Get No of columns • e[0].length ? • e[1].length ?
Print A Matrix Matrix is 2-d Array (rectangular) 1 2 3 4 5 6 7 8 9 print: 1 2 3 4 5 6 7 8 9 public static void Print(int[][] A) { int row = A.length; int col = A[0].length; for (int I=0; I<row; I++) for (int j=0; j<col; j++) System.out.prinln(A[I][j] + “ “); }
Matrix Addition 1 2 3 1 1 1 2 3 4 4 5 6 + 2 2 2 = 6 7 8 4 6 4 3 3 3 7 9 7 A B C formula: Cij = Aij + Bij public static int[][] Add(int[][] A, int[][] B) { }
Matrix Multiplication just for your fun 3 2 2 1 8 5 2 1 + 1 1 = 5 3 formula: Cij = Ai * Bj public static int[][] Multiplication(int[][] A, int[][]B) { }
Sequential or Binary Search Can you perform sequential search on A1? A2? Can you perform binary search on A1? A2? A1: A2:
Summary • Binary search • takes advantage of the characteristics of sorted array, can only be performed on a sorted array. • It’s fast • Sequential search • can be performed on both unsorted and sorted array • It’s slow
Selection Sort: Select the smallest oneDemo: http://www-courses.cs.uiuc.edu/~cs125/cs125/_nav/_framepage/notesPage.html
Insertion Sort: Maintain partial and sorted arrayDemo: http://www-courses.cs.uiuc.edu/~cs125/cs125/_nav/_framepage/notesPage.html
Sort Example • Sort students by height • Sort by height, but female student go first • Sort students by height, if there is a tie, smaller studentID stay in front. • Sort students by name • Sort using insertionsort algorithm
Inheritance • Constructor in child class will call constructor in parent class automatically • method overriding • method overloading
File I/O , GUI • see Kiri’s handout: TextFileInputDemo.java TextFileOutputDemo.java ButtonDemo.java