DOC PREVIEW
Duke CPS 004 - Chap 7 Sec 1

This preview shows page 1-2-19-20 out of 20 pages.

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

Unformatted text preview:

CompSci 4 Chap 7 Sec 1 Mar 31, 2005AnnouncementsWhat we will do todayRepetitionExampleBunny.hopOne solutionCounted LoopDemo - Code to hop 6 timesLet’s Modify this animationFirst write whichBrocClosestWrite method bunny.eatBroccoliInfinity times….Slide 14How do we fix this?More Complicated LoopsDemo -Ferris Wheel nested loopsModify bunny.eatBroccoliClasswork todayClasswork (cont)CompSci 4Chap 7 Sec 1Mar 31, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements•Read Chapter 7, Sec 2 for next time•Registration time – CPS 6–CPS 4 prepares you to take CPS 6–For CPS 6 need to know - Objects, methods, conditionals (if), repetition (loop), list or arrays (we will do)–Will review these topics in CPS 6 with Java•Assignment 7 due Tuesday, April 5What we will do today•Lecture on Chap 7, Sec 1–Definite Loops•ClassworkRepetition•In many kind of animations, especially simulation and games, some actions happen again and again–Example•Games where targets randomly appear and are caught or shot down, then appear elsewhere•Actions are made to happen again and again by running an instruction or method more than onceExample•Bunny sneaks into garden and wants to eat broccoli. Bunny needs to hop several times over to broccoliBunny.hop•Makes bunny hop up and down, making a sound and traveling .8 meters total•See code in book•How do we get bunny to hop many times over to the broccoli?One solution•What is the problem with this solution?Counted Loop•A counted loop is an alternative way to write repetitive code•Repeats instructions a counted number of timesDemo - Code to hop 6 times•The loop instruction executes a definite number of times, specified by a count•Using a loop instruction–Saves time–Is convenient, easy to change the count–Can use a function in place of the count (must return a number)Let’s Modify this animation•Want the bunny to hop over to the closest broccoli and eat it•Then hop to the next closest broccoli and eat it•Move broccoli so not all together•Only do with 3 broccoli–easy to expand to more broccoliFirst write whichBrocClosestWrite method bunny.eatBroccoli•Make bunny turn to face closest broccoli•Then hop over to it stopping in front of it•Then eat the broccoli–Make it invisible and move it far awayInfinity times….•If “infinity times” is selected for a loop, loop will run until the program is shut downExample•What happens if we make the other bunny hop up and down infinity times?How do we fix this?•How do we get both bunnies to move, one infinitely and one definitely?More Complicated Loops•It is possible to place a loop within another loop statement, this is nested loops•Example in book: double ferris wheelDemo -Ferris Wheel nested loopsModify bunny.eatBroccoli •For each of the broccoli do–Make bunny turn to face closest broccoli–Then hop over to it stopping in front of it–Then eat the broccoliClasswork today•Copy file fishGameSetup•Write function moveTheSame–14 times do•Each fish moves .1 m towards appropriate tennisball•Then wiggles a littleClasswork (cont)•Write moveForwardRandomly–Do the following 10 times•Two fish move randomly .1 to .5 m each time•One fish moves steady .2 meters each time•Fish move together in same time units•Fish must wiggle after each .1m of movement•Fish that reaches tennisball first (<.15m) says “I win” for 10 seconds•Don’t worry about fish still moving after winner


View Full Document

Duke CPS 004 - Chap 7 Sec 1

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 1
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 1 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 1 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?