Bag - newContents[newIndex] = itr.next(); newIndex++; }...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public class Bag extends BoundedBag { // data members private BoundedBagIterator itr; private int[] newContents; private int scalar = 2; public Bag() { super(); } // @Mutator-only when @requires is met // @requires contents is full // @returns void // @ensures creates newContents with double the capacity and replace // contents with newContents // never idempotent public void expandAndCopy() { capacity = scalar * capacity;// double the capacity int newIndex = 0; newContents = new int[capacity]; itr = this.iterator(); // copy contents into newContents while (itr.hasNext()) {
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: newContents[newIndex] = itr.next(); newIndex++; } contents = new int[capacity]; contents = newContents; } // @Mutator // @requires newContents is not full // @returns none // @ensures size of newContents is incremented by 1 // never idempotent public void add(int theValue) { // creates contents if (size() <= capacity) super.add(theValue); // when contents is full if (size() == capacity) expandAndCopy(); // add additional new items to newContents if (getTopPosition() > capacity - 1 && size() <= capacity) super.add(theValue); } }...
View Full Document

Ask a homework question - tutors are online