Java Programming: Create a class called Contact that has a firstName, lastName,
ID: 3690669 • Letter: J
Question
Java Programming:
Create a class called Contact that has a firstName, lastName, phone and email. Also create a PhoneBook class that will have an arrayList of contact The PhoneBook class will have the following methods: addContact that takes a Contact object and adds is to the arrayList deleteContact that takes a Contact object and deletes it from the arrayList displayContacts that displays all contacts in the arrayList getSize that returns the number of contacts in the arrayList main method to run and test your program.Explanation / Answer
import java.util.*;
class Contact
{
String firstName;
String lastName;
String email;
String phoneNumber;
Contact(String fname,String lname,String phone,String mail)
{
firstName=fname;
lastName=lname;
phoneNumber=phone;
email=mail;
}
}
class PhoneBook
{
ArrayList<Contact> contactList=new ArrayList<Contact>();
void addContact(Contact c)
{
contactList.add(c);
}
void deleteContact(Contact c)
{
contactList.remove(c);
}
int getSize()
{
return contactList.size();
}
void display()
{
System.out.print("FirstName LastName Phone Email");
for(Contact c:contactList)
{
System.out.print(" "+c.firstName+" ");
System.out.print(c.lastName+" ");
System.out.print(c.phoneNumber+" ");
System.out.print(c.email+" ");
}
}
}
public class HelloWorld{
public static void main(String []args){
Contact c1=new Contact("a1","b1","a@b1","131");
Contact c2=new Contact("a2","b2","a@b2","132");
Contact c3=new Contact("a3","b3","a@b3","133");
Contact c4=new Contact("a4","b4","a@b4","134");
PhoneBook pb=new PhoneBook();
pb.addContact(c1);
pb.addContact(c2);
pb.addContact(c3);
pb.addContact(c4);
pb.display();
System.out.println(" No of contacts: "+pb.getSize());
pb.deleteContact(c1);
pb.display();
System.out.println(" No of contacts: "+pb.getSize());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.