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

hi i have been working on this assignment but could not find a way out (this is

ID: 3561742 • Letter: H

Question

hi i have been working on this assignment but could not find a way out (this is and old one and i am little bit rusty )

Loopbag

You will implement
the following interface:

import java.util.Iterator;
public interface LoopBag extends Iterable {
/**
* Adds the given item to bad LoopBag.
* @param item the item to add
*/
void insert(E item);
/**
* Returns the number of items in this LoopBag.
* @return the number of items in this LoopBag
*/
int size();
/**
* Returns true i f this LoopBag is empty and false otherwise.
* @return true i f this LoopBag is empty; false otherwise.
**/
boolean isEmpty();

/**
* i f the bag contains a given item?
* @return true i f bag contains the item. false otherwise
*/
boolean contains(E item);
/**
* creates the union with the given LoopBag
*/
void union(LoopBag lb);
/**
* Returns an iterator for this LoopBag. Iterator iterates from oldest to newest.
* @return an iterator for this LoopBag
*/
Iterator iterator();
}

LoopBag bag = new LoopBag(5);
bag.insert(1);
bag.insert(2);
bag.insert(3);

bag.insert(4);
bag.insert(5);
bag.insert(6);
bag.insert(7);
for(Integer i: bag){
System.out.print(i+",");
}
Output:
3,4,5,6,7

You are not allowed to use Java ArrayList. You have to implement the LoopBag using basic arrays.

Iterator iterates from the oldest to the newest.

Thank you for your help!

Explanation / Answer

import java.util.Collection; import java.util.Iterator; class Bag implements Collection{ private T[] array; public int bagSize; public Bag(){ array=(T[])new Object[10]; } public Bag(Collection other ){ //Not sure what to put here //creates a bag containing all of the items passed to it as a Collection } public int size() { return bagSize; } public boolean isEmpty() { if(size()==0) return true; else return false; } public boolean contains(Object o) { //Not sure what to put here /*Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)). */ return (o.toArray()==null ? this.toArray()==null : o.toArray() == this.toArray()); } } public Iterator iterator() { throw new UnsupportedOperationException("not implemented."); } public Object[] toArray() { return array; } public T[] toArray(T[] a) { throw new UnsupportedOperationException("not implemented."); } public boolean add(T e) { if(bagSize>=array.length) return false; else { ensureCapacity(bagSize+10); array[bagSize]=e; bagSize++; return true; } } public boolean remove(Object o) { for(int i=0; i c) { throw new UnsupportedOperationException("not implemented."); } public boolean retainAll(Collection c) { throw new UnsupportedOperationException("not implemented."); } public void clear() { //Not sure what to put here /*Removes all of the elements from this collection (optional operation). The collection will be empty after this call returns (unless it throws an exception).*/ } @Override public int hashCode(){ throw new UnsupportedOperationException("not implemented."); } @Override public boolean equals(Object e) { if (e == null) { return false; } if (getClass() != e.getClass()) { return false; } final Bag other = (Bag) e; return true; } public void ensureCapacity(int minCapacity){ T[] biggerArray; if(array.length