This week we\'ll practice using ArrayList and the Comparable Interface. We will
ID: 3594260 • Letter: T
Question
This week we'll practice using ArrayList and the Comparable Interface. We will write a class to represent a Phone Book Entry with first name, last name, and phone number. Then we will write a class to represent the Phone Book Catalog that holds all of the phone book entries.
Sample Output 1
PhoneBookEntry class
PART I:
Write a class named PhoneBookEntry that keeps up with the first name, last name, and phone number of a person. This class will implement the Comparable Interface for Type PhoneBookEntry.
Declare three instance variables (fields) for the PhoneBookEntry's first name, last name, and phone number as type String
Write a 3-parameter constructor that accepts first name, last name, and phone number as parameters and initializes them.
Write accessor (getter) and mutator (setter) instance methods for each of the instance variables (fields). There will be 6 total methods.
Write instance method:
public int compareTo(PhoneBookEntry other) - that compares the first names, last names, and phone numbers and returns the correct value (0 if equal, positive if 'this' larger than 'other', negative if 'this' smaller than 'other')
PART II:
Write a class named PhoneBookCatalog that will store all of our PhoneBookEntry objects in an ArrayList.
Declare a constant NUM_ENTRIES and set it equal to 5. This will be how many entries will be accepted for each run of the program.
Declare one instance variable (field) for the phone book entries as type ArrayList
Write a 0-parameter constructor that initializes the ArrayList instance variable as a new ArrayList
Write instance methods:
public void greetUser() – that explains to the user what will happen (see Sample Output)
private PhoneBookEntry getEntry() – that is a private "helper" method to get names and phone numbers from the console.
Declare a Scanner inside the method
Declare 3 local variables for first name, last name, and phone number
Prompt the user for the 3 values (see Sample Output) and store them in the local variables
Create a new PhoneBookEntry object and pass its constructor the 3 values you read from the console.
Return this object from the method
public void storeEntry() - that uses a traditional for loop to add entries to the ArrayList. (You can do this by using the .add method and passing a call to the getEntry() method as a parameter)
public void displayEntry() - that sorts the Collection of Phone Book entries in the ArrayList and then outputs it (see Sample Output). Note: A For-Each loop is useful here
PART III:
Download the Driver class code PhoneBookDriver that will test both classes.
// This is the PhonebookDriver
/**
* Chapter 10
* This program demonstrates the PhoneBookDemo class.
*/
public class PhoneBookDriver
{
public static void main(String args[])
{
PhoneBookCatalog demo = new PhoneBookCatalog();
demo.greetUser();
demo.storeEntry();
demo.displayEntry();
}
}
Explanation / Answer
PhoneBookDriver.java
import java.util.*;
public class PhoneBookDriver
{
public static void main(String[] args)
{
PhoneBookCatalog demo = new PhoneBookCatalog();
demo.greetUser();
demo.storeEntry();
demo.displayEntry();
}
}
class PhoneBookCatalog{
private static final int NUM_ENTRIES=5;
ArrayList<PhoneBookEntry> entries;
PhoneBookCatalog(){
entries=new ArrayList<PhoneBookEntry>();
}
public void greetUser(){
System.out.println("I'm going to ask you to enter 5 names and phone numbers. ");
}
private PhoneBookEntry getEntry(){
String firstName,lastName,phoneNo;
Scanner console=new Scanner(System.in);
System.out.print(" Enter a person's first name:");
firstName=console.nextLine();
System.out.print("Enter a person's last name:");
lastName=console.nextLine();
System.out.print("Enter that person's phone number:");
phoneNo=console.nextLine();
PhoneBookEntry entry=new PhoneBookEntry(firstName,lastName,phoneNo);
return entry;
}
public void storeEntry(){
for(int i=0;i<NUM_ENTRIES;i++){
entries.add(getEntry());
}
}
public void displayEntry(){
Collections.sort(entries);
System.out.println(" Here's the data you entered: ");
for(PhoneBookEntry entry:entries){
System.out.println(" -----------------------------------------------");
System.out.println("Name: "+entry.getFirstName()+" "+entry.getLastName());
System.out.println("Phone number: "+entry.getPhoneNo());
System.out.println("-----------------------------------------------");
}
}
}
class PhoneBookEntry implements Comparable<PhoneBookEntry>{
private String firstName;
private String lastName;
private String phoneNo;
PhoneBookEntry(String firstName,String lastName,String phoneNo){
this.firstName=firstName;
this.lastName=lastName;
this.phoneNo=phoneNo;
}
//getters
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getPhoneNo(){
return phoneNo;
}
//setters
public void setFirstName(String firstName){
this.firstName=firstName;
}
public void setLastName(String lastName){
this.lastName=lastName;
}
public void setPhoneNo(String phoneNo){
this.phoneNo=phoneNo;
}
public int compareTo(PhoneBookEntry other){
int first=this.firstName.compareToIgnoreCase(other.getFirstName());
int second=this.lastName.compareToIgnoreCase(other.getLastName());
int third=this.phoneNo.compareToIgnoreCase(other.getPhoneNo());
if(first!=0){
return first;
}else if(second!=0){
return second;
}else {
return third;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.