Repeat Project, but instead define a class of sets that implements the interface SetInterface. Recall from Project in Chapter 1 that a set is a bag whose entries are distinct. Define SetInterface by extending BagInterface, as defined in Listing in Chapter 1.
LISTING A Java interface for a class of bags
/**An interface that describes the operations of a bag of objects.@author Frank M. Carrano*/public interface BagInterface{ /** Gets the current number of entries in this bag. @return the integer number of entries currently in the bag */ public int getCurrentSize(); /** Sees whether this bag is full. @return true if the bag is full, or false if not */ public boolean isFull(); /** Sees whether this bag is empty. @return true if the bag is empty, or false if not */ public boolean isEmpty(); /** Adds a new entry to this bag. @param newEntry the object to be added as a new entry @return true if the addition is successful, or false if not */ public boolean add(T newEntry); /** Removes one unspecified entry from this bag, if possible. @return either the removed entry, if the removal was successful, or null */ public T remove(); /** Removes one occurrence of a given entry from this bag, if possible. @param anEntry the entry to be removed @return true if the removal was successful, or false if not */ public boolean remove(T anEntry); /** Removes all entries from this bag. */ public void clear(); /** Counts the number of times a given entry appears in this bag. @param anEntry the entry to be counted @return the number of times anEntry appears in the bag */ public int getFrequencyOf(T anEntry); /** Tests whether this bag contains a given entry. @param anEntry the entry to locate @return true if the bag contains anEntry, or false otherwise */ public boolean contains(T anEntry); /** Creates an array of all entries that are in this bag. @return a newly allocated array of all the entries in the bag */ public T[] toArray();} // end BagInterface
Define a class of bags that implements the interface BagInterface, as defined in Listing in Chapter 1. Use an instance of the class ArrayList to contain a bag’s entries. Then write a program that adequately demonstrates your new class. Note that you might have to handle exceptions thrown by methods of ArrayList.
LISTING A Java interface for a class of bags
/**An interface that describes the operations of a bag of objects.@author Frank M. Carrano*/public interface BagInterface{ /** Gets the current number of entries in this bag. @return the integer number of entries currently in the bag */ public int getCurrentSize(); /** Sees whether this bag is full. @return true if the bag is full, or false if not */ public boolean isFull(); /** Sees whether this bag is empty. @return true if the bag is empty, or false if not */ public boolean isEmpty(); /** Adds a new entry to this bag. @param newEntry the object to be added as a new entry @return true if the addition is successful, or false if not */ public boolean add(T newEntry); /** Removes one unspecified entry from this bag, if possible. @return either the removed entry, if the removal was successful, or null */ public T remove(); /** Removes one occurrence of a given entry from this bag, if possible. @param anEntry the entry to be removed @return true if the removal was successful, or false if not */ public boolean remove(T anEntry); /** Removes all entries from this bag. */ public void clear(); /** Counts the number of times a given entry appears in this bag. @param anEntry the entry to be counted @return the number of times anEntry appears in the bag */ public int getFrequencyOf(T anEntry); /** Tests whether this bag contains a given entry. @param anEntry the entry to locate @return true if the bag contains anEntry, or false otherwise */ public boolean contains(T anEntry); /** Creates an array of all entries that are in this bag. @return a newly allocated array of all the entries in the bag */ public T[] toArray();} // end BagInterface
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.