1 / 24

Copywrite 2003 Walter Savitch

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.

keon
Download Presentation

Copywrite 2003 Walter Savitch

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. Reading Order • Chapters 1-6, then • Chapter 11 (Recursion), then • Chapter 10, only section 10.1 (vectors), then • Chapter 7 (Inheritance)

  4. 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.

  5. You may need to make an inner class static public class Match { public static class Person { …. }//end Person …. }//end Match

  6. Length Instance Variable • String[] a = new String[10]; • a.length has the value 10 • a.length is read-only; You cannot change it.

  7. 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.

  8. 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; }

  9. 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();

  10. 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

  11. 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;

  12. 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; }

  13. Selection Sort • See text, other type of slides used here

  14. 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

  15. writeVertical Example Static method ClassName.writeVertical(1234); Outputs 1 2 3 4

  16. 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); } }

  17. 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);

  18. 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);

  19. Stack • Like a stack of paper • Last-in/First-out • Used to implement recursion

  20. Successful Recursive Method • Two cases • Case with recursive calls • Stopping case (Base case) with no recursive calls

  21. Binary Search • Search an array to see if a given value is in the array. • Array must be sorted. • Very fast.

  22. 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)

  23. 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; }

  24. 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);

More Related