OO Programming Example

I chose a very simple minded programming example for this demonstration. We will be writing a Die object, specifically a 6-sided die.

The 6-Sided die must conform to a standard dice protocol. In other words, it must be able to be tossed (to cause a new face value to appear at the top of the die). It must be able to report the current top face value. It must also be able to report the number of sides (even though our Die object will always have 6 sides, we must exist in a world where multi-sided dice are available).

To exercise our Die object, we will be using a second object, a DieCounter. DieCounters work with a Die object and toss it a number of times, tracking the number of times each face appears on top. A DieCounter will be able to report the number of time each face appears. Note that even though a DieCounter has no special knowledge of our specialize 6 sided die, it is able to use it through the standard Die inteface of top/toss/sides.

Example Code

You will find actual code for this example at ... For a quick comparison of the SixSidedDie class in the various languages, see ...

Class Descriptions

I used a Java-like notation in the class descriptions below to describe the methods in each class. I am assuming that Java would be the most likely OO language for most people to be familiar with.

Die Interface

Any Die object must repond to the following messages.

Not all languages require an explicit interface (e.g. Python and SmallTalk). Some languages treat interfaces differently than classes (e.g. Java). Others treat interfaces as normal classes, except that all methods are abstract (e.g. C++ and Eiffel).

int top ()
Return the current top face of the die object. Valid face values are between 1 and sides() inclusive.

int sides ()
Return the number of sides for this die object. Must be greater than 1.

void toss ()
Toss the die, causing a (potentially) new face value to appear on the top of the die. New face values should be randomly chosen (although a particular random distribution is not required).
(see also: Eiffel short form)

SixSidedDie Class

A SixSidedDie class conforms to the interface defined by Die.
int top ()
Return the current top face of the die object. Valid face values are between 1 and sides() inclusive.

int sides ()
Return the number of sides for this die object. In our case, we always return 6.

void toss ()
Toss the die, causing a (potentially) new face value to appear on the top of the die. New face values will be uniformly distributed between 1 and the number of sides (inclusive).
(see also: Eiffel short form)

DieCounter Class

A DieCounter is used to evaluate the randomness of an object conforming to the Die interface. Because of time limitations, I did not have time to go through each language example and make the definition of DieCounter consistent, so there will be minor variations in the functionality of DieCounter in each example.
Die CountedDie ()
Return the Die object under test.

int maxFace ()
Return the maximum number of faces used by the Die object under test.

int countFor (int faceValue)
Return the number of times faceValue appeared in the test.

int[] counts ()
Return an array describing the number of counts for each face value.

int throws ()
Return the number of times the die under test has been thrown in the current sequence of tests.

void setDie (Die d)
Use die d in running the face counting tests. A reset is automatically performed when changing dice.

void reset ()
Reset all count statistics in the current DieCounter. Start a new sequence of tests.

void run (int times)
Toss the die times times and record the result of each throw. throws() will be incremented by times.
(see also: Eiffel short form)

UML Model


[ OO Demo Main Page ]
[ CLUG Home Page ]
Jim Weirich / jweirich@one.net