Event CS Thinking Example 3

Follow the full solution, then compare it with the other examples linked below.

Example 3

medium
A 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. 1
    Step 1: Events: (1) timer reaches alarm time, (2) user presses 'Snooze', (3) user presses 'Dismiss'.
  2. 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