asked 114k views
0 votes
JAVA ECLIPSE

Write a class named PhoneBookEntry that has fields for a person's 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.

There this code:

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 phoneList = new ArrayList();

phoneList.add(new PhoneBookEntry("John Smith","818.555.1234"));

phoneList.add(new PhoneBookEntry("Jane Brown","818.555.1235"));

phoneList.add(new PhoneBookEntry("Jose Vargas","818.555.1236"));

phoneList.add(new PhoneBookEntry("Armen Avetian","818.555.1237"));

phoneList.add(new PhoneBookEntry("Xin Liu","818.555.1238"));

phoneList.add(new PhoneBookEntry("James White","818.555.1239"));

phoneList.add(new PhoneBookEntry("Julie McGuiness","818.555.1240"));

phoneList.add(new PhoneBookEntry("Juan Ballardos","818.555.1241"));

phoneList.add(new PhoneBookEntry("Arthur London","818.555.1242"));

phoneList.add(new PhoneBookEntry("Ashot Aghajanian","818.555.1243"));

for (PhoneBookEntry entry : phoneList) {

System.out.println(entry.getName()+" "+entry.getNumber());

}

}

}

_________________________________________________________

Gives me this output:

John Smith 818.555.1234

Jane Brown 818.555.1235

Jose Vargas 818.555.1236

Armen Avetian 818.555.1237

Xin Liu 818.555.1238

James White 818.555.1239

Julie McGuiness 818.555.1240

Juan Ballardos 818.555.1241

Arthur London 818.555.1242

Ashot Aghajanian 818.555.1243

______________________________________

I would like to get the output like this:

First Name: John\\

Last Name: Smith\\

Phone Number: 818.555.1234\\

\\

First Name: Jane\\

Last Name: Brown\\

Phone Number: 818.555.1235\\

\\

First Name: Jose\\

Last Name: Vargas\\

Phone Number: 818.555.1236\\

\\

First Name: Armen\\

Last Name: Avetian\\

Phone Number: 818.555.1237\\

\\

First Name: Xin\\

Last Name: Liu\\

Phone Number: 818.555.1238\\

\\

First Name: James\\

Last Name: White\\

Phone Number: 818.555.1239\\

\\

First Name: Julie\\

Last Name: McGuiness\\

Phone Number: 818.555.1240\\

\\

First Name: Juan\\

Last Name: Ballardos\\

Phone Number: 818.555.1241\\

\\

First Name: Arthur\\

Last Name: London\\

Phone Number: 818.555.1242\\

\\

First Name: Ashot\\

Last Name: Aghajanian\\

Phone Number: 818.555.1243\\

\\

1 Answer

6 votes

Answer:

The code provided creates a class called

PhoneBookEntry that represents a phone book entry for a person, including their name and phone number. The class has a constructor that takes in the name and number as parameters and initializes the corresponding fields.

To access the name and number of a PhoneBookEntry object, the class provides getter methods, getName() and getNumber(), which return the values of the name and number fields respectively. The class also provides setter methods, setName() and setNumber(), which allow you to change the values of the name and number fields.

In the main method, an ArrayList called

phoneList is created to store PhoneBookEntry objects. Then, several PhoneBookEntry objects are created using the constructor and added to the phoneList using the add() method.

Finally, a for-each loop is used to iterate over the phoneList and display the contents of each PhoneBookEntry object. The loop retrieves each object from the list and uses the getName() and getNumber() methods to retrieve the name and number, which are then printed using System.out.println().

To modify the output to display the first name and last name separately, you can split the name field using the split() method. The split() method splits a string into an array of substrings based on a specified delimiter. In this case, you can split the name using a space (" ") as the delimiter.

Here's an example of how you can modify the code to achieve the desired output:

***java

for (PhoneBookEntry entry: phoneList) { String nameParts =

entry.getName().split(" ");

String firstName = nameParts[0];

String lastName = nameParts[1];

System.out.println("First Name: * +

firstName);

System.out.println("Last Name: " + lastName);

System.out.println("Phone Number: "+ entry.getNumber());

System.out.println();

}

Step-by-step explanation:

In the modified code, the name field of each PhoneBookEntry object is split into first name and last name using the split() method. The first name is stored in the firstName variable, and the last name is stored in the lastName variable. Then, these values are printed along with the phone number. After each entry, an empty line is printed to provide separation between the entries.

answered
User Cate Daniel
by
7.9k points