Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Counter is a non-negative Integer with restricted operations for counting.
- *
- * @author J. Dalbey
- * @version 2014.2.27
- */
- public class Counter
- {
- // counter value
- private int count;
- // ending value (optional)
- private int target;
- /**
- * Default Constructor sets counter to zero.
- */
- public Counter()
- {
- count = 0;
- }
- /**
- * Constructor with a target end value.
- * @param end is the ending value
- * @pre end > 0
- */
- public Counter(int end)
- {
- this();
- target = end;
- }
- /**
- * Increment by one.
- */
- public void inc()
- {
- count = count + 1;
- }
- /**
- * Check if counter has not reached the target.
- * @return true if count not equals target, false otherwise
- * @throws InvalidStateException if count > target
- */
- public boolean notDone() throws InvalidStateException
- {
- if (count > target)
- {
- throw new InvalidStateException();
- }
- return count != target;
- }
- class InvalidStateException extends Exception {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement