Advertisement
jdalbey

Counter.java Identify DBC problem

Feb 27th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. /**
  2.  *  Counter is a non-negative Integer with restricted operations for counting.
  3.  *
  4.  * @author J. Dalbey
  5.  * @version 2014.2.27
  6.  */
  7. public class Counter
  8. {
  9.     // counter value
  10.     private int count;
  11.     // ending value (optional)
  12.     private int target;
  13.    
  14.     /**
  15.      * Default Constructor sets counter to zero.
  16.      */
  17.     public Counter()
  18.     {
  19.         count = 0;
  20.     }
  21.    
  22.     /**
  23.      * Constructor with a target end value.
  24.      * @param end is the ending value
  25.      * @pre end > 0
  26.      */
  27.     public Counter(int end)
  28.     {
  29.         this();
  30.         target = end;
  31.     }
  32.  
  33.     /**
  34.      * Increment by one.
  35.      */
  36.     public void inc()
  37.     {
  38.         count = count + 1;
  39.     }
  40.  
  41.     /**
  42.      * Check if counter has not reached the target.
  43.      * @return true if count not equals target, false otherwise
  44.      * @throws InvalidStateException if count > target
  45.      */
  46.     public boolean notDone() throws InvalidStateException
  47.     {
  48.         if (count > target)
  49.         {
  50.             throw new InvalidStateException();
  51.         }
  52.         return count != target;
  53.     }
  54.    
  55.     class InvalidStateException extends Exception {}
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement