Question: JAVA /** BagCQS.java defines a parameterized interface Bag for the Bag Abstract Data Type that conforms (with one exception) to the principle of Command-Query-Separation The
JAVA

/** BagCQS.java defines a parameterized interface Bag for the Bag Abstract Data Type that conforms (with one exception) to the principle of Command-Query-Separation
The Bag ADT is an unordered collection that allows for multiple occurrences of any item.
This interface is closely modeled on Frank Carrano's Bag interface in "Data Structures and Abstractions in Java" (3rd Edition)
Invariants:
if (isEmpty() == true) then getCount() == 0 else getCount() > 0
if (contains(item) == true) then getFrequencyOf(item) > 0 else getFrequencyOf(item) == 0
Creation constraints:
The constructor for any class the implements the Bag ADT must create an initially empty bag. So, immediately after contruction, isEmpty() == true AND getCount() == 0
/ interface BagWithCQS { /** * Adds the specified item to the bag *
* pre-condition: NOT isFull() *
* post-condition: * getFrequencyOf(item) == * getFrequencyOf(item)@pre + 1 * AND * getCount() == getCount()@pre + 1 *
* @param item -- the item to be added to the bag * */ public void add(T item); /** * Returns the total number of items in the bag. * * @return number of items in the bag (counting duplicates) */ public int getCount(); /** Removes all items from the bag
pre-condition: none (true)
post-condition: getCount() == 0 */ public void clear(); /** * Removes one occurrence of the specified item from the * bag, if there is one *
* pre-condition: EXERCISE * * post-condition: EXERCISE * * @param item - the item to be removed */ public void remove(T item); /** * Removes an occurrence of an unspecified item from the * bag and returns it. Returns null if the bag is empty *
* pre-condition: EXERCISE * * post-condition: EXERCISE * * @return removed item - note that this is a (minor) * violation of command query separation */ public T remove(); /** Checks whether the bag is empty
@return if getCount() == 0 true else false */ public boolean isEmpty(); /** Checks whether the bag is full (i.e., no item can be added), false otherwise
@return if bag is full then true else false */ public boolean isFull(); /** Checks whether the bag contains an occurrence of the specified item
@return if getFrequencyOf(item) > 0 then true else false */ public boolean contains (T item); /** Returns the number of occurrences of the specified item in the bag
@return the number of occurrences of item in the bag */ public int getFrequencyOf(T item); /** Returns an array of length getCount() containing the items in the bag. */ public T[] toArray(); } // interface Bag
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
