Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Items can be inserted into the bag in any order, one at a time. The operations o

ID: 3553977 • Letter: I

Question

Items can be inserted into the bag in any order, one at a time. The operations on a bag are; insert,

removeOne, removeAll, display, and size. The BagElement class will have two data members, the value itself and count for the number of times the value is in the bag. Create the bag layered on a LinkedList or an ArrayList. The class must raise exceptions if

you attempt to remove from the bag and it is empty or the item in question is not in the bag.

What is given so far:


Exception class(es)

public class Bag<T>

{

public class BagElement<T>

{

private T data;

private int count;

public BagElement(T d)

public void addOne()

public void removeOne()

public boolean equals(Object o)

public int getCount()

public T getData()

}

//declare data members of the bag here

public Bag()

public boolean empty()

public boolean removeAll(T item) throws

//post-condition item is completely removed from the bag

public int size()

//post-condition number of different items in the bag, not the total number

// of objects in the bag

public void insert (T item)

//post condition item is added to the bag, if item is already in the bag its count

//is incremented, if not the item is added to the bag with a count of 1.

public void removeOne(T item) throws

//post-condition *one* item is removed from the bag, if the item already

//exists its count is decremented, if removing the item causes count = 0 the

//item should be removed from the bag completely

public void display()

}


// The layout is above, please stay to this. If someone could show me how to implement this I would really appreciate it. Thanks

Explanation / Answer

( Done EXACTLY following the mentioned Specifications )