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 .

Dani AI

Generated

Short answer for : create and populate each Contact (first/last name, phone, email) before adding it to the ArrayList. When listing, print the Contact fields inside the for-each loop. Creating or mutating fields "after the loop" will not magically attach them to the already‑stored objects — the list holds references to whatever object was added at the time of add().

As pointed out, use private fields with getters/setters. A compact pattern that keeps printing simple is to override toString() on Contact and always add a fresh Contact instance to the list:

class Contact {
  private String firstName, lastName, phone, email;

  public Contact(String f, String l, String p, String e) {
    firstName = f; lastName = l; phone = p; email = e;
  }

  public String getFirstName() { return firstName; }
  public String getLastName()  { return lastName; }
  public String getPhone()     { return phone; }
  public String getEmail()     { return email; }

  @Override
  public String toString() {
    return firstName + " " + lastName + " | " + phone + " | " + email;
  }
}

Example usage patterns (add, display, search, delete):

List<Contact> db = new ArrayList<>();
db.add(new Contact("Bill","Elmore","555-1212","bill@ex.com"));

for (Contact c : db) System.out.println(c); // display inside loop

// case-insensitive search:
String term = "elmore".toLowerCase();
for (Contact c : db) {
  if (c.getFirstName().toLowerCase().contains(term)
      || c.getLastName().toLowerCase().contains(term)
      || c.getPhone().contains(term)
      || c.getEmail().toLowerCase().contains(term)) {
    System.out.println("Found: " + c);
  }
}

// safe removal while iterating:
Iterator<Contact> it = db.iterator();
while (it.hasNext()) {
  if (it.next().getLastName().equalsIgnoreCase("Elmore")) it.remove();
}

Quick tips: always create a new Contact per entry (don’t reuse one instance), use toLowerCase() for case-insensitive matching, and use an Iterator (not a for-each) when removing items during iteration to avoid ConcurrentModificationException.

Recommended Answers

All 2 Replies

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.