public class Counter extends java.lang.Object implements java.lang.Comparable<Counter>
For additional documentation, see Section 1.2 of Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
Counter(java.lang.String id)
Initializes a new counter starting at 0, with the given id.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Counter that)
Compares the value of this counter to the value of that counter.
|
void |
increment()
Increments the counter by 1.
|
static void |
main(java.lang.String[] args)
Reads two command-line integers N and T; creates N counters;
increments T counters at random; and prints results.
|
int |
tally()
Returns the current value of the counter
(i.e., the number of increments since creation).
|
java.lang.String |
toString()
Returns a string representation of this counter.
|
public Counter(java.lang.String id)
id
- the name of the counter.public void increment()
public int tally()
public java.lang.String toString()
toString
in class java.lang.Object
public int compareTo(Counter that)
compareTo
in interface java.lang.Comparable<Counter>
public static void main(java.lang.String[] args)