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.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a counter given its id.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns a comparison of this counter with other, by their tally.
    boolean
    equals(Object other)
    Returns true if this counter and other have the same tally, and false otherwise.
    void
    Increments this counter by 1.
    static void
    main(String[] args)
    Unit tests the data type.
    void
    Resets this counter to zero.
    int
    Returns the current value of this counter.
    Returns a string representation of this counter.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Counter

      public Counter(String id)
      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

      public boolean equals(Object other)
      Returns true if this counter and other have the same tally, and false otherwise.
      Overrides:
      equals in class Object
      Parameters:
      other - the other counter.
      Returns:
      true if this counter and other have the same tally, and false otherwise.
    • toString

      public String toString()
      Returns a string representation of this counter.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this counter.
    • compareTo

      public int compareTo(Counter other)
      Returns a comparison of this counter with other, by their tally.
      Specified by:
      compareTo in interface Comparable<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.