Event CS Thinking Example 3
Follow the full solution, then compare it with the other examples linked below.
Example 3
mediumA simple alarm clock app has these features: set alarm time, snooze for 5 minutes, dismiss alarm. Identify the events and describe what each event handler should do.
Solution
- 1 Step 1: Events: (1) timer reaches alarm time, (2) user presses 'Snooze', (3) user presses 'Dismiss'.
- 2 Step 2: Handlers: (1) play alarm sound and show notification, (2) stop sound and reset timer to current time + 5 minutes, (3) stop sound and cancel the alarm.
Answer
Three events: alarm triggers (play sound), snooze pressed (delay 5 min), dismiss pressed (stop alarm).
Breaking an application into events and handlers is a form of decomposition specific to event-driven programming. Each handler is a self-contained response to one event.
About Event
A detectable action or occurrence in a program—such as a user click, key press, mouse movement, or timer expiry—that the program can respond to by executing a predefined event handler function.
Learn more about Event →More Event Examples
Example 1 easy
In a GUI application, a user clicks a 'Submit' button. Describe what happens in terms of events and
Example 2 mediumCompare event-driven programming with sequential programming. Give an example of each.
Example 4 hardIn a game, multiple events can occur simultaneously: a key is pressed, a timer fires, and two object