1/* 2 * ArrayBag.java3 * 4 * Computer Science S-225 */6 7import java.util.*;8 9/**10 * An simple implementation of a bag data structure that uses 11 * an array to store the items.12 */13public class ArrayBag {14 /** 15 * The array used to store the items in the bag.16 */17 private Object[] items;18 19 /** 20 * The number of items in the bag.21 */22 private int numItems;23 24 public static final int DEFAULT_MAX_SIZE = 50;25 26 /**27 * Constructor with no parameters - creates a new, empty ArrayBag with 28 * the default maximum size.29 */30 public ArrayBag() {31 this.items = new Object[DEFAULT_MAX_SIZE];32 this.numItems = 0;33 }34 35 /** 36 * A constructor that creates a new, empty ArrayBag with the specified37 * maximum size.38 */39 public ArrayBag(int maxSize) {40 if (maxSize <= 0) {41 throw new IllegalArgumentException("maxSize must be > 0");42 }43 this.items = new Object[maxSize];44 this.numItems = 0;45 }46 47 /**48 * numItems - accessor method that returns the number of items 49 * in this ArrayBag.50 */51 public int numItems() {52 return this.numItems;53 }54 55 /** 56 * add - adds the specified item to this ArrayBag. Returns true if there 57 * is room to add it, and false otherwise.58 * Throws an IllegalArgumentException if the item is null.59 */60 public boolean add(Object item) {61 if (item == null) {62 throw new IllegalArgumentException("item must be non-null");63 } else if (this.numItems == this.items.length) {64 return false; // no more room!65 } else {66 this.items[this.numItems] = item;67 this.numItems++;68 return true;69 }70 }71 72 /** 73 * remove - removes one occurrence of the specified item (if any)74 * from this ArrayBag. Returns true on success and false if the75 * specified item (i.e., an object equal to item) is not in this ArrayBag.76 */77 public boolean remove(Object item) {78 for (int i = 0; i < this.numItems; i++) {79 if (this.items[i].equals(item)) {80 // Shift the remaining items left by one.81 for (int j = i; j < this.numItems - 1; j++) {82 this.items[j] = this.items[j + 1];83 }84 this.items[this.numItems - 1] = null;85 86 this.numItems--;87 return true;88 }89 }90 91 return false; // item not found92 }93 94 /**95 * contains - returns true if the specified item is in the Bag, and96 * false otherwise.97 */98 public boolean contains(Object item) {99 for (int i = 0; i < this.numItems; i++) {100 if (this.items[i].equals(item)) {101 return true;102 }103 }104 105 return false;106 }107 108 /**109 * containsAll - does this ArrayBag contain all of the items in110 * otherBag? Returns false if otherBag is null or empty. 111 */112 public boolean containsAll(ArrayBag otherBag) {113 if (otherBag == null || otherBag.numItems == 0) {114 return false;115 }116 117 for (int i = 0; i < otherBag.numItems; i++) {118 if (! this.contains(otherBag.items[i])) {119 return false;120 }121 }122 123 return true;124 }125 126 /**127 * grab - returns a reference to a randomly chosen item in this ArrayBag.128 */129 public Object grab() {130 if (this.numItems == 0) {131 throw new IllegalStateException("the bag is empty");132 }133 134 int whichOne = (int)(Math.random() * this.numItems);135 return this.items[whichOne];136 }137 138 /**139 * toArray - return an array containing the current contents of the bag140 */141 public Object[] toArray() {142 Object[] copy = new Object[this.numItems];143 144 for (int i = 0; i < this.numItems; i++) {145 copy[i] = this.items[i];146 }147 148 return copy;149 }150 151 /**152 * toString - converts this ArrayBag into a string that can be printed.153 * Overrides the version of this method inherited from the Object class.154 */155 public String toString() {156 String str = "{";157 158 for (int i = 0; i < this.numItems; i++) {159 str = str + this.items[i];160 if (i != this.numItems - 1) {161 str += ", ";162 }163 }164 165 str = str + "}";166 return str;167 }168 169 /* Test the ArrayBag implementation. */170 public static void main(String[] args) {171 // Create a Scanner object for user input.172 Scanner scan = new Scanner(System.in);173 174 // Create an ArrayBag named bag1.175 System.out.print("size of bag 1: ");176 int size = scan.nextInt();177 ArrayBag bag1 = new ArrayBag(size);178 scan.nextLine(); // consume the rest of the line179 180 // Read in strings, add them to bag1, and print out bag1.181 String itemStr; 182 for (int i = 0; i < size; i++) {183 System.out.print("item " + i + ": ");184 itemStr = scan.nextLine();185 bag1.add(itemStr);186 }187 System.out.println("bag 1 = " + bag1);188 System.out.println();189 190 // Select a random item and print it.191 Object item = bag1.grab();192 System.out.println("grabbed " + item);193 System.out.println();194 195 // Iterate over the objects in bag1, printing them one per196 // line.197 Object[] items = bag1.toArray();198 for (int i = 0; i < items.length; i++) {199 System.out.println(items[i]);200 }201 System.out.println();202 203 // Get an item to remove from bag1, remove it, and reprint the bag.204 System.out.print("item to remove: ");205 itemStr = scan.nextLine();206 if (bag1.contains(itemStr)) {207 bag1.remove(itemStr);208 }209 System.out.println("bag 1 = " + bag1);210 System.out.println();211 212 scan.close();213 }214}
Object[] items
Field defined at line 17.
int numItems
Field defined at line 22.
int DEFAULT_MAX_SIZE
Field defined at line 24.
ArrayBag()
Constructor defined at line 30.
ArrayBag(maxSize)
Constructor defined at line 39.
numItems()
Method defined at line 51.
add(item)
Method defined at line 60.
remove(item)
Method defined at line 77.
contains(item)
Method defined at line 98.
containsAll(otherBag)
Method defined at line 112.
grab()
Method defined at line 129.
toArray()
Method defined at line 141.
toString()
Method defined at line 155.
main(args)
Method defined at line 170.