/* Counter.java ** Java class that models the concept of a counter that can be ** incremented, decremented, and set to a specified value. ** ** Author: R. McCloskey */ public class Counter { // instance variable // ----------------- private int cntrVal; // current count value // constructors // ------------ /* Initializes this Counter to have the specified count value. */ public Counter(int initCntrVal) { cntrVal = initCntrVal; } /* Initializes this Counter to have zero as its count value. */ public Counter() { this(0); } // observer // -------- /* Returns this Counter's current count value. */ public int countVal() { return cntrVal; } // mutators // -------- /* Increments (i.e., increases by one) this Counter's count value. */ public void increment() { cntrVal = cntrVal + 1; } /* Decrements (i.e., decreases by one) this Counter's count value. */ public void decrement() { cntrVal = cntrVal - 1; } /* Sets this Counter's count value to that specified. */ public void setTo(int newVal) { cntrVal = newVal; } }