Question
3rd part of the total question: Design a new Generic class called LinkedBag that implements the Generic Bag Interface created earlier. This class will include
3rd part of the total question:
Design a new Generic class called LinkedBag that implements the Generic Bag Interface created earlier. This class will include a Type Parameter as part of class declaration. A client class using LinkedBag will specify the actual type.
Declare an instance variable list a the SinglyLinkedList of Generic type from Task 1. This structure will hold the items in the bag.
You will not need to declare an instance variable for size in this LinkedBag class.
When you need to know how many items are in the list you can call the size () method from the SinglyLinkedList class.
Let the SinglyLinkedList class keep track of the number of items in the LinkedBag.
Adding a separate size variable to the LinkedBag class will require you to do unnecessary work to maintain the value of the size variable. And, if you incorrectly maintain the size variable in the LinkedBag class then you may run into the situation where the sizes are not equal.
Provide a default constructor that will initialize the instance variable bag with an empty Singly Linked list.
Provide and overload constructor that allows the client to specify the initial capacity of the bag.
This constructor can ignore the parameter and just call the default constructor.
This constructor allows the LinkedBag to provide the Client with the same functionality as the ArrayBag.
Your Client should not care if it uses an ArrayBag or a LinkedBag, they should work exactly the same for the Clients perspective.
Implement the methods received from the interface.
The method that removes a specific item which is passed as a parameter should use the objects equals( )method to compare the contents of object in the bag with the contents of the parameter. If there is an object in the bag with the same contents then, it removes the first occurrence of that item from the bag and returns a reference to the object removed. It returns a null of there is no item with equal contents.
The method that removes a random item should return a reference to the object being removed. If the list is empty it should return null.
Implement a method get( int index ) that
Returns an object reference to the object at the index location in the list.
This method should throw an ArrayIndexOutOfBound exception when appropriate.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started