/** * The class Counter represents simple counters that keep track of the * number of "hits" (cf. a traditional visitor counter on a web page). * A counter's value starts at zero and is increases by one every time its advance() * method is called. * 0 * 1 * 2 * 3 * ... * Each counter has a maximum value before which its value "wraps around to zero". * For instance, if the maximum value of a counter is 100, the counter's value * never goes past 99: * ... * 97 * 98 * 99 * 0 * 1 * 2 * ... */ public class Counter { private int hits; private int maximum; /** * Creates a new counter with the given maximum value. The counter is * initially set to zero. * * @param maximum a positive number that this counter's value must never reach */ public Counter(int maximum) { this.hits = 0; this.maximum = maximum; } /** * Advances the counter by one step. */ public void advance() { this.hits = this.hits + 1; } /** * Returns the counter's current value (between zero and maximum-1). * * @return the counter's current value */ public int getValue() { return this.hits % this.maximum; } /** * Determines if the current value of this counter is higher than * that of another counter. This comparison is based on the * values of the two counters (between zero and maximum-1) as * determined by getValue(). * * @param another another counter that this one is compared to * @return a boolean value indicating if this counter's value is * higher than that of the given counter */ public boolean isHigherThan(Counter another) { return another.getValue() < this.getValue(); } }