1: /** 2: A class of bags whose entries are stored in a fixed-size array. 3: INITIAL, INCOMPLETE DEFINITION; no security checks 4: @version 4.0 5: @author Frank M. Carrano 6: */ 7: public final class ArrayBag1<T> implements BagInterface<T> 8: { 9: private final T[] bag; 10: private int numberOfEntries; 11: private static final int DEFAULT_CAPACITY = 25; 12: 13: /** Creates an empty bag whose capacity is 25. */ 14: public ArrayBag1() 15: { 16: this(DEFAULT_CAPACITY); 17: } // end default constructor 18: 19: /** Creates an empty bag having a given capacity. 20: @param capacity The integer capacity desired. */ 21: public ArrayBag1(int capacity) 22: { 23: // The cast is safe because the new array contains null entries 24: @SuppressWarnings("unchecked") 25: T[] tempBag = (T[])new Object[/*desiredC*/capacity]; // Unchecked cast 26: bag = tempBag; 27: numberOfEntries = 0; 28: } // end constructor 29: 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: boolean result = true; 36: if (isArrayFull()) 37: { 38: result = false; 39: } 40: else 41: { // Assertion: result is true here 42: bag[numberOfEntries] = newEntry; 43: numberOfEntries++; 44: } // end if 45: 46: return result; 47: } // end add 48: 49: /** Retrieves all entries that are in this bag. 50: @return A newly allocated array of all the entries in this bag. */ 51: public T[] toArray() 52: { 53: // The cast is safe because the new array contains null entries. 54: @SuppressWarnings("unchecked") 55: T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast 56: 57: for (int index = 0; index < numberOfEntries; index++) 58: { 59: result[index] = bag[index]; 60: } // end for 61: 62: return result; 63: // Note: The body of this method could consist of one return statement, 64: // if you call Arrays.copyOf 65: } // end toArray 66: 67: // Returns true if the array bag is full, or false if not. 68: private boolean isArrayFull() 69: { 70: return numberOfEntries >= bag.length; 71: } // end isArrayFull 72: 73: // Throws an exception if receiving object is not initialized. 74: // private void checkInitialization() 75: // { 76: // if (!initialized) 77: // throw new IllegalStateException("Uninitialized object used " + 78: // "to call an ArrayBag method."); 79: // } // end checkInitialization 80: // STUBS: 81: /** Sees whether this bag is empty. 82: @return True if this bag is empty, or false if not */ 83: public boolean isEmpty() 84: { 85: return false; //STUB 86: } // end isEmpty 87: 88: /** Gets the number of entries currently in this bag. 89: @return The integer number of entries currently in this bag */ 90: public int getCurrentSize() 91: { 92: return -1; // STUB 93: } // end getCurrentSize 94: 95: /** Removes one unspecified entry from this bag, if possible. 96: @return Either the removed entry, if the removal 97: was successful, or null */ 98: public T remove() 99: { 100: return null; // STUB 101: } // end remove 102: 103: /** Removes one occurrence of a given entry from this bag. 104: @param anEntry The entry to be removed 105: @return True if the removal was successful, or false otherwise */ 106: public boolean remove(T anEntry) 107: { 108: return false; // STUB 109: } // end remove 110: 111: /** Removes all entries from this bag. */ 112: public void clear() 113: { 114: // STUB 115: } // end clear 116: 117: /** Counts the number of times a given entry appears in this bag. 118: @param anEntry The entry to be counted 119: @return The number of times anEntry appears in the bag */ 120: public int getFrequencyOf(T anEntry) 121: { 122: return 0; // STUB 123: } // end getFrequencyOf 124: 125: /** Tests whether this bag contains a given entry. 126: @param anEntry The entry to locate 127: @return True if this bag contains anEntry, or false otherwise */ 128: public boolean contains(T anEntry) 129: { 130: return false; // STUB 131: } // end contains 132: } // end ArrayBag1 133: 134: 135: 136: