Copywrite 2003 Walter Savitch

24
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. [email protected]

description

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. [email protected]. CSE 11. - PowerPoint PPT Presentation

Transcript of Copywrite 2003 Walter Savitch

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

[email protected]

Page 2: Copywrite 2003 Walter Savitch

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

Page 3: Copywrite 2003 Walter Savitch

Reading Order

• Chapters 1-6, then

• Chapter 11 (Recursion), then

• Chapter 10, only section 10.1 (vectors), then

• Chapter 7 (Inheritance)

Page 4: Copywrite 2003 Walter Savitch

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.

Page 5: Copywrite 2003 Walter Savitch

You may need to make an inner class static

public class Match

{

public static class Person

{

….

}//end Person

….

}//end Match

Page 6: Copywrite 2003 Walter Savitch

Length Instance Variable

• String[] a = new String[10];

• a.length has the value 10

• a.length is read-only; You cannot change it.

Page 7: Copywrite 2003 Walter Savitch

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.

Page 8: Copywrite 2003 Walter Savitch

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

Page 9: Copywrite 2003 Walter Savitch

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

Page 10: Copywrite 2003 Walter Savitch

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

Page 11: Copywrite 2003 Walter Savitch

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;

Page 12: Copywrite 2003 Walter Savitch

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

Page 13: Copywrite 2003 Walter Savitch

Selection Sort

• See text, other type of slides used here

Page 14: Copywrite 2003 Walter Savitch

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

Page 15: Copywrite 2003 Walter Savitch

writeVertical Example

Static method

ClassName.writeVertical(1234);

Outputs

1

2

3

4

Page 16: Copywrite 2003 Walter Savitch

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

}

}

Page 17: Copywrite 2003 Walter Savitch

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

Page 18: Copywrite 2003 Walter Savitch

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

Page 19: Copywrite 2003 Walter Savitch

Stack

• Like a stack of paper

• Last-in/First-out

• Used to implement recursion

Page 20: Copywrite 2003 Walter Savitch

Successful Recursive Method

• Two cases

• Case with recursive calls

• Stopping case (Base case) with no recursive calls

Page 21: Copywrite 2003 Walter Savitch

Binary Search

• Search an array to see if a given value is in the array.

• Array must be sorted.

• Very fast.

Page 22: Copywrite 2003 Walter Savitch

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)

Page 23: Copywrite 2003 Walter Savitch

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

Page 24: Copywrite 2003 Walter Savitch

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