Source of ArrayBag.java


  1: /**
  2:     A class of bags whose entries are stored in a fixed-size array.
  3:     INITIAL, INCOMPLETE DEFINITION; no security checks
  4:     @author Frank M. Carrano, Timothy M. Henry
  5:     @version 5.0
  6: */
  7: public final class ArrayBag<T> implements BagInterface<T>
  8: {
  9:         private final T[] bag;
 10:         private int numberOfEntries;
 11:         private static final int DEFAULT_CAPACITY = 25;

 13:         /** Creates an empty bag whose initial capacity is 25. */
 14:         public ArrayBag()
 15:         {
 16:                 this(DEFAULT_CAPACITY);
 17:         } // end default constructor

 19:         /** Creates an empty bag having a given initial capacity.
 20:        @param desiredCapacity  The integer capacity desired. */
 21:         public ArrayBag(int desiredCapacity)
 22:         {
 23:       // The cast is safe because the new array contains null entries.
 24:       @SuppressWarnings("unchecked")
 25:       T[] tempBag = (T[])new Object[desiredCapacity]; // Unchecked cast
 26:       bag = tempBag;
 27:       numberOfEntries = 0;
 28:         } // end constructor

 30:         /** Adds a new entry to this bag.
 31:        @param newEntry  The object to be added as a new entry.
 32:        @return  True if the addition is successful, or false if not. */
 33:         public boolean add(T newEntry)
 34:         {
 35:                 // To be defined
 36:         } // end add
 37:    
 38:         /** Retrieves all entries that are in this bag.
 39:        @return  A newly allocated array of all the entries in this bag. */
 40:         public T[] toArray()
 41:         {
 42:                 // To be defined
 43:         } // end toArray
 44:         
 45:    // Returns true if the array bag is full, or false if not.
 46:         private boolean isArrayFull()
 47:         {
 48:                 // To be defined
 49:         } // end isArrayFull
 50: } // end ArrayBag