package counters;
/**
*
* Implements a simple counter value whose current value is an
* an integer and a positive integer limit that is specified
* when the counter is created.
*
*
*
* Class Invariant: *
*limit
is a positive integer that can be
* represented exactly using Java’s int
* data type
* value
is an integer whose value is between
* 0
and limit−1
(inclusive)
* limit
.
* inLimit
is a postive integer. limit
of this new object is
* equal to the given value inLimit
* and the value
for this new object
* is equal to zero. inLimit
is an integer that is less than or equal to zero. IllegalArgumentException
is thrown. limit
of the object to be created
* @throws IllegalArgumentException if the input is less than
* or equal to zero
*/
public SimpleCounter(int inLimit) {
if (inLimit < 1) {
throw new IllegalArgumentException("Negative or zero counter limit: "
+ inLimit);
} else {
limit = inLimit;
value = 0;
};
};
// Accessor Methods
/**
*
* Accessor method used to display the value of the
* counter’s limit
.
* limit
limit
of this counter
*
*/
public int getLimit () {
return this.limit;
};
/**
*
* Accessor method used to display the counter’s
* current value
.
* value
value
of this Counter
*
*/
public int getValue () {
return this.value;
};
// Mutator Method
/**
*
* Mutator method to increment the counter’s
* value
.
* value < limit−1
limit
is not changed
* (so that it is still a postive integer that can be
* represented exactly using Java’s int
* data type). value
is v, then
* the final value is equal to v+1
* (which is an integer between 0
* and limit−1
, as required) value = limit−1
limit
is not changed
* (so that it is still a positive integer that can be
* represented exactly using Java’s int
* data type). limit
= 0. LimitReachedException
is thrown. value
is at
* its maximal value when this method is applied
*
*/
public void advanceValue () throws LimitReachedException {
this.value++;
if (this.value == this.limit) {
this.value = 0;
throw new LimitReachedException("Counter value reset.");
};
};
}