go to previous page   go to home page   go to next page        

Answer:

Entry Methods

You may have thought of some methods like setName() and setNumber() that would be useful in a more realistic example. But let us use just the above methods.

class Entry
{
  private String name;
  private String number;

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

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

  public String getNumber()
  {
    return number ;
  }

  public boolean equals( Object other )
  {
    return getName().equals( ((Entry)other).getName() );
  }

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

The toString() method of Entry overrides the toString() method that all objects have. Our method returns a more useful string than the inherited method.

QUESTION 22:

What does the following code write?

Entry ent = new Entry( "Amy", "123-4567");

System.out.println( ent );