(Solved) : 23 34 Pts Implement Methods Adt Bag Integers Using Linked List Bag Container Collection It Q44088619 . . .

23 (34 pts) Implement the methods of the ADT Bag of integers using a linked list. A Bag is just a container for a collection of items (analogy: a bag of keys) with the following behaviors: — The positions of data items do not matter. For instance, (3, 2, 10,6) is equivalent to {2, 3, 6, 10) — There can be duplicate items (e.g., {7,2, 10, 7,5}) You may assume all required libraries have been imported. Your task is to implement the ADT Bag class with the following public methods: (10pts). public void addint item): add an item to the Bag (you may insert it at the front – the order of insertions does not matter). You may draw some pictures to help you implement methods correctly, e.g. adding an item at the front of the link: -12 (10pts). public boolean remove(int item): remove one occurrence of item (if any) from the Bag. Return true if removal is successful; otherwise return false. (10pts). public int grab(: get an item at random, without removing it, which reflects the fact that item orders do not matter (and we can’t say “get the 5th item in the Bag”). Hint: Math.random( generates doubles between 0 (inclusive) and 1 (exclusive). Throw an exception when the user attempts to get an item from an empty bag. (2pts). public int size(): return the number of items in the Bag (2pts). public boolean isEmpty: indicate whether no items are stored in the Bag public class Bag public class Node { int item; Node next; // default value is null private Node head; private int size; public Bag() // constructor { head = null; size = 0; } 17 your implementation – Start with easy ones public int size() // OK to use “size” for a method & an instance variable Show transcribed image text 23 (34 pts) Implement the methods of the ADT Bag of integers using a linked list. A Bag is just a container for a collection of items (analogy: a bag of keys) with the following behaviors: — The positions of data items do not matter. For instance, (3, 2, 10,6) is equivalent to {2, 3, 6, 10) — There can be duplicate items (e.g., {7,2, 10, 7,5}) You may assume all required libraries have been imported. Your task is to implement the ADT Bag class with the following public methods: (10pts). public void addint item): add an item to the Bag (you may insert it at the front – the order of insertions does not matter). You may draw some pictures to help you implement methods correctly, e.g. adding an item at the front of the link: -12 (10pts). public boolean remove(int item): remove one occurrence of item (if any) from the Bag. Return true if removal is successful; otherwise return false.
(10pts). public int grab(: get an item at random, without removing it, which reflects the fact that item orders do not matter (and we can’t say “get the 5th item in the Bag”). Hint: Math.random( generates doubles between 0 (inclusive) and 1 (exclusive). Throw an exception when the user attempts to get an item from an empty bag. (2pts). public int size(): return the number of items in the Bag (2pts). public boolean isEmpty: indicate whether no items are stored in the Bag public class Bag public class Node { int item; Node next; // default value is null private Node head; private int size; public Bag() // constructor { head = null; size = 0; } 17 your implementation – Start with easy ones public int size() // OK to use “size” for a method & an instance variable
Expert Answer
Answer to 23 (34 pts) Implement the methods of the ADT Bag of integers using a linked list. A Bag is just a container for a collec…
OR