I'm new to java and seriously needing some help..I'm having problems deciding accessor and mutator methods.. and how to create an ArrayList.


Write a program that uses an ArrayList of parameter type Contact to store a database of contacts. The Contact class should store the contact's first and last name, phone number, and email address. Add appropriate accessor and mutator methods. Your database program should present a menu that allows the user add a contact, display all contacts, search for a specific contact and display it, or search for a specific contact and give the user the option to delete it. The searches should find any contact where an instance variable contains a target search string. For example, if "elmore" is the search target then any contact where the first name, last name, phone number, or email address contains "elmore" should be returned for display or deletion. Use a "for each" loop to iterate through the ArrayList .

Example:

class Contact {
  private String name = null;

  public Contact() {
  }

  public Contact(String name) {
    setName(name);
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

}

The name is private so no one can access it outside the class.
The getName is the accessor to get the name
The setName is the mutator to change the value of the name.

As you can see, accessor, mutator are just fancy words. Just add in the class whatever your assignments says that a Contact should have, make them private and add get/set methods.

Then provided that you have java 1.5 and later you can create the ArrayList and put as many as you like:

ArrayList<Contact> list = new ArrayList<Contact>();

Contact cnt1 = new Contact();
cnt1.setName("my name");

Contact cnt2 = new Contact("my other name");

list.add(cnt1);
list.add(cnt2);

for (int i=0;i<list.size();i++) {
  Contact cont = list.get(i);

  System.out.println("Name: "+cont.getName());
}

Thank you so much .. One more question. When listing the arrays should I add the email and address after the for loop or should i create it before ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.