Completed Search

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

The complete search() follows:

Completed Search

Here is the completed search() method.

  PhoneEntry search( String targetName )  
  {
    for ( int j=0; j < phoneBook.length; j++ )
    {
      if ( phoneBook[ j ].name.equals( targetName ) )
        return phoneBook[ j ];
    }

    return null;
  }

You may be uncomfortable with this expression:

  phoneBook[ j ].name.equals( targetName )

Look at it piece by piece:

  • phoneBook[ j ] contains a reference to a PhoneEntry object.
  • The PhoneEntry object contains an instance variable, name.
  • name is a reference to a String object.
  • A String object has an equals() method.
  • The equals() method is used with the String referred to by targetName .
  • The entire expression evaluates to true or false.

This is complicated. If you are still uncomfortable don't worry too much. Read through the code and the explanations a few times and then move one.

QUESTION 25:

Does the expression

  targetName.equals( phoneBook[ j ].name )

do the same thing as the above expression?