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.)

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

{ Red Pink Yellow Black }

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.

2. public int size()

The size method returns the number of strings that the linked list contains at the time when this method is called.

3. public int searchElement(Object element)

The searchElement method searches the element in the parameter (in this assignment, it will be a string), then returns the index of the first occurrence of the element. If it is not found, then it returns -1.

4. 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.

5. public void setElement(int index, Object element)

The setElement sets the parameter element at the parameter specified index by deleting the element at the index. If the parameter index is larger or smaller than the existing indices, it should throw an object of the IndexOutOfBoundsException class.

6. public void addElement(int index, Object element)

The addElement adds the parameter element at the parameter specified index. The element at the index and any elements at the later indices will be shifted towards the end of the list. If the parameter index is larger or smaller than the existing indices, it should throw an object of the IndexOutOfBoundsException class.

7. public void removeLastFew(int howMany)

The removeLastFew method removes the parameter specified number of elements from the end of the linked list. For instance, if the parameter integer is 3, then it should remove the last three elements of the linked list. If the parameter integer is larger than the current size of the linked list, then the linked list should become empty. If the parameter integer is less than or equals to 0, nothing should be removed from the linked list.

8. public void reverse()

The reverse method reverses the elements in the linked list.

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.