Write a class named PhoneBookEntry that has fields for a persons name and phone
ID: 3551977 • Letter: W
Question
Write a class named PhoneBookEntry that has fields for a persons name and phone num-ber. The class should have a constructor and appropriate accessor and mutator methods. Then write a program that creates at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList.
Output
Enter 5 names and phone numbers.(Make them up) using the Scanner Class
Then have the program display the data entered
Here's the data you entered:
------------------------------
Name:
Phone number:
------------------------------
Name:
Phone number:
------------------------------
Name:
Phone number:
------------------------------
Name:
Phone number:
------------------------------
Name:
Phone number:
Explanation / Answer
import java.util.*;
public class PhoneBookEntry {
private String name;
private String number;
public PhoneBookEntry(String name, String number) {
this.name=name;
this.number=number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public static void main(String[] args) {
List<PhoneBookEntry> phoneList = new ArrayList<PhoneBookEntry>();
phoneList.add(new PhoneBookEntry("Sue","347-2984"));
phoneList.add(new PhoneBookEntry("Rod","384-2898"));
phoneList.add(new PhoneBookEntry("Ike","382-9082"));
phoneList.add(new PhoneBookEntry("Ann","389-9083"));
phoneList.add(new PhoneBookEntry("Sid","982-8984"));
for (PhoneBookEntry entry : phoneList) {
System.out.println(entry.getName()+" "+entry.getNumber());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.