Question: Part 4 : Implement class TypesLists You will use your OrderedList collection class in an application that needs to maintain two subsets of items, one

Part 4: Implement class TypesLists
You will use your OrderedList collection class in an application that needs to maintain two subsets of items, one subset is of type and the other subset is of type .
Reminder: test all methods in OrderedList before you start implementing the method in TypesLists.
Instance Variables:
private OrderedList List
A list that is used to store items of type ItemA.
private OrderedList List
A list that is used to store items of type ItemB.
Constructor:
public TypesLists()
Construct the empty ordered lists for ItemA and ItemB (i.e., initialize the instance variables).You will need to call the OrderedList constructor to construct each list.
Methods:
Implement the following methods. Use the exact method headers as specified. Replace Item with your specific item name.
public void add(Item element)
Adds element to the correct list. Find the correct list by using the instanceof operator.If the Item is not of subtype ItemA or ItemB, throw a new ClassNotFoundException and catch it. When you catch it, terminate the program with the following error message to the console:Element not instance of either subclass.
public int sizeOf( OrderedList iol )
Returns the number of nodes in the given list.
public void display( OrderedList iol )
Displays the contents of the specified list. 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 element to the correct 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, or if element is not of subtype ItemA or ItemB. The method returns true if the item is added and false otherwise.[1]
public boolean remove( Item target )
Removes one occurrence of target if there is at least one instance of target in the correct list.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 the equals() method to search for target.
public boolean remove( OrderedList iol, int index )
Removes the Item located at position index in the OrderedList iol where the head node is at index 1. The method returns true if an element 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 in the appropriate ordered list. Returns -1 if target is not in the list. Use the equals() method to search for target.
public OrderedList getList( char type )
Returns the OrderedList of the given type (a for OrderedList or b for OrderedList). The character should be case-insensitive. Return null if type is invalid.
public Item get( OrderedList iol, int index )
Returns the item at position index in the specified list. Returns null if index is less than 1 or greater than the size of the list.
[1] Looking ahead, note that the two add methods show two different ways of handling invalid data: throwing an exception or returning false. When we study Queues you will see that the Java API has two methods for adding/removing objects from a queue, one of which throws exceptions and the other of which returns a boolean value.

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!