DOC PREVIEW
Duke CPS 004 - Chapter 4

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

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

Unformatted text preview:

CompSci 4Chap 6 Sec 2 Sep 29, 2005Prof. Susan RodgerAnnouncements• Review for test next time.– Hand out Midterm from last semester• Note – Midterm covered Chap 5, not Chap6!– Old Quizzes will be available on Blackboard– Study classwork and lecture notes• Next assignment handed out after fall break – enjoy your break!What we will do today• Lecture on Chap 6, Sec 2 through page 167– Execution control with if/else statements and Boolean functions– Relational operators– Logical operators• Exam 1 covers up through todays topic.• ClassworkThinking about More Advanced Worlds• How do you build animations like simulations and video games?• Need to write code that involves decisionsExamples of Decisions• A car-race simulation – the driver steers the car around curves and past mile markers– If the car stays on the road the score increases– If the car goes off the road into the stands, the car crashes– If the driver gets the car over the finish line, the time is posted and the driver wins!Logical Expressions• Decision is made based on current conditions.• Condition is checked in a logical expression that evaluates to true or false (Boolean) value.– car on road true– car over finish line falseIf/Else• In Alice, a logical expression is used as the condition in an If/Else control structure• Decisions (using If/Else) are used in– Functions– MethodsExample: Boolean Functions• Suppose we build a simulation system used to train flight controllers• One of the tasks of a flight controller is to be alert for possible collisions in flight spaceStoryboard• Two aircraft – biplane and helicopter• As the biplane moves towards the helicopter we want to make sure they do not collide• If they are too close, they need to adjust their altitude (height)Storyboard (cont)• Two factors in determining whether two aircraft are in danger of collision– distance between them– Vertical distance between them• We can write functions to determine these• Both functions return true if aircraft are too close, otherwise falseisTooCloseByDistanceisTooCloseByDistance:Parameters: aircraft1, aircraft2, minDistanceIf distance between aircraft1 and aircraft2 is less than minDistancereturn trueElsereturn falseUsing a Relational Operator• Use the < relational operator from the World’s built-in functions to check the distance against the minimumis equal tois not equal tois greater thanis greater than or equal tois less thanis less than or equal toImplementing the FunctionVertical Distance Function• To find the difference in altitude, use the built-in distance above function– Don’t know which aircraft is above the other– To avoid a possible negative value, use absolute value of the distanceistooCloseByVerticalStoryboardforwardAndCheckCollisionParameters: aircraft1, aircraft2, distanceaircraft1 move forward distanceIf aircraft1 and aircraft2 are closer than twice distanceavoid collision if they are too close heightwisemove aircraft1 forward twice the distanceImplementation and Calling FunctionadjustForHeightCollisionAvoid CollisionPutting it All Together - DemoDemo and Testing• Try helicopter at different heights– Move up 5 meters– Move up 10 meters– Stay the same– Down 5 metersProblem• The helicopter may go below the ground!• How do we fix this? – Only move down if above a certain distance!– Use nested if’s to check more than one conditionHow do we fix this? Nest ifsAnother Way - Logical Operators• Use Boolean logic operators to check more than one conditionCheck• Where do you get the if?• Do you have to fill all the parts of the if?• Where do you find the relational operators?• Where do you find the logical operators?Classwork


View Full Document

Duke CPS 004 - Chapter 4

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

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 Chapter 4
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 4 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 4 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?