1 / 11

Phone Book

Phone Book. search for a name and will then print out that person's phone number. The list of names and phone numbers is implemented as a list of PhoneEntry objects. PhoneEntry Class. public class PhoneEntry { String name; // name of a person String phone; // their phone number

ava-sellers
Download Presentation

Phone Book

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Phone Book • search for a name and will then print out that person's phone number. • The list of names and phone numbers is implemented as a list of PhoneEntry objects

  2. PhoneEntry Class public class PhoneEntry { String name; // name of a person String phone; // their phone number // creates a new phoneBookEntry with the specified information public PhoneEntry( String n, String p ) { name = n; phone = p; } //returns a description of this phoneEntry public String toString() { String description = ""; description += name + "\t" + phone +"\n"; return description; }

  3. PhoneBook Class public class PhoneBook { PhoneEntry[] phoneBook; int currentSize; int count; public PhoneBook() // constructor { currentSize=6; phoneBook = new PhoneEntry[ currentSize ] ; count=0; }

  4. PhoneBook Class continued public void addCustomer(String name, String phoneNumber) { System.out.println(count); phoneBook[count] = new PhoneEntry(name, phoneNumber); count++; }

  5. PhoneBook Class continued public PhoneEntry search( String targetName ) {// use a linear search for (int j=0; j<phoneBook.________; j++) { if ( phoneBook[ j ].name.equals( ____________)) return phoneBook[ j ]; } return null; }

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

  7. 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.

  8. phoneBook[ j ].name.equals( targetName ) Does the expression • targetName.equals( phoneBook[ j ].name ) do the same thing as the above expression? • Yes

  9. PhoneBook Class continued //returns a report describing the phoneBook collection public String toString() { String report = "+=========================\n"; report += "Number of Phone Entries " + count + "\n"; for (int p=0; p<count; p++) report += phoneBook[p].toString() + "\n"; return report; } }

  10. PhoneBook Class public class phoneBookTester { public static void main (String[] args) { PhoneBook pb = new PhoneBook(); pb.addCustomer("Paul Kratides", "(815)439-9271"); pb.addCustomer("James Barclay", "(418)665-1223"); pb.addCustomer("Grace Dunbar", "(860)399-3044"); pb.addCustomer("Violet Smith", "(312)223-1937"); pb.addCustomer("John Wood", "(913)883-2874"); pb.addCustomer("Paul Kratides", "(815)439-9271");

  11. PhoneBook Class // Display the list of Phone Entries System.out.println(pb); PhoneEntry entry = pb.search( "Violet Smith" ); // search for "Violet Smith" if ( entry != null ) System.out.println( entry.name + ": " + entry.phone ); else System.out.println("Name not found"); } }

More Related