public class BagWithReceipts
extends java.lang.Object
implements java.lang.Cloneable
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 and Description |
|---|
BagWithReceipts()
Initialize an empty
BagWithReceipts. |
| Modifier and Type | Method and Description |
|---|---|
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.
|
public BagWithReceipts()
BagWithReceipts.
Postcondition:
This bag is empty.public int add(java.lang.Object element)
element - the element to be added to this bagjava.lang.OutOfMemoryError - Indicates insufficient memory for adding a new element.public java.lang.Object clone()
BagWithReceipts.clone in class java.lang.ObjectBagWithReceipts.
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.java.lang.OutOfMemoryError - Indicates insufficient memory for creating the clone.public int countOccurrences(java.lang.Object target)
BagWithReceipts.target - an element to be countedtarget occurs
in this bag. If target is non-null, then the occurrences
are found using the target.equals method.public Lister iterator()
Iterator containing the elements of this bag.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 int[] receipts()
public boolean remove(java.lang.Object target)
BagWithReceipts.target - an element to remove from this BagWithReceiptstarget 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_by_receipt(int receipt)
receipt - the receipt of
an element to remove from this BagWithReceiptsreceipt
then that element has been removed and the method returns true.
Otherwise this BagWithReceipts remains unchanged
and the method returns false.public java.lang.Object retrieve(int receipt)
receipt - the receipt of an element
Precondition:
using_receipt(receipt)public int size()
public boolean using_receipt(int receipt)
receipt - a possible receipt of an elementreceipt
then the method returns true. Otherwise the method returns false.