DOC PREVIEW
Duke CPS 004 - Chap 7 Sec 2

This preview shows page 1-2-3-25-26-27 out of 27 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 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 27 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 27 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 27 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 27 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 27 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CompSci 4Chap 7 Sec 2Nov 1, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements• Read Chapter 9.1 for next time• Assignment 6 due Nov 8– Finish classwork from Thursday first…What we will do today• Lecture on Chap 7 Sec 2 and Tips and Tech.– While loop – indefinite loop– Event Loops• ClassworkLast time -Loop – definite number• What is the output?Repetition• Sometimes don’t know exactly how many times a set of instructions are repeated.• Stopping is based on a condition•Example: – Game of Chess, how many moves until win– Stop: when markers are in check mate positionIndefinite Repetition• In programs where number of repetitions not known in advance, can use– While statement (today)– Recursion (Chap 8) - laterWhile statement• While some condition is true– execute instructionsconditiontrue?execute instructionsnoyesexitloopExample• Common feature in popular “action films”is a chase scene• Example: hungry shark chasing fleeing goldfish– Repeat: fish swim away from shark, and shark swim toward fish– Shark swim distance a little more than fish swim distance– Eventually, shark will catch up with fish and eat fishStoryboardWorld.chaseWhile goldfish more than .5 meters from sharkDo in ordershark point at goldfishDo togethershark swim (toward goldfish)goldfish flee (away from shark)shark eat goldfishshark.Swim, shark.eat and goldfish.flee in bookWorld.chaseShark will catch goldfish• How do you know the shark will eventually catch the goldfish?– Shark always moves 0.4 meters towards goldfish– Goldfish moves randomly away from shark at most .2 meters– Shark will eventually catch up, the loop will endGeneral “Rule of Thumb”• As a general rule, a While loop should be written so the loop will eventually end– Requires statements inside the loop change the conditions of the world such that condition for While eventually becomes false• If While loop never ends– Infinite while loopPractice – From Bunny eats Broccoli• Replace with while loop, bunny hops until close to closestBroc (< 1 meter)Practice – Eat Broccoli until all invisibleAssume method bunny.hopToClosestBroccoliAndEatIt existsReWrite with while loop• Eat broccoli (broc1, broc2, and broc3) until gone (all invisible)• Assume at least one of the brocs is visible when startClasswork – Can Cinderella and Handsome Prince ever get together?• Objects– Cinderella, HandsomePrince, ballCinderella, ballPrince•Setup – Cinderella and Prince 10 units each from middle• Game – both Cinderella and Handsome prince move forward randomly– If both reach the middle, they connect!– If either reach the middle alone, then that one starts overSetup• They start by touching hands.– ballCinderella (invisible) is at same location as Cinderella– ballPrince (invisible) is at same location as Prince• They both turn and walk 10 units• They turn and face.PlayRepeat forever• They both move randomly forward• Cinderella gets to the middle first alone.– She turns around– Goes forward 10 units– She turns back around• They are both in middle at same time– Message displayed and stopWorld. My first methodNote: variable, where is it set and used?What is repeated a lot?• Turn around• Move forward 10 units• Turn back aroundSetupturnAround• Turn halfway around• Move forward 10 units, 1 unit at a time• Turn back aroundrandomIntHow to move forward randomly?• Move forward randomly 1 to 3 units, but not past marker ball?• Use randomIntMore Detailed Play• Repeat over and over– Cinderella moves forward 1 to 3 units, but not past the ballCinderella, her spot in the middle– Prince moves forward 1 to 3 units, but not past the ballPrince, his spot in the middle– If both are in the middle, display a message, then stop.– If just one in the middle, then that one starts


View Full Document

Duke CPS 004 - Chap 7 Sec 2

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

Chapter 8

Chapter 8

14 pages

Lecture

Lecture

15 pages

Load more
Download Chap 7 Sec 2
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 Chap 7 Sec 2 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 Chap 7 Sec 2 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?