Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I would like some help in completing this exercise. Thank You Given two classed

ID: 3706483 • Letter: I

Question

I would like some help in completing this exercise. Thank You

Given two classed Name and Contact, write class ContactManager.

public class ContactManager {

// declare the contact ArrayList as an instance variable here.

private ArrayList <Contact> contacts;

/**

   * Initialize an empty ContactManager.

   */

public ContactManager() {

throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

}

/**

   * Initialize a ContactManager which contains all the Contacts in the given

   * array.

   */

public ContactManager(Contact[] contacts) {

throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

}

/**

   * Given 4 String parameters, add a contact to the contacts list. If first name

   * or last name is null or an empty string, then do not add a contact but return

   * false.

   *

   * Otherwise, create a new contact, and add the phone number to the new contact.

   * If the phone number is in a valid format, add the contact to the list and

   * return true. You may not rewrite the validity check method but can simply

   * reuse the Contact class code written for L5.

   *

   * We allow adding duplicate contacts. Note that there is no limit to how many

   * times this method can be called.

   *

   * @param fName

   * @param lName

   * @param mName

   * @param phoneNumber

   * @return a boolean value. See the description above

   */

public boolean addContact(String fName, String lName, String mName, String phoneNumber) {

throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

}

/**

   * Remove the Contact with Name n from your List. Make no changes if it doesn't

   * exist.

   *

   * @param n

   */

public void removeContact(Name n) {

throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

  

}

/**

   * Return whether the Contact List contains a Contact with Name n or not. Hint:

   * A 1-line solution exists.

   */

public boolean contains(Name n) {

throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

}

/**

   * Return the number of contacts (int) Hint: A 1-line solution exists.

   */

public int countContacts() {

throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

}

/**

   * Return a toString of the contacts. The toString() method should call the

   * Contact toString() method and add a new line after every Contact, EXCEPT the

   * last. Example Output with 2 contacts:

   *

   * Name: __________ (Phone Number: ___-___-____) Name: __________ (Phone Number:

   * ___-___-____)

   */

public String toString() {

throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

  

}

}

public class Contact {

/**

   * Add private instance variables here. See the class structure given in the

   * assignment description.

   */

  

   private Name name;

   private String phoneNumber;

/**

   * Implement your constructor here. Initialize the phoneNumber to null.

   */

public Contact(Name name) {

//throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

       this.name = name;

this.phoneNumber = null;

}

/**

   * Implement your addPhoneNumber method here.

   *

   * The phone number should be in the format of three digits followed by a dash,

   * followed by another three digits, followed by a dash, and finally followed by

   * four digits (i.e., 301-405-2755). Return false if the phone number format is

   * incorrect or null.

   *

   */

public boolean addPhoneNumber(String pNumber) {

//throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

   boolean check = false;

       boolean check1 = false;

       boolean check2 = false;

       this.phoneNumber = pNumber;

      

       if(pNumber.length() == 12) {

           int count = 0;

           for(int i=0; i<3; i++) {

               if(Character.isDigit(pNumber.charAt(i))) {

                   count++;

                   if(count == 3) {

                       check = true;

                   }

               }

           }

          

           String hyphen = "";

hyphen += pNumber.charAt(3);

           if(hyphen.equals("-")) {

               int count1 = 0;

               for(int i=4; i<7; i++) {

                   if(Character.isDigit(pNumber.charAt(i))) {

                       count1++;

                       if(count1 == 3) {

                           check1 = true;

                       }

                   }

               }

           }

           String hyphen1 = "";

hyphen1 += pNumber.charAt(7);

           if(hyphen1.equals("-")) {

               int count2 = 0;

               for(int i=8; i<12; i++) {

                   if(Character.isDigit(pNumber.charAt(i))) {

                       count2++;

                       if(count2 == 4) {

                           check2 = true;

                       }

                   }

               }

           }

          

           if(check == true && check1 == true && check2 == true) {

               return true;

           }

       }

      

       return false;

  

}

/**

   * Implement your toString method. The output should be in the format:

   *

   * Name: __________ (Phone Number: ___-___-____)

   *

   * For example, if a contact has the first name John, last name Doe and middle

   * name Xavier, and the phone number 123-456-7890, this should return:

   *

   * Name: Doe, John Xavier (Phone Number: 123-456-7890)

   *

   * Do not insert new line character after the last digit of the phone number.

   */

public String toString() {

//throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

       return "Name: "+name+" (Phone Number: "+phoneNumber+")";

}

  

  

   public boolean equals(Object obj) {

       if (this == obj)

           return true;

       if (obj == null)

           return false;

       if (getClass() != obj.getClass())

           return false;

       Contact other = (Contact) obj;

       if (name == null) {

           if (other.name != null)

               return false;

       } else if (!name.equals(other.name))

           return false;

      

       return true;

   }

}

public class Name {

/**

   * Add private instance variables here. See the class structure given in the

   * assignment description.

   */

  

   private String firstName;

   private String lastName;

   private String middleName;

  

/**

   * Add your constructor here, referring to the class figure given in the

   * assignment description.

   */

public Name(String firstName, String lastName, String middleName) {

//throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

       this.firstName = firstName;

       this.lastName = lastName;

       this.middleName = middleName;

}

/**

   * Implement your toString method. The output should be in the format:

   * "Lastname, FirstName Middlename". For example, if the first name is John,

   * last name is Doe and middle name is Xavier, this method should return:

   *

   * "Doe, John Xavier"

   *

   * If the middle name were empty or null, this this method should return:

   *

   * "Doe, John"

   *

   */

public String toString() {

//throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

       if(middleName == null || middleName.equals("")) {

           return lastName+", "+firstName;

       }else {

          

           return lastName+", "+firstName+" "+middleName;

       }

}

/**

   * Complete the equals method

   */

public boolean equals(Object obj) {

//throw new UnsupportedOperationException("Remove this line and replace with your implementation.");

   if (this == obj)

           return true;

       if (obj == null)

           return false;

       if (getClass() != obj.getClass())

           return false;

       Name other = (Name) obj;

       if (firstName == null) {

           if (other.firstName != null)

               return false;

       } else if (!firstName.equals(other.firstName))

           return false;

       if (lastName == null) {

           if (other.lastName != null)

               return false;

       } else if (!lastName.equals(other.lastName))

           return false;

       if (middleName == null) {

           if (other.middleName != null)

               return false;

       } else if (!middleName.equals(other.middleName))

           return false;

       return true;

   }

}

Explanation / Answer

import java.util.ArrayList; import java.util.Arrays; public class ContactManager { // declare the contact ArrayList as an instance variable here. private ArrayList contacts; /** * Initialize an empty ContactManager. */ public ContactManager() { contacts = new ArrayList(); } /** * Initialize a ContactManager which contains all the Contacts in the given * array. */ public ContactManager(Contact[] contacts) { for(Contact contact: contacts) { this.contacts.add(contact); } } /** * Given 4 String parameters, add a contact to the contacts list. If first name * or last name is null or an empty string, then do not add a contact but return * false. * * Otherwise, create a new contact, and add the phone number to the new contact. * If the phone number is in a valid format, add the contact to the list and * return true. You may not rewrite the validity check method but can simply * reuse the Contact class code written for L5. * * We allow adding duplicate contacts. Note that there is no limit to how many * times this method can be called. * * @param fName * @param lName * @param mName * @param phoneNumber * @return a boolean value. See the description above */ public boolean addContact(String fName, String lName, String mName, String phoneNumber) { if(fName == null || lName == null) { return false; } else { Contact contact = new Contact(new Name(fName, lName, mName)); if(contact.addPhoneNumber(phoneNumber)) { contacts.add(contact); return true; } else { return false; } } } /** * Remove the Contact with Name n from your List. Make no changes if it doesn't * exist. * * @param n */ public void removeContact(Name n) { for(int i = 0; i