Question
To make it easy to sort all of the entries in the PhoneBook class, we will make the following improvement to the PhoneBookEntry class and
To make it easy to sort all of the entries in the PhoneBook class, we will make the following improvement to the PhoneBookEntry class and PhoneBook class.
Make PhoneBookEntry class implement the Comparable interface, and override the compareTo() method.
In PhoneBook class, call Collection's sort() method to sort the phone book.
/*FIXME(1) Make PhoneBookEntry class implement Comparable
PhoneBook.java
import java.util.ArrayList; import java.util.Collections;
public class PhoneBook { private ArrayList
} public void addEntry(String firstName, String lastName, String number) { list.add(new PhoneBookEntry(firstName, lastName, number)); } public void printBook() { for(int i = 0; i < list.size(); i++) { System.out.println(list.get(i).toString()); } } public static void main(String[] args) { PhoneBook pb = new PhoneBook(); pb.initialize(); /*FIXME(1) Call the sort method in the Collections class to sort the phone book*/ pb.printBook(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started