// File: BagWithReceipts.java from the package edu.colorado.collections // This is an assignment for students who are using // "Data Structures and Other Objects Using Java" by Michael Main. // Check with your instructor to see whether you should put this class in // a package. At the moment, it is declared as part of edu.colorado.collections: package edu.colorado.collections; import edu.colorado.nodes.Lister; // From Section 5.4 to return an iterator. /****************************************************************************** * This class is a homework assignment; * A BagWithReceipts is a collection of references to Objects. * Each time an Object is placed in the bag, an integer receipt is provided. * The receipt can later be used to retrieve the Object. * * Limitations: * (1) Beyond Int.MAX_VALUE elements, this bag no longer works * because of arithmetic overflow. * (2) Because of the slow linear algorithms of this class, large bags will have * poor performance. * * Note: * This file contains only blank implementations ("stubs") * because this is a Programming Project for my students. * My students implement this by storing the elements on a linked list and * using receipts 1, 2, 3... * * Outline of Java Source Code for this class: * * http://www.cs.colorado.edu/~main/edu/colorado/collections/BagWithReceipts.java * * * @version Feb 10, 2016 ******************************************************************************/ public class BagWithReceipts implements Cloneable { // The student's private instance variables are declared here: /** * Initialize an empty BagWithReceipts. * Postcondition: * This bag is empty. **/ public BagWithReceipts( ) { // Student implementation. } /** * Put a reference to an object into this bag. The new element may be the * null reference. * @param element * the element to be added to this bag * @return * The element has been added to this bag and the return value is an * integer called the "receipt." The receipt can later be used to retrieve * the element. * @exception OutOfMemoryError * Indicates insufficient memory for adding a new element. **/ public int add(Object element) { // Student implementation. return 0; } /** * Generate a copy of this BagWithReceipts. * @return * The return value is a copy of this BagWithReceipts. * Subsequent changes to the copy will not affect * the original, nor vice versa. Note that the return value must be * type cast to an BagWithReceipts before it can be used. * @exception OutOfMemoryError * Indicates insufficient memory for creating the clone. **/ public Object clone( ) { // Clone a BagWithReceipts object. // Student implementation. return null; } /** * Accessor method to count the number of occurrences of a particular element * in this BagWithReceipts. * @param target * an element to be counted * @return * The return value is the number of times that target occurs * in this bag. If target is non-null, then the occurrences * are found using the target.equals method. **/ public int countOccurrences(Object target) { // Student implementation. return 0; } /** * Create an Iterator containing the elements of this bag. * @return * an Iterator containing the elements of * this bag. * Note: * If changes are made to this bag before the Iterator * returns all of its elements, then the subsequent behavior of the * Iterator is unspecified. **/ public Lister iterator( ) { return null; } /** * Create an array containing all the receipts of elements of this bag. * @return * an array containing all the receipts elements of * this bag. **/ public int[] receipts( ) { return null; } /** * Remove one copy of a specified element from this BagWithReceipts. * @param target * an element to remove from this BagWithReceipts * @return * If target was found in this BagWithReceipts, * then one copy of * target has been removed and the method returns true. * Otherwise this BagWithReceipts remains unchanged * and the method returns false. * Note that if target is non-null, then * target.equals is used to find * target in the bag. **/ public boolean remove(Object target) { // Student implementation. return true; } /** * Remove an element with a specified receipt. * @param receipt * the receipt of * an element to remove from this BagWithReceipts * @return * If an element was found with the given receipt * then that element has been removed and the method returns true. * Otherwise this BagWithReceipts remains unchanged * and the method returns false. **/ public boolean remove_by_receipt(int receipt) { // Student implementation. return true; } /** * Get a copy of the element with the specified receipt. * @param receipt * the receipt of an element * Precondition: * using_receipt(receipt) * @return * the element with the specified key **/ public Object retrieve(int receipt) { // Student implementation. return null; } /** * Determine the number of elements in this bag. * @return * the number of elements in this bag **/ public int size( ) { // Student implementation. return 0; } /** * Determine whether a specified receipt is being used. * @param receipt * a possible receipt of an element * @return * If an element was found with the given receipt * then the method returns true. Otherwise the method returns false. **/ public boolean using_receipt(int receipt) { // Student implementation. return true; } }