240 likes | 356 Views
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any other purpose without the written permission of Walter Savitch. wsavitch@ucsd.edu. CSE 11.
E N D
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any other purpose without the written permission of Walter Savitch. wsavitch@ucsd.edu
CSE 11 • HW 2 , 3 and 4 Posted in public and on web • Deadlines HW 2turnin: Wed Jan 22 (already past) interview: Saturday January 25 • .class files in public directory for HW 2 & 3 • Do not need to turnin the HW if you take an interview before the turnin deadline
Reading Order • Chapters 1-6, then • Chapter 11 (Recursion), then • Chapter 10, only section 10.1 (vectors), then • Chapter 7 (Inheritance)
Midterm Warning • In lecture Tuesday February 4. • Quiz grades are not so great. • Pick up graded quizzes from TA. • Do Self-Test Exercises. • Sample midterm on web page and in public.
You may need to make an inner class static public class Match { public static class Person { …. }//end Person …. }//end Match
Length Instance Variable • String[] a = new String[10]; • a.length has the value 10 • a.length is read-only; You cannot change it.
Entire Array as an Argument • Array variables are reference variables, just like class objects. • Array parameters are just like class parameters (Call-by-value for a reference.) • Arrays know their size. One array type for all arrays with the same base type, e.g. double[] is the type for any array of doubles of any size.
public static double[] averageArray (int firstScore, int[] nextScore) { double[] temp = new double[nextScore.length]; int i; for (i = 0; i < temp.length; i++) temp[i] = average(firstScore, nextScore[i]); return temp; }
What is wrong? • Species[] a = new Species[3];a[0].readInput(); • Error message “Null Pointer Exception” • a[0] is a varaible of type Species, but isjust a variable. It has no refernce to any object. • Need a[0] = new Species();
Species[] a = new Species[3]; for (int i = 0; i < a.length; i++) a[i] = new Species(); a[0].readInput(); //OK a[1].readInput(); //OK a[2].readInput(); //OK
Array as Instance Variables public class OneWayNoRepeatsList { public static int START_POSITION = 1; public static int DEFAULT_SIZE = 50; private int countOfEntries; //can be less than entry.length. private String[] entry;
public class OneWayNoRepeatsList { public static int START_POSITION = 1; public static int DEFAULT_SIZE = 50; private int countOfEntries; private String[] entry; public OneWayNoRepeatsList (int maximumNumberOfEntries) { entry = new String[maximumNumberOfEntries]; countOfEntries = 0; }
Selection Sort • See text, other type of slides used here
Recursion (Chapter 11) • Recursive method == one that invokes itself • Anything will compile. • Need some care to get method to run OK. • Read all of Chapter 11
writeVertical Example Static method ClassName.writeVertical(1234); Outputs 1 2 3 4
public static void writeVertical(int n) { if (n < 10) System.out.println(n); else //n is two or more digits long: { writeVertical(n/10); System.out.println(n%10); } }
ClassName.writeVertcal(123);Equivalent towriteVertical(123/10);System.out.println(123%10); Equivalent towriteVertical(12);System.out.println(3); Equivalent towriteVertical(12/10);System.out.println(12%10);System.out.println(3);
writeVertical(12/10);System.out.println(12%10);System.out.println(3); Equivalent towriteVertical(1);System.out.println(2);System.out.println(3); Equivalent to System.out.println(1);System.out.println(2);System.out.println(3);
Stack • Like a stack of paper • Last-in/First-out • Used to implement recursion
Successful Recursive Method • Two cases • Case with recursive calls • Stopping case (Base case) with no recursive calls
Binary Search • Search an array to see if a given value is in the array. • Array must be sorted. • Very fast.
public class ArraySearcher { private int[] a; public ArraySearcher(int[] theArray) { a = theArray } public int find(int target) {return search(target, 0, a.length - 1);} //Searches a[first] through a[last] //Returns the index of target if found. //Returns -1 if target is not found. private int search(int target, int first, int last)
privateint search(int target,int first,int last) { int result = -1;//to keep compiler happy. int mid; if (first > last) result = -1; else { Recursive Case, Next slide } return result; }
mid = (first + last)/2; if (target == a[mid]) result = mid; else if (target < a[mid]) result = search(target, first, mid - 1); else //(target > a[mid]) result = search(target, mid + 1, last);