Programming Project: Java Collections, the old way and the new way
publicclass ArrayBagimplementsBag
/**
* This java interface represents a Bag type. Duplicate elements are allowed.
* A class implementing this interface can be used to store any type element
* because of the Object parameters and the Object return types.
*
* @author Rick Mercer
*/
publicinterface Bag {
// Return true if this Bag has no elements.
publicboolean isEmpty();
// Return the number of elements currently in this Bag.
publicint size();
// Add element to this Bag even if element equals another element
// already in this Bag. New elements may be added anywhere.
publicvoid add(Object element);
// Return a reference to the element at the given index.
// Precondition: 0 <= index < size()
publicObject get(int index);
// Return how frequently element exists in this Bag.
// Precondition: element overrides equals to compare state
public int occurencesOf(Object element);
// Add all elements from other to this Bag.
publicvoid addAll(Bag other);
// Remove an element from this Bag if it equals the argument and return true.
// If element is not found, return false and make no changes made to this Bag.
// Precondition: element overrides equals to compare state
publicboolean remove(Object element);
// Remove all elements from this Bag that equals the argument and return true.
// Return the number of elements removed from this Bag, which could be 0.
// If element is not found, return false and make no changes made to this Bag.
// Precondition: element overrides equals to compare state
publicint removeAll(Object element);
}
Implement a Generic collection class that implements the Bag<E> interface (retype all of the interface code below or download Bag.java into your Eclipse project). Use a type parameter such as <E> to allow for type-safe Bags. Use an Object[] instance variable.
/**
* This java interface represents a Bag type. Duplicate elements are not allowed.
*
* @author Rick Mercer
*
* @param<E>
* The type of elements any class implementing Bag can store.
*/
publicinterfaceBag<E> {
// Return the number of elements currently in this Bag.
publicint size();
// Return true if this Bag has no elements.
publicboolean isEmpty();
// Add element to this Bag only if element does not equal another element
// already in this Bag and return false. If element already exists, return
// false with no changes made to this Bag.
publicboolean add(E element);
// Add all unique elements from other to this Bag. Return true if one or more
// elements gets added to this Bag.
publicboolean addAll(Bag<E> other);
// Return true if element exists in this Bag or false if element is not found.
publicboolean contains(E element);
// Return true if this Bag contains every element in other.
publicboolean containsAll(Bag<E> other);
// Remove an element from this Bag if it equals the argument and return true.
// If element is not found, return false; no changes made to this Bag.
publicboolean remove(E element);
}
Class ArrayBag<E> must begin like this:
publicclass ArrayBag<E> implementsBag<E> {
private Object[] data;
private int maxCapacity;
public ArrayBag(int maxCapacitiy) {
this.maxCapacity = maxCapacity;
Grading Criteria 20pts
+20 WebCat score for code and problem coverage
-15 if you did not use an Object[] instance variable or failed to implement Bag<E>