ArrayLists and Iterators

23. Entry Methods


Answer:

  • String getName()
  • String getNumber()
  • boolean equals(Object other)
  • String toString()

Entry Methods

You may have thought of some additional methods that would be useful in a more realistic example. But for now, use the above methods.

class Entry
{
  private String name;
  private String number;

  // constructor
  public Entry( String n, String num )
  {
    name = n; number = num;
  }

  // methods
  public String getName()
  {
    return name ;
  }

  public String getNumber()
  {
    return number ;
  }

  // Compare the name of this Entry with that of some other object
  public boolean equals( Object other )
  {
    String otherName = ((Entry)other).getName();  // Get name in the other object.
    return name.equals( otherName );              // Compare name in this object 
                                                  // with that in the other object.
  }

  public String toString()
  {
    return "Name: " + getName() + "; Number: " + getNumber() ;
  }
 
}

The toString() method of Entry overrides the toString() method that all objects have.

Also, the equals(Object) method of Entry overrides the equals(Object) method that all objects have.

Question 23:

Why is the equals(Object) method included? Is it really necessary?