Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java language, make a program that allows a user to add their contacts (name and phone #) to a list, sorted in alphabetical order.

Using Java language, make a program that allows a user to add their contacts (name and phone #) to a list, sorted in alphabetical order.

Create the following classes:

  1. A Contact class, which should have two String reference variables, one referring to the name of the contact and the other referring to that contact's phone number.
  2. A ContactList class, which contains a Singly linked list. It should have a private inner class Node, which has a reference to a Contact object and a next pointer to another Node object. The ContactList itself should have a reference variable, firstNode, which refers to the start of the linked list.

Implement the following methods:

  1. add(). Adds a new contact object to the list. This will be a little different from what we have done in class. We want the contact list to remain sorted in alphabetical order. Therefore, whenever a contact is added to the list, you need to find the location it belongs in within the list and place it there. Do not worry about sorting by last name, just sort by overall name i.e. "Ryan Adams" should go after "Jill Smith".
  2. toString(). Should override Object's toString method. Nicely formats the list of contacts, printing out their names and phone numbers
  3. remove(String name). Remove a specific contact from the list.
  4. removeLast(). Properly remove the last contact from the list. Throw an exception if the list is empty!
  5. changePhoneNumber(). Change the phone number of a specific contact.

Make sure to properly update pointers when adding and removing Nodes!

Finally, write a program that thoroughly tests ContactList. Make sure to test as many special cases as possible (i.e. removing the last Node from a list, etc).

Note: You are not required to perform a sorting algorithm on the list! When you enter a new Contact, add() simply has to find the correct location of the newly created Node and place it there. If your add method works properly, every time you add a new Node the rest of the list should already be sorted! Determining if one string is greater or less than another string can be done manually, but it is not necessary and is a ton of extra work. I highly recommend looking into how String's compareTo method works and using this in your program.

Please provide comments in the code!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Office 365 For Beginners 2022 8 In 1

Authors: James Holler

1st Edition

B0B2WRC1RX, 979-8833565759

More Books

Students also viewed these Databases questions