Question: /* APCSA Ch85pg28: Phone Book Array List 20230206 Modify the program so that it searches for a name entered by the user. Test, change, play

/* APCSA Ch85pg28: Phone Book Array List 20230206 Modify the program so that it searches for a name entered by the user. Test, change, play with code. Know all the concepts, be able to clearly explain each part and each line! */ import java.util.* ; public class Ch85pg28_PhoneBook_STU2a { public static void main ( String[] args) { ArrayList phone = new ArrayList(); phone.add( new Entry( "Amy", "123-4567") ); phone.add( new Entry( "Bob", "123-6780") ); phone.add( new Entry( "Hal", "789-1234") ); phone.add( new Entry( "Deb", "789-4457") ); phone.add( new Entry( "Zoe", "446-0210") ); // Look for Hal in phone. The indexOf() method uses the // equals(Object) method of each object in the list. // System.out.println("Begin Search" ); Entry target = new Entry( "Hal", null ); int spot = phone.indexOf( target ) ; System.out.println("End Search" ); System.out.println( "indexOf returns: " + spot ) ; } } 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 ; } public boolean equals( Object other ) { System.out.print (" Compare " + other + " To " + this ); System.out.println(" Result: " + name.equals( ((Entry)other).name ) ); return getName().equals( ((Entry)other).getName() ); } public String toString() { return "Name: " + getName() + "; Number: " + getNumber() ; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!