/* * ArrayBag.java * * A blueprint class for objects that represent a bag of other objects -- * i.e., a collection of items in which the items do not have a position. * This implementation uses an array to store to objects in the bag. * * Computer Science 112 */ public class ArrayBag { /** * The array used to store the items in the bag. */ private Object[] items; /** * The number of items in the bag. */ private int numItems; public static final int DEFAULT_MAX_SIZE = 50; /** * Constructor with no parameters - creates a new, empty ArrayBag with * the default maximum size. */ public ArrayBag() { this.items = new Object[DEFAULT_MAX_SIZE]; this.numItems = 0; } /** * A constructor that creates a new, empty ArrayBag with the specified * maximum size. */ public ArrayBag(int maxSize) { if (maxSize <= 0) { throw new IllegalArgumentException("maxSize must be > 0"); } this.items = new Object[maxSize]; this.numItems = 0; } /** * numItems - accessor method that returns the number of items * in this ArrayBag. */ public int numItems() { return this.numItems; } /** * add - adds the specified item to this ArrayBag. Returns true if * there is room to add it, and false otherwise. Throws an * IllegalArgumentException if the item is null. */ public boolean add(Object item) { if (item == null) { throw new IllegalArgumentException("item must be non-null"); } else if (this.numItems == this.items.length) { return false; // no more room! } else { this.items[this.numItems] = item; this.numItems++; return true; } } /** * remove - removes one occurrence of the specified item (if any) * from this ArrayBag. Returns true on success and false if the * specified item (i.e., an object equal to item) is not in this * ArrayBag. */ public boolean remove(Object item) { for (int i = 0; i < this.numItems; i++) { if (this.items[i].equals(item)) { // Shift the remaining items left by one. for (int j = i; j < this.numItems - 1; j++) this.items[j] = this.items[j + 1]; this.items[this.numItems - 1] = null; this.numItems--; return true; } } return false; // item not found } /** * contains - returns true if the specified item is in the Bag, and * false otherwise. */ public boolean contains(Object item) { for (int i = 0; i < this.numItems; i++) { if (this.items[i].equals(item)) { return true; } } return false; } /** * grab - returns a reference to a randomly chosen item in this ArrayBag. */ public Object grab() { if (this.numItems == 0) { throw new IllegalStateException("the bag is empty"); } int whichOne = (int)(Math.random() * this.numItems); return this.items[whichOne]; } /** * toArray - return an array containing the current contents of the bag */ public Object[] toArray() { Object[] copy = new Object[this.numItems]; for (int i = 0; i < this.numItems; i++) { copy[i] = this.items[i]; } return copy; } /** * toString - converts this ArrayBag into a string that can be printed. * Overrides the version of this method inherited from the Object class. */ public String toString() { String str = "{"; for (int i = 0; i < this.numItems; i++) { str = str + this.items[i]; if (i != this.numItems - 1) { str += ", "; } } str = str + "}"; return str; } /* * Lab 3, Task 3.9: hasMoreRoom - returns true if this ArrayBag has * more room left for items (i.e., more unused array elements) * than the ArrayBag other does, and false otherwise. */ public boolean hasMoreRoom(ArrayBag other) { if (other == null) { throw new IllegalArgumentException(); } int roomInThis = this.items.length - this.numItems; int roomInOther = other.items.length - other.numItems; return (roomInThis > roomInOther); } /* Test the ArrayBag implementation. */ public static void main(String[] args) { ArrayBag b = new ArrayBag(); b.add("don't blink"); b.add("baggy"); // calls the toString() method above System.out.println(b); // Note that we need a type cast here, // as explained in the lab exercise. String s = (String)b.grab(); System.out.println(s); ArrayBag b1 = new ArrayBag(10); ArrayBag b2 = new ArrayBag(12); System.out.println(b2.hasMoreRoom(b1)); b2.add("hello"); b2.add("world"); System.out.println(b2.hasMoreRoom(b1)); } }