CompSci 4Chap 6 Sec 2 Sep 27, 2007Prof. Susan RodgerAnnouncements• Review for test next time.– Hand out Test 1 from last semester• Should try it before next class– Old Quizzes will be available on Blackboard– Study classwork and lecture notes• Next assignment handed out after fall break• Today – Chap 6, Sec 2– Execution control – if/else & Boolean functions– Relational operators– Logical OperatorsThinking - More Advanced Worlds• How do you build animations like simulations and video games?• Need to write code that involves decisions• Example car-race simulation– 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 conditionAnother 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?Random Numbers• We will cover this later in more detailClasswork today• Write functions and methods with
View Full Document