compsci 4 chap 8 sec 1 nov 17, 2005 prof. susan rodger note: thanks to wanda dann and steve cooper...

14
CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger hanks to Wanda Dann and Steve Cooper for slide idea

Upload: byron-henry

Post on 02-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

CompSci 4Chap 8 Sec 1 Nov 17, 2005

Prof. Susan Rodger

Note: thanks to Wanda Dann and Steve Cooper for slide ideas

Page 2: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

Announcements

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

Page 3: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

What we will do today

• Lecture on Chap 8 Section 1– Recursion

• Classwork

Page 4: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

Repetition

• 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 ends

Page 5: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

Indefinite Repetition

• When number of repetitions is indefinite– While statement – previously– Recursion - today

Page 6: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

Recursion• 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 itself

Page 7: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

Example – horse race

• Horse race

• One horse randomly selected to move forward, repeatedly

• First horse to finish line is winner

Page 8: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

Storyboard

• “do everything again” means the entire method should be repeated – this is recursion

race

if one of the horses has won winner says “I won”else randomly choose horse and move do everything again

Page 9: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

“Do everything again” -Call race method

• Recursion means that a method calls a “clone of itself”

race

if one of the horses has won winner says “I won”else randomly choose horse and move call the race method

Page 10: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

Stepwise Refinementrace

if one of the horses has won winner says “I won”else randomly choose horse and move call the race method

isGameOver? whichHorseWon?

moveRandomHorseForward

Page 11: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

isGameOver 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 won

Page 12: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

moveRandomHorseForward• To choose horse to move forward, use built-in random selection function

Page 13: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

race method• Uses recursion

• Where is the “way out”?

Page 14: CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

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: BunnysInGarden