DOC PREVIEW
Duke CPS 004 - Lecture

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

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

Unformatted text preview:

CompSci 4Chap 6 Sec 2 – Part 2 Mar 22, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements• Read Chapter 6 Tips and Techniques for next time• Assignment 7 outLast time – if/else statementsLast time – Boolean functionsUse Function in a method:What we will do today• Lecture on Chap 6, Part 2– Execution control– Nested ifs– Logical Operators• ClassworkRevisit the Zeus world• Recall theZeus world• Testing this world – two problems– Anything clicked gets zapped– Philosophers can get zapped more than onceProblem• We need conditional execution– Check conditions• Is selected object a philosopher?• If so, has philosopher already been zapped?– Conditional execution• Lightning bolt will be shot or notMultiple Conditions• First problem to solve - restrict Zeus to shoot only philosophers• Four possible conditions must be checked– User could click on one of four philosophersOne Condition• Begin with just one philosopher - homerStart with blank if statementDrag in the who tile, then select == and homer== is a relational operator meaning “is equal to”Add in Do in order blockThe Do in order block is nested inside the ifNow, lightning will strike homer, only if user clickson homer. But, what about the other three philosophers?Logical OperatorsUse Boolean logic operators tocheck more than one conditionhomer or plato or socrates or euripidesDemo • Demo Zeus world with if statement and logic operatorsAbstraction• Multiple conditions, as in this example,– become complex– are difficult to read– are difficult to debug• Better solution – write a Boolean function that checks the conditions• This is an example of abstraction – allowing you to think on a higher plane• Used nested if statements• If one of the conditionsis true, function returnstrue, and nothing else in the function is checked!• If none of the conditionsare true, the function returns falseisPhilosopher functionDemo of isPhilospher functionCompleting the Zeus world• Second problem – how to prevent lightning striking the same philosopher more than once• How do we know if a philosopher has already been struck by lightning?– When zapped, color property is set to black– Checking color is convenient way to check for a previous lightning strikePreventing a duplicate strike• Only need to check for a possible duplicate-strike if we already know a philosopher is clicked– Nest a second if statement inside the first!= is a relational operator meaning “is not equal to”Complete shootBoltDemo• Demo of completed Zeus worldClasswork today• New partners• Redo the ball and goals animation–Start


View Full Document

Duke CPS 004 - Lecture

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

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