Question

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

public class PhoneBookEntryTest {
   public static void main(String args[]){
       ArrayList<PhoneBookEntry> phonebook = new ArrayList<PhoneBookEntry>();
       Scanner sc = new Scanner(System.in);
       System.out.println("\nEnter phone book details"+
       "\n e.g. John 1234");
       for(int i=0;i<10;i++){
          
           String str = sc.nextLine();
           PhoneBookEntry records = new PhoneBookEntry(str.split(" ")[0],Integer.parseInt(str.split(" ")[1]));
           phonebook.add(records );
       }
      
       System.out.println("Records are: ");
       for(int i=0;i<phonebook.size();i++){
           PhoneBookEntry book = phonebook.get(i);
           System.out.println("Name: "+book.getName()+" Phone No: "+book.getPhoneno());
       }
   }
}

workspace- Java-HomeworkLib pracrise/src/Phone B File Edit Source Refactor Navigate Search Project Run Window Help Quick Access | |

Add a comment
Know the answer?
Add Answer to:
Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person’s name and...
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
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