Question: Part 2 : Implement class OrderedList The OrderedList class is a collection class that uses a linked list to store a collection of items and

Part 2: Implement class OrderedList
The OrderedList class is a collection class that uses a linked list to store a collection of items and the class supports operations to add/remove/read items from a specific position in the list. The class should include the following:
Strong suggestion: Create the driver class OrderedListDriver (see Part 3) at the same time you create this class, and use it to test each method in this class before you go on to the next method.
Instance variables:
private Node head
A reference variable to the head of the linked list.
private Node tail
A reference variable to the tail of the linked list.
Constructor:
public OrderedList()
A no-argument constructor to construct an empty list.
Methods:
public boolean add(Item element)
Adds an element to the list at the location where it belongs in the ordered list
public int size()
Returns the number of nodes in the list.
public void display()
Displays the contents of the list, one item per line. You need to display the values of the items attributes separated by tab. Do not include any additional text. Note that this method displays the list on the screen and does NOT return a String representation of the list.
public boolean add(int index, Item element)
Adds an element to the list at the given index where the head node is at index 1. If index is greater than the list size, then the element is added as the last element in the linked list. The method does not do anything if index is less than 1. The method returns true if the item is added and false otherwise.
public boolean remove(Item target)
Removes one occurrence from target from the list if any. The method returns true if an item is removed and false otherwise. All other items in the list should remain in the same order. Use equals() method to search for target.
public boolean remove(int index)
Removes the item located at position index in the list where the head node is at index 1. The method returns true if an item is removed and false if no element is removed because index is negative or beyond the list length. This method should not change the order of the other elements in the list.
public int indexOf(Item target)
Returns the index of the first occurrence of target if any and returns -1 if target is not in the list. Use equals() method to search for target.
public Item get(int index)
Returns the item at position index in the list and returns null if index is less than 1 or greater than the size of the list.
Part 3: Implement class OrderedListDriver
Create a driver class, OrderedListDriver, and use it to test each method before you implement to the following method. A part of your grade for each method is allocated to the method test from the driver class. This class only needs a main method to test the OrderedListDriver class, you dont need to create an object of this class.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!