event driven programming

28
Event Driven Event Driven Programming Programming Chapter 5 Chapter 5

Upload: benedict-green

Post on 03-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

Event Driven Programming. Chapter 5. Sequential Programming. Computer-Centric Program Runs as Programmer Intended Programmer Sets the Order of Action Programmer Controls the Program’s Flow. Event Driven Programming. User-Centric Computer User Determines the Order of Actions - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Event Driven Programming

Event Driven Event Driven ProgrammingProgramming

Chapter 5Chapter 5

Page 2: Event Driven Programming

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

Page 3: Event Driven Programming

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

Page 4: Event Driven Programming

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

Page 5: Event Driven Programming

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)

Page 6: Event Driven Programming

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

Page 7: Event Driven Programming

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.

Page 8: Event Driven Programming

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

Page 9: Event Driven Programming

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

Page 10: Event Driven Programming

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

Page 11: Event Driven Programming

Create MethodsCreate Methods

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

Create moleAppear methodCreate moleAppear method

Page 12: Event Driven Programming

Linking Event to MethodLinking Event to Method

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

Page 13: Event Driven Programming

Linking Events to MethodsLinking Events to Methods

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

Page 14: Event Driven Programming

Link Event to MethodLink Event to Method

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

TESTTEST

Page 15: Event Driven Programming

Create MethodCreate Method

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

Page 16: Event Driven Programming

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

Page 17: Event Driven Programming

HomeworkHomework

Chapter 5Chapter 5 5-1 Exercises5-1 Exercises

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

Page 18: Event Driven Programming

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

Page 19: Event Driven Programming

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.

Page 20: Event Driven Programming

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

Page 21: Event Driven Programming

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)

Page 22: Event Driven Programming

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\

Page 23: Event Driven Programming

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)

Page 24: Event Driven Programming

SavePerson MethodSavePerson Method

Page 25: Event Driven Programming

Link Method to EventLink Method to Event

Page 26: Event Driven Programming

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

Page 27: Event Driven Programming

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

Page 28: Event Driven Programming

HomeworkHomework

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