Program Description

Class Diagram: See image.

In the Assignment #10, you are given three files Assignment10.java, LinkedList.java, and ListIterator.java. You will need to add additional methods in the LinkedList class in the LinkedList.java file. The LinkedList will be tested using strings only.

Specifically, the following methods must be implemented in the LinkedList class: (You should utilize listIterator() method already defined in the LinkedList class to obtain its LinkedListIterator object, and use the methods in the LinkedListIterator class to traverse from the first element to the last element of the linked list to define the following methods.)

public String toString()

The toString method should concatenate strings in the linked list, and return a string of the following format:

{ Apple Banana Melon Orange }

Thus it starts with "{" and ends with "}", and there is a space between strings and "{" or "}". If the list is empty, it returns "{ }" with a space in between. Note that all elements in the linked list will be added in alphabetical order.

public boolean isEmpty()

The isEmpty method should return true if the link list is empty and return false otherwise.

public Object getElement(int index)

The getElement method returns an element at the given index by its parameter. If the parameter index is larger or smaller than the existing indices, it should throw an object of the IndexOutOfBoundsException class.

public void addElement(Object element)

The addElement adds the parameter element into the linked list. The linked list should contains all elements (strings) in alphabetical order. Therefore, in this addElement method, a correct location to insert the parameter element needs to be searched and the element needs to be inserted in that location.

public void addElements(Object element, int howMany)

The addElements adds the parameter element into the linked list for the number of times specified by the parameter "howMany". The linked list should contains all elements (strings) in alphabetical order. Therefore, in this addElement method, a correct location to insert the parameter element needs to be searched and the element needs to be inserted in that location for the specified number of times.

For instance, if the linked list contains { Apple Banana Melon Orange } and addElements("Lemon", 3) is called, then it will have:

{ Apple Banana Lemon Lemon Lemon Melon Orange }

Lemon is added three times in alphabetical order.

public void searchAndRemove(Object element)

The searchAndRemove method searches the parameter element in the linked list and removes all occurrences of the element in the linked list. If the parameter element does not exist in the linked list, then the linked list should be unchanged.

public void duplicateEach( )

The duplicateEach method makes a duplicate of each element in the linked list. For instance, if the linked list contains { Apple Banana Melon Orange } then calling this method makes it: { Apple Apple Banana Banana Melon Melon Orange Orange }

Test your LinkedList class with the given Assignment10.java file. It is recommended to test boundary cases such as the cases when the linked list is empty, when it contains only one element, when adding/removing at the beginning or at the end of the linked list.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.