Event Driven Programming Chapter 5 Sequential Programming n

  • Slides: 28
Download presentation
Event Driven Programming Chapter 5

Event Driven Programming Chapter 5

Sequential Programming n n Computer-Centric Program Runs as Programmer Intended Programmer Sets the Order

Sequential Programming n n Computer-Centric Program Runs as Programmer Intended Programmer Sets the Order of Action Programmer Controls the Program’s Flow

Event Driven Programming n n User-Centric Computer User Determines the Order of Actions Programs

Event Driven Programming n n User-Centric Computer User Determines the Order of Actions Programs are Interactive Flow of Control is Determined at Runtime • User Clicks Mouse / Presses Key • Object in Scene Moves to create a Condition

Event n n Something that happens Can be caused by computer or user •

Event n n Something that happens Can be caused by computer or user • Mouse Click • Key Press • System Clock Tick n n Can occur differently each time the program is executed Events are WORLD Level

Event Handling n n Response to an event Action or Sequence of Actions carried

Event Handling n n Response to an event Action or Sequence of Actions carried out when Event Occurs (Methods) Events TRIGGER response Programmer must think of all possible events • Plan a Response for Each (Event Handler)

Linking n Event Handling Methods must be linked to the Specific Event Occurs which

Linking n Event Handling Methods must be linked to the Specific Event Occurs which causes the Event Handling Method to execute

Testing n Testing of Event Driven Programs is much more difficult • Programmer does

Testing n Testing of Event Driven Programs is much more difficult • Programmer does not know flow of program • Flow of Program changes every time program is executed • User’s Actions are difficult to pre-determine n Use Incremental Development and Testing • • • Write a Method Test it Etc.

Example Program n Create a “Whack A Mole” program. Use the whack. Amole. Booth

Example Program n Create a “Whack A Mole” program. Use the whack. Amole. Booth object from the amusement park gallery. • Pressing the “M” key makes the mole pop up • Clicking the mouse on the mole makes the bopper try to hit the mole

Design Storyboards Event: Key Press Letter M Response: Make mole lift out of booth

Design Storyboards Event: Key Press Letter M Response: Make mole lift out of booth Mole says “Get me if you can!” Make mole go back into booth

Design Storyboard Event: Mouse Click on Mole Object Response: Lift Bopper off Booth Move

Design Storyboard Event: Mouse Click on Mole Object Response: Lift Bopper off Booth Move Bopper to Booth towards Mole Lift Bopper off Booth

Create Methods n World Level or Class Level Methods? • Why n Create mole.

Create Methods n World Level or Class Level Methods? • Why n Create mole. Appear method

Linking Event to Method n n Create New Event When a Key is Typed

Linking Event to Method n n Create New Event When a Key is Typed

Linking Events to Methods n n Select Specific Key M Make Mole Appear

Linking Events to Methods n n Select Specific Key M Make Mole Appear

Link Event to Method n Select Method to Link wack. Amole. Booth, mole. Appears

Link Event to Method n Select Method to Link wack. Amole. Booth, mole. Appears n TEST n

Create Method n n n Create bop. Mole method Link to Mouse Click on

Create Method n n n Create bop. Mole method Link to Mouse Click on Mole Event Test

Run the Program n Try to Whack The Mole • Click “M” to make

Run the Program n Try to Whack The Mole • Click “M” to make mole. Appear Event Handling Method execute • Mouse Click on Mole to make bop. Mole Event Handling Method execute

Homework n n Chapter 5 5 -1 Exercises • 1 Flight Simulator • 4

Homework n n Chapter 5 5 -1 Exercises • 1 Flight Simulator • 4 Typing Tutor

Events with Parameters n Remember: Parameters are how we send information to the methods

Events with Parameters n Remember: Parameters are how we send information to the methods • Customize Methods for different objects/values

Create Scenes/Methods n Example: (Textbook page 125) A firetruck has been called to an

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

Design n n n Event 1 – Click on 1 st person Responding Method

Design n n n Event 1 – Click on 1 st person Responding Method – Save person on 1 st floor Event 2 – Click on 2 nd person Responding Method – Save person on 2 nd floor Event 3 – Click on 3 rd person Responding Method – Save person on 3 rd floor

Design 2 n n Events: Click on Person Responding Method: Save. Person Point the

Design 2 n n Events: Click on Person Responding Method: Save. Person Point the ladder at the correct floor (parameter – Which floor) Extend the ladder correct amount (parameter – How far) Specific person (parameter – Which person) slides down ladder to the firetruck Pull the ladder back in correct amount (parameter – How far)

Create Scene n n n Burning Building (building) Firetruck (vehicles) 3 People – use

Create Scene n n n Burning Building (building) Firetruck (vehicles) 3 People – use whichever you want • Random. Girl 1 • Random. Girl 2 • Random. Guy 2 n Fire. Anim – www. alice. org • Gallery • Save As name. a 2 c (not ZIP file) n Put in correct Alice gallery folder: AliceRequiredGallery

Create Method as Before n n World Method or Class Method? Parameters: • which.

Create Method as Before n n World Method or Class Method? Parameters: • which. Floor (object) • which. Person (object) • how. Far (number)

Save. Person Method

Save. Person Method

Link Method to Event

Link Method to Event

Test the Program n n Run the program VERY Important when using parameters •

Test the Program n n Run the program VERY Important when using parameters • Run several times • Number Parameters Small Number n Large Number n Negative Number n

Complex Problems n n Responses to events may involve multiple actions Methods can call

Complex Problems n n Responses to events may involve multiple actions Methods can call other methods • Parameters can be used when methods call methods

Homework n 5 -2 • Problems 12 & 14

Homework n 5 -2 • Problems 12 & 14