/** * A simple counter, typically used to count the number of steps * used in an algorithm. * * @author Samuel A. Rebelsky * @version 1.0 of September 1998 */ public class Counter { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The value we've counted. */ protected int count; // +---------+------------------------------------------------- // | Methods | // +---------+ /** * Reset the counter to 0. */ public void reset() { this.count = 0; } // reset() /** * Increment the counter. Print an error message and reset to * 0 if the counter gets too large. (This should really throw * an exception if the counter gets too large, but not everyone * who reads this will know about exceptions. It should also * use a better mechanism for printing errors, but that was * deemed inappropriate.) */ public void increment() { if (this.count == Integer.MAX_VALUE) { System.err.println("*** Error: exceeding maximum counter value ***"); reset(); } else { ++this.count; } } // increment() /** * Get the current value of the counter. */ public int value() { return this.count; } // value() } // class Counter