Package dsa
Class Counter
java.lang.Object
dsa.Counter
- All Implemented Interfaces:
Comparable<Counter>
A data type to represent a counter.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Returns a comparison of this counter withother
, by their tally.boolean
Returnstrue
if this counter andother
have the same tally, andfalse
otherwise.void
Increments this counter by 1.static void
Unit tests the data type.void
reset()
Resets this counter to zero.int
tally()
Returns the current value of this counter.toString()
Returns a string representation of this counter.
-
Constructor Details
-
Counter
Constructs a counter given its id.- Parameters:
id
- id of the counter.
-
-
Method Details
-
increment
public void increment()Increments this counter by 1. -
tally
public int tally()Returns the current value of this counter.- Returns:
- the current value of this counter.
-
reset
public void reset()Resets this counter to zero. -
equals
Returnstrue
if this counter andother
have the same tally, andfalse
otherwise. -
toString
Returns a string representation of this counter. -
compareTo
Returns a comparison of this counter withother
, by their tally.- Specified by:
compareTo
in interfaceComparable<Counter>
- Parameters:
other
- the other counter.- Returns:
- a comparison of this counter with
other
, by their tally.
-
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-