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

Consider this code using the ArrayBag: Integer i = new Integer(42); Integer j =

ID: 3645154 • Letter: C

Question

Consider this code using the ArrayBag:

Integer i = new Integer(42);
Integer j = new Integer(43);
ArrayBag b = new ArrayBag( );

// Add two i's and one j:
b.add(i);
b.add(i);
b.add(j);

Draw a diagram showing the reference variables i and j with their pointers to Integer objects, and also show b's data array with its pointers after these add statements.

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