Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 interface*/ public class PhoneBookEntry { private String firstName; private String lastName; private String phoneNumber; public PhoneBookEntry(String firstName, String lastName, String number) { this.firstName = firstName; this.lastName = lastName; this.phoneNumber = number; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getPhoneNumber() { return phoneNumber; } public void setPhonenumber(String number) { phoneNumber = number; } @Override /*Complete the definition of the compareTo() method such that elements are sorted based on entry's last name, first name and phone number. Specifically, entries should first be sorted in ascending order according to last name first, and those entries with the last name should be sorted in ascending order according to the first name, then to the phone number. */ public int compareTo(PhoneBookEntry entry) { } @Override public String toString() { return lastName + ", " + firstName + " \tPhone Number: " + phoneNumber; } }

PhoneBook.java

import java.util.ArrayList; import java.util.Collections;

public class PhoneBook { private ArrayList list; public PhoneBook() { list = new ArrayList(); } public void initialize() { addEntry("Roxanne", "Hughes", "443-555-2864"); addEntry("Juan", "Alberto", "410-555-9385"); addEntry("Rachel", "Phillips", "310-555-6610"); addEntry("Adam", "Phillips", "310-578-3250"); addEntry("Roxanne", "Hughes", "410-333-5387");

} 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Relational Model A Retrospective Review And Analysis

Authors: C. J. Date

1st Edition

0201612941, 978-0201612943

More Books

Students also viewed these Databases questions