Package dsa
Class Counter
- java.lang.Object
-
- dsa.Counter
-
- All Implemented Interfaces:
Comparable<Counter>
public class Counter extends Object implements Comparable<Counter>
A data type to represent a counter.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(Counter other)
Returns a comparison of this counter withother
, by their tally.boolean
equals(Object other)
Returnstrue
if this counter andother
have the same tally, andfalse
otherwise.void
increment()
Increments this counter by 1.static void
main(String[] args)
Unit tests the data type.void
reset()
Resets this counter to zero.int
tally()
Returns the current value of this counter.String
toString()
Returns a string representation of this counter.
-
-
-
Constructor Detail
-
Counter
public Counter(String id)
Constructs a counter given its id.- Parameters:
id
- id of the counter.
-
-
Method Detail
-
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
public boolean equals(Object other)
Returnstrue
if this counter andother
have the same tally, andfalse
otherwise.
-
toString
public String toString()
Returns a string representation of this counter.
-
compareTo
public int compareTo(Counter other)
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
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-