In: Computer Science
Write a class named ContactEntry that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five ContactEntry objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. I repeat, NO-ARG constructors. This is imperative!!
Hello
To call a method without using or creatin object or without writing no arg constructo we can declare that method as static and then call it by using name of the method
In below code i have create a method called show detail which will display all the objects stored in arraylist
In below code i didnot create NO ARG Constructor
I am also sharing Screenshot of the code and result
/////
ContactEntry.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ContactEntry {
String PersonsName;
String Phone;
String Email;
public ContactEntry(String personsName, String phone,
String email) {
super();
PersonsName = personsName;
Phone = phone;
Email = email;
}
public String getPersonsName() {
return PersonsName;
}
public void setPersonsName(String personsName) {
PersonsName = personsName;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
static List<ContactEntry> contactentry = new
ArrayList <ContactEntry>();
public static void showdetail()
{
Iterator
itr=contactentry.iterator();
//traverse elements of ArrayList object
while(itr.hasNext()){
ContactEntry
st=(ContactEntry)itr.next();
System.out.println(st.PersonsName+" "+st.Phone+"
"+st.Email);
}
}
public static void main(String[] args) {
ContactEntry c1=new
ContactEntry("John","5413134","[email protected]");
ContactEntry c2=new
ContactEntry("Jack","482123","[email protected]");
ContactEntry c3=new
ContactEntry("Josh","78642","[email protected]");
ContactEntry c4=new
ContactEntry("Jolly","325478","[email protected]");
ContactEntry c5=new
ContactEntry("Jordan","325479","[email protected]");
contactentry.add(c1);
contactentry.add(c2);
contactentry.add(c3);
contactentry.add(c4);
contactentry.add(c5);
showdetail();
}
}
Thank YOU!!!