Java Reference
In-Depth Information
Class NumberDisplay
We shall now analyze a complete implementation of this task. The project clock-display in the
examples attached to this topic contains the solution. First, we shall look at the implementation
of the class NumberDisplay . Code 3.3 shows the complete source code. Overall, this class is
fairly straightforward. It has the two fields discussed above (Section 3.5), one constructor, and
four methods ( getValue , setValue , getDisplayValue , and increment ).
The constructor receives the roll-over limit as a parameter. If, for example, 24 is passed in as
the roll-over limit, the display will roll over to 0 at that value. Thus, the range for the display
value would be 0 to 23. This feature allows us to use this class for both hour and minute dis-
plays. For the hour display, we will create a NumberDisplay with limit 24; for the minute
display, we will create one with limit 60.
The constructor then stores the roll-over limit in a field and sets the current value of the
display to 0.
Code 3.3
Implementation of the
* The NumberDisplay class represents a digital number
* display that can hold values from zero to a given limit.
* The limit can be specified when creating the display. The
* values range from zero (inclusive) to limit-1. If used, for
* example, for the seconds on a digital clock, the limit
* would be 60, resulting in display values from 0 to 59.
* When incremented, the display automatically rolls over to
* zero when reaching the limit.
* @author Michael K├Âlling and David J. Barnes
* @version 2011.07.31
public class NumberDisplay
private int limit;
private int value;
* Constructor for objects of class NumberDisplay
public NumberDisplay( int rollOverLimit)
limit = rollOverLimit;
value = 0;
* Return the current value.
Search WWH ::

Custom Search