A variable-speed digital clock

Allan M. Stavely

last revised July 13, 2006 (except for formatting)


Table of Contents

1. Introduction
2. Threads and atomic operations
3. The main program: the clock class
4. The "model"
4.1. Counting seconds
4.2. Generating clock ticks
5. The "view"
5.1. The clock display
5.2. The clock observer
5.3. A time formatter
6. The control panel
6.1. On-off buttons
6.2. The speed control
6.3. Resetting the time
6.4. Packaging

1. Introduction

This document presents code to implement a variable-speed digital clock that will run in a window on a computer's display screen. The clock will have controls to do the following:

  • set the clock to a given time

  • set the speed of the clock; i.e., the rate at which the seconds "tick" relative to the normal rate

  • make the clock pause or start "ticking" again

  • turn off or on the display of seconds, because at high speeds the rapidly changing display of seconds may be annoying to the observer

  • change between 12-hour am/pm and 24-hour display of the time

The programming language is Java. I chose the above set of features, not because of any particular need, but to allow me to learn about Java and its libraries, particularly the facilities for threading, atomic variables, and graphical user interfaces. For all practical purposes, what you see here is just a programming exercise.

I will not attempt to explain all of the features of Java and its libraries that I use; readers who want all the details should consult the on-line Java reference material or some other reference material on Java.

Disclaimer: I am not an experienced Java programmer. This is my first Java program. Much of the code here was written by imitating examples in [On to Java] by Patrick Henry Winston and Sundar Narasimhan (Addison Wesley, 2001). Of course, I take responsibility for any flaws, including bugs and clumsy use of the Java language.