DOC PREVIEW
Duke CPS 004 - Chapter 8

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CompSci 4Chap 8 Sec 1 Nov 17, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements• Read Chapter 10, Section 1 and 2 for next time– Sec 1 is a review of variables– Sec 2 introduces arrays• Note: We will NOT cover Chap 8, Sec 2What we will do today• Lecture on Chap 8 Section 1– Recursion• ClassworkRepetition• Sometimes don’t know exactly how many times a block of instructions should be repeated– Repeat until some condition is true– Repetition gets closer to condition being true• Example – Chess, don’t know when in advance how many moves til game endsIndefinite Repetition• When number of repetitions is indefinite– While statement – previously– Recursion - todayRecursion• Many times a structure is identified by a special word– Do in order– Do Together– If/Else–Loop• Recursion– Is NOT a program statement with a special word– Recursion means a method (or function) calls a clone of itselfExample – horse race•Horse race• One horse randomly selected to move forward, repeatedly• First horse to finish line is winnerStoryboard• “do everything again” means the entire method should be repeated – this is recursionraceif one of the horses has wonwinner says “I won”elserandomly choose horse and movedo everything again“Do everything again” -Call race method• Recursion means that a method calls a “clone of itself”raceif one of the horses has wonwinner says “I won”elserandomly choose horse and movecall the race methodStepwise Refinementraceif one of the horses has wonwinner says “I won”elserandomly choose horse and movecall the race methodisGameOver?whichHorseWon?moveRandomHorseForwardisGameOver and WhichHorseWon• isGameOver– Is the finish line < 0.5 meters in front of any horse? If so, game is over– Returns true if game is over•WhichHorseWon– Which horse is within 0.5 meters of finish line?– Returns the horse that wonmoveRandomHorseForward• To choose horse to move forward, use built-in random selection functionrace method• Uses recursion• Where is the “way out”?Classwork today• Written: raceHorse move recursive statement• Write recursive method BunnysMeet– Two bunnys repeatedly hop towards each other at the same time, as long as they are > 3 meters apart. – When bunnys are close (<= 3 meters apart but still at least 2 meters apart, 1 bunny hops once.– When bunnys are too close to jump any more, they bow to each other once at the same time.• Download file:


View Full Document

Duke CPS 004 - Chapter 8

Documents in this Course
Lecture

Lecture

18 pages

Chapter 7

Chapter 7

18 pages

Chapter 9

Chapter 9

15 pages

Java 1

Java 1

24 pages

Java 3

Java 3

11 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

28 pages

Chap 2

Chap 2

16 pages

Graphics

Graphics

20 pages

Lecture

Lecture

12 pages

HTML

HTML

16 pages

Java 1

Java 1

6 pages

Chapter 4

Chapter 4

16 pages

The Plan

The Plan

25 pages

Lecture

Lecture

16 pages

Chapter 6

Chapter 6

21 pages

Lecture

Lecture

18 pages

Lecture

Lecture

23 pages

Lecture

Lecture

16 pages

Lecture

Lecture

19 pages

Lecture

Lecture

12 pages

Lecture

Lecture

5 pages

Lecture

Lecture

26 pages

Lecture

Lecture

16 pages

Chapter 7

Chapter 7

23 pages

Lecture

Lecture

21 pages

Lecture

Lecture

4 pages

Lecture

Lecture

4 pages

Lecture

Lecture

8 pages

Lecture

Lecture

4 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

32 pages

Java

Java

4 pages

CompSci 4

CompSci 4

18 pages

Lecture

Lecture

26 pages

CompSci 4

CompSci 4

12 pages

HTML

HTML

17 pages

Lecture

Lecture

16 pages

Chapter 5

Chapter 5

22 pages

Lecture

Lecture

4 pages

Chapter 4

Chapter 4

10 pages

Chapter 2

Chapter 2

15 pages

Lecture

Lecture

15 pages

Load more
Download Chapter 8
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 8 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 8 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?