Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event Driven Programming Chapter 5. Sequential Programming Computer-Centric Computer-Centric Program Runs as Programmer Intended Program Runs as Programmer.

Similar presentations


Presentation on theme: "Event Driven Programming Chapter 5. Sequential Programming Computer-Centric Computer-Centric Program Runs as Programmer Intended Program Runs as Programmer."— Presentation transcript:

1 Event Driven Programming Chapter 5

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

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

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

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

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

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

8 Example Program Create a “Whack A Mole” program. Use the whackAmoleBooth object from the amusement park gallery. Create a “Whack A Mole” program. Use the whackAmoleBooth object from the amusement park gallery. Pressing the “M” key makes the mole pop upPressing the “M” key makes the mole pop up Clicking the mouse on the mole makes the bopper try to hit the moleClicking the mouse on the mole makes the bopper try to hit the mole

9 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

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

11 Create Methods World Level or Class Level Methods? World Level or Class Level Methods? WhyWhy Create moleAppear method Create moleAppear method

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

13 Linking Events to Methods Select Specific Key Select Specific Key M  Make Mole Appear M  Make Mole Appear

14 Link Event to Method Select Method to Link Select Method to Link wackAmoleBooth, moleAppears wackAmoleBooth, moleAppears TEST TEST

15 Create Method Create bopMole method Create bopMole method Link to Mouse Click on Mole Event Link to Mouse Click on Mole Event Test Test

16 Run the Program Try to Whack The Mole Try to Whack The Mole Click “M” to make moleAppear Event Handling Method executeClick “M” to make moleAppear Event Handling Method execute Mouse Click on Mole to make bopMole Event Handling Method executeMouse Click on Mole to make bopMole Event Handling Method execute

17 Homework Chapter 5 Chapter 5 5-1 Exercises 5-1 Exercises 1  Flight Simulator1  Flight Simulator 4  Typing Tutor4  Typing Tutor

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

19 Create Scenes/Methods Example: (Textbook page 125) 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.

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

21 Design 2 Events: Click on Person Events: Click on Person Responding Method: SavePerson Responding Method: SavePerson 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)

22 Create Scene Burning Building (building) Burning Building (building) Firetruck (vehicles) Firetruck (vehicles) 3 People – use whichever you want 3 People – use whichever you want RandomGirl1RandomGirl1 RandomGirl2RandomGirl2 RandomGuy2RandomGuy2 FireAnim – www.alice.org 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\

23 Create 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)

24 SavePerson Method

25 Link Method to Event

26 Test the Program Run the program Run the program VERY Important when using parameters VERY Important when using parameters Run several timesRun several times Number ParametersNumber Parameters Small Number Small Number Large Number Large Number Negative Number Negative Number

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

28 Homework 5-2 5-2 Problems 12 & 14Problems 12 & 14


Download ppt "Event Driven Programming Chapter 5. Sequential Programming Computer-Centric Computer-Centric Program Runs as Programmer Intended Program Runs as Programmer."

Similar presentations


Ads by Google