event driven programming chapter 5. sequential programming computer-centric computer-centric program...

28
Event Driven Event Driven Programming Programming Chapter 5 Chapter 5

Upload: bryce-osborne

Post on 30-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Event Driven Event Driven ProgrammingProgramming

Chapter 5Chapter 5

Sequential ProgrammingSequential Programming

Computer-CentricComputer-Centric Program Runs as Programmer Program Runs as Programmer

IntendedIntended Programmer Sets the Order of ActionProgrammer Sets the Order of Action Programmer Controls the Program’s Programmer Controls the Program’s

FlowFlow

Event Driven ProgrammingEvent Driven Programming

User-CentricUser-Centric Computer User Determines the Order Computer User Determines the Order

of Actionsof Actions Programs are InteractivePrograms are Interactive Flow of Control is Determined at Flow of Control is Determined at

RuntimeRuntime• User Clicks Mouse / Presses KeyUser Clicks Mouse / Presses Key• Object in Scene Moves to create a Object in Scene Moves to create a

ConditionCondition

EventEvent

Something that happensSomething that happens Can be caused by computer or userCan be caused by computer or user

• Mouse ClickMouse Click• Key PressKey Press• System Clock TickSystem Clock Tick

Can occur differently each time the Can occur differently each time the program is executedprogram is executed

Events are WORLD LevelEvents are WORLD Level

Event HandlingEvent Handling

Response to an eventResponse to an event Action or Sequence of Actions carried Action or Sequence of Actions carried

out when Event Occurs (Methods)out when Event Occurs (Methods) Events TRIGGER responseEvents TRIGGER response Programmer must think of all Programmer must think of all

possible eventspossible events• Plan a Response for Each (Event Plan a Response for Each (Event

Handler)Handler)

LinkingLinking

Event Handling Methods must be Event Handling Methods must be linked to the Specific Eventlinked to the Specific Event

Event Occurs which causes the Event Event Occurs which causes the Event Handling Method to executeHandling Method to execute

TestingTesting Testing of Event Driven Programs is much Testing of Event Driven Programs is much

more difficultmore difficult• Programmer does not know flow of programProgrammer does not know flow of program• Flow of Program changes every time program Flow of Program changes every time program

is executedis executed• User’s Actions are difficult to pre-determineUser’s Actions are difficult to pre-determine

Use Incremental Development and TestingUse Incremental Development and Testing• Write a MethodWrite a Method• Test itTest it• Write a MethodWrite a Method• Test itTest it• Etc. Etc.

Example ProgramExample Program

Create a “Whack A Mole” program. Create a “Whack A Mole” program. Use the whackAmoleBooth object Use the whackAmoleBooth object from the amusement park gallery.from the amusement park gallery.• Pressing the “M” key makes the mole Pressing the “M” key makes the mole

pop uppop up• Clicking the mouse on the mole makes Clicking the mouse on the mole makes

the bopper try to hit the molethe bopper try to hit the mole

Design StoryboardsDesign Storyboards

Event: Key Press Letter MEvent: Key Press Letter M

Response: Response:

Make mole lift out of boothMake mole lift out of booth

Mole says “Get me if you can!”Mole says “Get me if you can!”

Make mole go back into boothMake mole go back into booth

Design StoryboardDesign Storyboard

Event: Mouse Click on Mole ObjectEvent: Mouse Click on Mole Object

Response:Response:

Lift Bopper off BoothLift Bopper off Booth

Move Bopper to Booth towards MoleMove Bopper to Booth towards Mole

Lift Bopper off BoothLift Bopper off Booth

Create MethodsCreate Methods

World Level or Class Level Methods?World Level or Class Level Methods?• WhyWhy

Create moleAppear methodCreate moleAppear method

Linking Event to MethodLinking Event to Method

Create New EventCreate New Event When a Key is TypedWhen a Key is Typed

Linking Events to MethodsLinking Events to Methods

Select Specific KeySelect Specific Key M M Make Mole Appear Make Mole Appear

Link Event to MethodLink Event to Method

Select Method to LinkSelect Method to Link wackAmoleBooth, moleAppearswackAmoleBooth, moleAppears

TESTTEST

Create MethodCreate Method

Create bopMole methodCreate bopMole method Link to Mouse Click on Mole EventLink to Mouse Click on Mole Event TestTest

Run the ProgramRun the Program

Try to Whack The MoleTry to Whack The Mole• Click “M” to make moleAppear Event Click “M” to make moleAppear Event

Handling Method executeHandling Method execute• Mouse Click on Mole to make bopMole Mouse Click on Mole to make bopMole

Event Handling Method executeEvent Handling Method execute

HomeworkHomework

Chapter 5Chapter 5 5-1 Exercises5-1 Exercises

• 1 1 Flight Simulator Flight Simulator• 4 4 Typing Tutor Typing Tutor

Events with ParametersEvents with Parameters

Remember: Parameters are how we Remember: Parameters are how we send information to the methodssend information to the methods

• Customize Methods for different Customize Methods for different objects/valuesobjects/values

Create Scenes/MethodsCreate Scenes/Methods

Example: (Textbook page 125)Example: (Textbook page 125)A firetruck has been called to an A firetruck has been called to an emergency in a burning building. A emergency in a burning building. A person and a fire object has been person and a fire object has been placed on each floor. When each placed on each floor. When each person is selected, the truck will person is selected, the truck will need to extend its ladder so that need to extend its ladder so that each person can climb down to each person can climb down to safety.safety.

DesignDesign Event 1 – Click on 1Event 1 – Click on 1stst person person Responding Method – Save person on 1Responding Method – Save person on 1stst

floorfloor

Event 2 – Click on 2Event 2 – Click on 2ndnd person person Responding Method – Save person on 2Responding Method – Save person on 2ndnd

floorfloor

Event 3 – Click on 3Event 3 – Click on 3rdrd person person Responding Method – Save person on 3Responding Method – Save person on 3rdrd

floorfloor

Design 2Design 2

Events: Click on PersonEvents: Click on Person Responding Method: SavePersonResponding Method: SavePerson

Point the ladder at the correct floor (parameter – Point the ladder at the correct floor (parameter – Which floor)Which floor)

Extend the ladder correct amount (parameter – Extend the ladder correct amount (parameter – How far)How far)

Specific person (parameter – Which person) Specific person (parameter – Which person) slides down ladder to the firetruckslides down ladder to the firetruck

Pull the ladder back in correct amount Pull the ladder back in correct amount (parameter – How far)(parameter – How far)

Create SceneCreate Scene Burning Building (building)Burning Building (building) Firetruck (vehicles)Firetruck (vehicles) 3 People – use whichever you want3 People – use whichever you want

• RandomGirl1 RandomGirl1 • RandomGirl2RandomGirl2• RandomGuy2RandomGuy2

FireAnim – FireAnim – www.alice.orgwww.alice.org • GalleryGallery• Save As name.a2c (not ZIP file)Save As name.a2c (not ZIP file)

Put in correct Alice gallery folder:Put in correct Alice gallery folder:Alice\Alice\Required\Gallery\Alice\Alice\Required\Gallery\

Create Method as BeforeCreate Method as Before

World Method or Class Method?World Method or Class Method? Parameters:Parameters:

• whichFloor (object)whichFloor (object)• whichPerson (object)whichPerson (object)• howFar (number)howFar (number)

SavePerson MethodSavePerson Method

Link Method to EventLink Method to Event

Test the ProgramTest the Program

Run the program Run the program VERY Important when using VERY Important when using

parametersparameters• Run several timesRun several times• Number ParametersNumber Parameters

Small NumberSmall Number Large NumberLarge Number Negative NumberNegative Number

Complex ProblemsComplex Problems

Responses to events may involve Responses to events may involve multiple actionsmultiple actions

Methods can call other methodsMethods can call other methods• Parameters can be used when methods Parameters can be used when methods

call methodscall methods

HomeworkHomework

5-25-2• Problems 12 & 14Problems 12 & 14