previous | start | next

Example Class Type II

The Counter class may not correspond to any object directly specified in a problem or application context, but this class may be used in writing and application in such a context.

public class Counter {
  /* private instance members */
  private String id;
  private int value;
        

  /* public instance constructor and methods */
  /**
   * Initialize this counter's id name = idName and value = 0.
   * @param idName
   */
  public Counter(String idName)
  {
    id = idName;
    value = 0;
  }
        
  /**
   * Increment this counter's value by 1
   */
  public void increment()
  {
    value++;
  }
        
  /**
   * Return the value of this counter.
   */
  public int val()
  {
    return value;
  }
        
  /**
   * (Inherited) method; new implementation
   * Returns a string representation of this instance.
   */
  public String toString()
  {
    return value + " " + id;
  }
}


previous | start | next