question of the day

36
Question of the Day While walking across a bridge I saw a boat filled with people. Nobody boarded or left the boat, but on board the boat there was not a single person. How is this possible?

Upload: ursula

Post on 15-Feb-2016

19 views

Category:

Documents


0 download

DESCRIPTION

Question of the Day. While walking across a bridge I saw a boat filled with people . Nobody boarded or left the boat, but on board the boat there was not a single person . How is this possible?. Question of the Day. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Question of the Day

Question of the Day

While walking across a bridge I saw a boat filled with people. Nobody boarded or left the boat, but on board the boat there was not a single person.

How is this possible?

Page 2: Question of the Day

Question of the Day

While walking across a bridge I saw a boat filled with people. Nobody boarded or left the boat, but on board the boat there was not a single person.

How is this possible?

Everybody on the boat was married.

Page 3: Question of the Day

LECTURE 18:RECURSION

Page 4: Question of the Day

Humorous Asides

Page 5: Question of the Day

“A journey begins with single step”

Page 6: Question of the Day

“A journey begins with single step”

Page 7: Question of the Day

“A journey begins with single step”

Page 8: Question of the Day

Large problems hard to solve Thinking about & solving small problems

easier Splitting problems into smaller ones

often helps Before you start coding, plan each

assignment Break up large methods with many ifs and

loops Move repeated action into small (private)

methods

Solving Problems

Page 9: Question of the Day

Smaller is Better

Page 10: Question of the Day

Smaller is Better

CENSORED

Page 11: Question of the Day

Smaller is Better (At least for programming)

CENSORED

Page 12: Question of the Day

Should be boring, easy, understandable drone Given its parameters, perform the expected

action Only perform action defined for its

parameters Should not cure cancer

Do not worry about the larger problem Solving entire problem is not this method’s

issue Split into tasks since solving whole

problem hard

Goal of a Java Method

Page 13: Question of the Day

re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems

Recursion

Page 14: Question of the Day

re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems

Recursion

Page 15: Question of the Day

Recursive step simplifies problem to base case(s) Recast using slightly easier version in

recursive step4! = 4 * 3!

See Recursion Work

Page 16: Question of the Day

Recursive step simplifies problem to base case(s) Recast using slightly easier version in

recursive step4! = 4 * 3! = 4 * (3 * 2!)

See Recursion Work

Page 17: Question of the Day

Recursive step simplifies problem to base case(s) Recast using slightly easier version in

recursive step4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))

See Recursion Work

Page 18: Question of the Day

Recursive step simplifies problem to base case(s) Recast using slightly easier version in

recursive step4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))

Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1))

See Recursion Work

Page 19: Question of the Day

Recursive step simplifies problem to base case(s) Recast using slightly easier version in

recursive step4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))

Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1))

After base case, combine solutions in recursive steps = 4 * (3 * 2)

See Recursion Work

Page 20: Question of the Day

Recursive step simplifies problem to base case(s) Recast using slightly easier version in

recursive step4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))

Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1))

After base case, combine solutions in recursive steps = 4 * (3 * 2)

= 4 * 6

See Recursion Work

Page 21: Question of the Day

Recursive step simplifies problem to base case(s) Recast using slightly easier version in

recursive step4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))

Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1))

After base case, combine solutions in recursive steps = 4 * (3 * 2)

= 4 * 6 = 24

See Recursion Work

Page 22: Question of the Day

Very easy to create solution that does not work Infinite recursion occurs if base case never

reached Frame-by-frame stack grows from method

calls StackOverflowError thrown by program

For Recursion To Work

Page 23: Question of the Day

Very easy to create solution that does not workRecursive step must advance toward a base case If there are multiple, which base case is

unimportant Get one step closer to base case at each

recursive call Must check if algorithm works for all

possible inputs

For Recursion To Work

Page 24: Question of the Day

A method is recursive if it calls itself:public static int factorial(int i) { if (i <= 1) {

return 1; } else {

return i * factorial(i - 1); }

}

Recursion in Java

Page 25: Question of the Day

A method is recursive if it calls itself:public static int factorial(int i) { if (i <= 1) {

return 1; } else {

int nextI = i – 1; int result = factorial(nextI); return i * result; }

}

Recursion in Java

Base case: Solution is simple

Page 26: Question of the Day

A method is recursive if it calls itself:public static int factorial(int i) { if (i <= 1) {

return 1; } else {

int nextI = i – 1; int result = factorial(nextI); return i * result; }

}

Recursion in Java

Recursive Step: • Take 1 step to solution

Page 27: Question of the Day

A method is recursive if it calls itself:public static int factorial(int i) { if (i <= 1) {

return 1; } else {

int nextI = i – 1; int result = factorial(nextI); return i * result; }

}

Recursion in Java

Recursive Step: • Take 1 step to solution• Make 1 or more recursive calls

Page 28: Question of the Day

A method is recursive if it calls itself:public static int factorial(int i) { if (i <= 1) {

return 1; } else {

int nextI = i – 1; int result = factorial(nextI); return i * result; }

}

Recursion in Java

Recursive Step: • Take 1 step to solution• Make 1 or more recursive calls• Simple process computes result

Page 29: Question of the Day

Start with check for base case(s) These cases must return blatantly obvious

answer 1+ recursive calls found within

recursive step(s) Write these assuming recursive call works Take 1 step toward base case (not 2, 3, or

10482)

Recursive Method Basics

Page 30: Question of the Day

Start with check for base case(s) These cases must return blatantly obvious

answer 1+ recursive calls found within

recursive step(s) Write these assuming recursive call works Take 1 step toward base case (not 2, 3, or

10482)

Recursive Method Basics

Page 31: Question of the Day

No different than usual tracing we were doing When method called, we add frame for the

call Local variables & parameters shown in

frame (Processors also include line being

executed)

Tracing Recursion

Page 32: Question of the Day

No different than usual tracing we were doing When method called, we add frame for the

call Local variables & parameters shown in

frame (Processors also include line being

executed)

NOT

Tracing Recursion

Page 33: Question of the Day

static int findMin(int[] a, int j) {if (j == a.length - 1) { return a[j];} else { int minFollowing = findMin(a, j+1); return Math.min(a[j], minFollowing);}

}

int[] example1 = { 0 };findMin(example1, 0);

Trace This, Buddy!

Page 34: Question of the Day

static int findMin(int[] a, int j) {if (j == a.length - 1) { return a[j];} else { int minFollowing = findMin(a, j+1); return Math.min(a[j], minFollowing);}

}

int[] example2 = { 2, 3, 0, 1 };findMin(example2, 0);

Trace This, Buddy!

Page 35: Question of the Day

Your Turn

Get into your groups and complete activity

Page 36: Question of the Day

For Next Lecture

Re-read GT3.5 for Friday How do you write a recursive method? Can recursion involve 2 (or more) methods? How do you write a recursive method?

Week #7 weekly assignment available now

Angel also has programming assignment #1 Pulls everything together and shows off

your stuff