Question: How do I complete this lab in Java? In this lab, your task is to implement a data structure called a Bag. A Bag literally
How do I complete this lab in Java?


In this lab, your task is to implement a data structure called a Bag. A Bag literally performs like a physical bag. You put objects in a bag, and pull things out of it. There is no particular order to Bag. Everything is jumbled up in there. We'll use a simple Java array as the internal representation of a bag of items. Our items will be Integers, so we will use an array of Integers backed by two integer indices. public class Bag \{ private int [] items; private int startIndex; private int endIndex; ... In our implementation, we initialize an array of initial size 4 to hold our items. The start index points to the position of the first valid entry, and the end index points to the position where the next item will be added to the array. When the array is created initially, start index is 1 and the end index is 0. Putting an item adds it to the items array and the end index is incremented by 1 . If there is not enough space in the items array to hold any more new items, a new items array is created that is twice the size of the original one, and the items are copied over to the new items array. Getting an item retrieves it from the start index, at which point the start index is incremented by one (the item is pulled out of the bag, and hence is no longer in the bag). Discussion What other ways are there to implement Bag? Come up with at least 2 options. How can you represent different types of Bag in an object-oriented inheritance hierarchy? For example, a Stack can be considered a type of Bag. What about a Queue? What should be the superclass, and what should be the subclass? Write these ideas as comments at the top of your file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
