Question

Write a class named PhoneBookEntry that has fields for a person's name and phone number.The class...

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. Thenwrite a program that creates at least five PhoneBookEntry objects and stores them in anArrayLitt.

Use

a loop to display the contents of each object in the

ArrayList.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Program:PhoneBookEntry .java

import java.util.ArrayList;
import java.util.Scanner;

class PhoneBookEntry
{
String PersonName;
String PhoneNumber;
public PhoneBookEntry(String Name, String Number)
{
PersonName=Name;
PhoneNumber=Number;
}
public void setPersonName(String Name)
{
PersonName=Name;
}
public void setPhoneNumber(String Name)
{
PersonName=Name;
}
public String getPersonName()
{
return PersonName;
}
public String getPhoneNumber()
{
return PhoneNumber;
}
public static void main(String[] args)
{
//creating 5 PhoneBookEntry objects to hold 5 entries
PhoneBookEntry Entry[]=new PhoneBookEntry[5];
//Creating a array list to hold PhoneBookEntry objects
ArrayList<PhoneBookEntry> L=new ArrayList <>();
//local variables
String Name, Number;
//Reading input from user or we can hard code
System.out.println("Creating 5 phone book entries");
for(int i=0;i<5;i++)
{
System.out.println("Enter Person "+(i+1)+" Name and phone number");
Scanner scan=new Scanner(System.in);// to get keyboard input
Name=scan.next(); // reading name
Number=scan.next();// reading phone number
//Creating Entry by calling constructor
Entry[i]=new PhoneBookEntry(Name, Number);
//Adding the entry to the arraylist
L.add(Entry[i]);
}
  
/* A loop to display the contents of each object in the ArrayList.*/
System.out.println("\nPhone Book Entries");
System.out.println("------------------------------------------");
System.out.println("Sno PersonName\tPhoneNumber");
System.out.println("------------------------------------------");
for(int i=0;i<5;i++)
{ /*Getting each entry obect from Arraylist and retrieving name and phone number */
System.out.println((i+1)+" "+L.get(i).getPersonName()+"\t"+L.get(i).getPhoneNumber());
}
}
}

//Output:

NetBeans IDE 8.0.2 File Edit View Navigate Source Refactor Run Debug Profile T <default config>v Output PhoneBookDemo (run)Ph

Add a comment
Know the answer?
Add Answer to:
Write a class named PhoneBookEntry that has fields for a person's name and phone number.The class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person’s name and...

    Phone Book ArrayList 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 Copyright | Addison-Wesley | Starting Out with Java | [email protected] | Printed from Chegg iOS App

  • Design a class named Person with fields for holding a person's name, address, and telephone number...

    Design a class named Person with fields for holding a person's name, address, and telephone number (all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes...

  • In C++, Step 1: Implement the Student Class and write a simple main() driver program to...

    In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...

  • Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type...

    Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...

  • Using JAVA* Design a class named Person with fields for holding a person’s name, address, and...

    Using JAVA* Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator...

  • Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double...

    Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double variable that holds the rooms length(in feet). Breadth: The double variable that holds the rooms breadth(in feet). Height: Double variable that holds rooms height in feet. Color: String object that holds the color you want the room to be painted with. The class should have a constructor to initialize the fields for a particular instance (or object) of the class as given: All double...

  • Create a data class named Automobile that implements the Comparable interface. Give the class data fields...

    Create a data class named Automobile that implements the Comparable interface. Give the class data fields for make, model, year, and price. Then add a constructor, all getters, a toString method that shows all attribute values, and implement Comparable by using the year as the criterion for comparing instances. Write a program named TestAutos that creates an ArrayList of five or six Automobiles. Use a for loop to display the elements in the ArrayList. Sort the Arraylist of autos by...

  • Using C++, Write a class named Employee that has the following member variables: name. A string...

    Using C++, Write a class named Employee that has the following member variables: name. A string that holds the employee's name. idNumber. An int variable that holds the employee's ID number. department. A string that holds the name of the department where the employee works. position. A string that holds the employee's job title. The class should have the following constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee's name,...

  • Code the following Program in C++ Define a class named Payment that contains a member variable...

    Code the following Program in C++ Define a class named Payment that contains a member variable of type float that stores the amount of the payment and appropriate accessor and mutator methods.    Also create a member function named paymentDetails that outputs an English sentence that describes the amount of the payment. Next define a class named CashPayment that is derived from Payment. This class should redefine the paymentDetails function to indicate that the payment is in cash. Include appropriate constructor(s)....

  • JAVA HELP Design a class named Employee. The class should keep the following information in fields:...

    JAVA HELP Design a class named Employee. The class should keep the following information in fields: Employee name Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. Hire date then, Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT