Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a class named PhoneBookEntry that has fields for a persons name and phone number. The class should have a constructor and appropriate accessor and

Write a class named PhoneBookEntry that has fields for a persons name and phone number. 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

I need help filling in the blanks with ......... and why?

public class PhoneBookEntry { private String name; // Person's name private String phoneNumber; // Person's phone number /** * The constructor initializes the person's name * and phone number. */ public PhoneBookEntry(String n, String pn) { name = n; phoneNumber = pn; } /** * The setName method sets the person's name. */ public void setName(String n) { ............... } /** * setPhoneNumber method sets the person's * phone number. */ public void setPhoneNumber(String pn) { ............... } /** * The getName method returns the person's * name. */ public String getName() { ............... } /** * The getPhoneNumber method returns the * person's phone number. */ public String getPhoneNumber() { ................ } } 

public class PhoneBookDemo { public static void main(String args[]) { // Constant for the numer of entries. final int NUM_ENTRIES = 5; // Create an ArrayList to hold PhoneBookEntry objects. ArrayList list = new ArrayList(); // Tell the user what's about to happen. System.out.println("I'm going to ask you to enter " + NUM_ENTRIES + " names and phone numbers."); System.out.println(); // Store PhoneBookEntry objects in the ArrayList. for (int i = 0; i < NUM_ENTRIES; i++) { ............ } System.out.println("Here's the data you entered:"); // Display the data stored in the ArrayList. for (int i = 0; i < list.size(); i++) { displayEntry(................); } } /** * The getEntry method creates a PhoneBookEntry object * populated with data entered by the user and returns * a reference to the object. */ public static PhoneBookEntry getEntry() { // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Variables to hold a person's name and // phone number. String name; String phoneNumber; // Get the data. System.out.print("Enter a person's name: "); name = keyboard.nextLine(); System.out.print("Enter that person's phone number: "); phoneNumber = keyboard.nextLine(); // Create a PhoneBookEntry object. PhoneBookEntry entry = new PhoneBookEntry(name, phoneNumber); // Return a reference to the object. return entry; } /** * The displayEntry method displays the data stored * in a PhoneBookEntry object. */ public static void displayEntry(PhoneBookEntry entry) { System.out.println("------------------------------"); System.out.println("Name: " + entry.getName()); System.out.println("Phone number: " + entry.getPhoneNumber()); } } 

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

Students also viewed these Databases questions