edu.colorado.collections
Class BagWithReceipts

java.lang.Object
  extended by edu.colorado.collections.BagWithReceipts
All Implemented Interfaces:
java.lang.Cloneable

public class BagWithReceipts
extends java.lang.Object
implements java.lang.Cloneable

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


Constructor Summary
BagWithReceipts()
          Initialize an empty BagWithReceipts.
 
Method Summary
 int add(java.lang.Object element)
          Put a reference to an object into this bag.
 java.lang.Object clone()
          Generate a copy of this BagWithReceipts.
 int countOccurrences(java.lang.Object target)
          Accessor method to count the number of occurrences of a particular element in this BagWithReceipts.
 Lister iterator()
          Create an Iterator containing the elements of this bag.
 int[] receipts()
          Create an array containing all the receipts of elements of this bag.
 boolean remove_by_receipt(int receipt)
          Remove an element with a specified receipt.
 boolean remove(java.lang.Object target)
          Remove one copy of a specified element from this BagWithReceipts.
 java.lang.Object retrieve(int receipt)
          Get a copy of the element with the specified receipt.
 int size()
          Determine the number of elements in this bag.
 boolean using_receipt(int receipt)
          Determine whether a specified receipt is being used.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BagWithReceipts

public BagWithReceipts()
Initialize an empty BagWithReceipts.

Parameters:
- - none
Postcondition:
This bag is empty.
Method Detail

add

public int add(java.lang.Object element)
Put a reference to an object into this bag. The new element may be the null reference.

Parameters:
element - the element to be added to this bag
Returns:
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.
Throws:
java.lang.OutOfMemoryError - Indicates insufficient memory for adding a new element.

clone

public java.lang.Object clone()
Generate a copy of this BagWithReceipts.

Overrides:
clone in class java.lang.Object
Parameters:
- - none
Returns:
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.
Throws:
java.lang.OutOfMemoryError - Indicates insufficient memory for creating the clone.

countOccurrences

public int countOccurrences(java.lang.Object target)
Accessor method to count the number of occurrences of a particular element in this BagWithReceipts.

Parameters:
target - an element to be counted
Returns:
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.

iterator

public Lister iterator()
Create an Iterator containing the elements of this bag.

Parameters:
- - none
Returns:
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.

receipts

public int[] receipts()
Create an array containing all the receipts of elements of this bag.

Parameters:
- - none
Returns:
an array containing all the receipts elements of this bag.

remove

public boolean remove(java.lang.Object target)
Remove one copy of a specified element from this BagWithReceipts.

Parameters:
target - an element to remove from this BagWithReceipts
Postcondition:
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.

remove_by_receipt

public boolean remove_by_receipt(int receipt)
Remove an element with a specified receipt.

Parameters:
receipt - the receipt of an element to remove from this BagWithReceipts
Postcondition:
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.

retrieve

public java.lang.Object retrieve(int receipt)
Get a copy of the element with the specified receipt.

Parameters:
receipt - the receipt of an element
Precondition:
using_receipt(receipt)
Returns:
the element with the specified key

size

public int size()
Determine the number of elements in this bag.

Parameters:
- - none
Returns:
the number of elements in this bag

using_receipt

public boolean using_receipt(int receipt)
Determine whether a specified receipt is being used.

Parameters:
receipt - a possible receipt of an element
Returns:
If an element was found with the given receipt then the method returns true. Otherwise the method returns false.