Chapter 6. Wheel Class

Table of Contents

Overview
Design
Fields
Constructors
Methods
Deliverables

This chapter builds on the previous two chapters, creating a more complete composite object from the Outcome and Bin classes we have already defined. We'll introduce some basic subclass constructor techniques, also.

Overview

The wheel has two responsibilities: it is a container for the Bins and it picks one of the Bins at random. We'll also have to look at how to initialize the various Bins.

Container Implementation. Since the Wheel is 38 Bins, it is a collection. We can review our survey of the Java collections in Java Collections and the Python collections in Python Collections for some guidance here. In this case, the choice of Bin will be selected by a random numeric index. For Java programmers, this makes the java.util.Vector very appealing. Python programmers will find that a List will do very nicely.

One consequence of using a Vector is that we have to choose an index for the various Bins. While each Bin contains a variety of individual Outcomes, the single number Outcome is unique to each Bin. The numbers 1 to 36 become the index to their respective Bins. We have a small problem, however, with 0 and 00: we need two separate indexes. While 0 is a valid index into a Vector, what do we do with 00?

Since the index of the Bin doesn't have any significance at all, we can assign the Bin that has the 00 Outcome to position 37 in the Vector. This gives us a unique place for all 38 Bins.

Random Selection. In order for the Wheel to select a Bin at random, we'll need a random number from 0 to 37 that we can use an an index. The random number generator in java.util.Random does everything we need. We can use the generator's public int nextInt(int n); method to return integers.

Python programmers can use the random module. This module offers a choice function which picks a random value from a sequence. This is ideal for returning a randomly selected Bin from our list of Bins. For some cautions on casual use of modules in Python, see Using Python Modules.

Initialization. Each instance of Bin has a list of Outcomes. The 0 and 00 Bins only have two Outcomes. The other numbers have anywhere from twelve to fourteen Outcomes. Constructing the entire collection of Bins would be a tedious undertaking. We'll apply a common OO design technique of deferred binding and work on that algorithm later, after we have the basic design for the Wheel finished.