Class IntBag

java.lang.Object
  |
  +--IntBag

public class IntBag
extends java.lang.Object

Stores a bounded collection of integers. Duplicates are allowed.

Version:
1.1 10-SEP-2003 Modified by Jack the Hacker to allow duplicate entries and basic iteration
Author:
Paul Strooper, Jack the Hacker, Ned Martin (40529927)

Field Summary
private  int count
           
private  int current
           
private  int[] elements
           
static int MAX_SIZE
          Maximum size of the set.
 
Constructor Summary
IntBag()
          Creates an empty collection.
 
Method Summary
private  int findPosition(int x)
          Find the position of x in the first count elements of the array elements.
 int getNext()
          Gets next integer from collection
 boolean hasNext()
          Checks if collection has another element
 void insert(int x)
          Insert the integer x into the collection.
 boolean isMember(int x)
          Determine whether or not the integer x is in the collection.
 int numberOfOccurrences(int x)
          Determines number of times integer x is found in the collection
 void remove(int x)
          Remove the first found integer x from the collection.
 void removeAllOccurrences(int x)
          Removes all occurences of integer x from the collection.
private  boolean removeOne(int x)
          Removes integer x from collection
 int size()
          Determine the size of the collection.
 void start()
          Resets current to zero
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

MAX_SIZE

public static final int MAX_SIZE
Maximum size of the set.

elements

private int[] elements

count

private int count

current

private int current
Constructor Detail

IntBag

public IntBag()
Creates an empty collection.
Method Detail

insert

public void insert(int x)
            throws FullExc
Insert the integer x into the collection.
Parameters:
x - the integer to be added
Throws:
FullExc - there are MAX_SIZE elements in the set

remove

public void remove(int x)
            throws NotFoundExc
Remove the first found integer x from the collection.
Parameters:
x - the integer to be removed
Throws:
NotFoundExc - x is not in the collection

removeAllOccurrences

public void removeAllOccurrences(int x)
                          throws NotFoundExc
Removes all occurences of integer x from the collection.
Parameters:
x - the integer to be removed
Throws:
NotFoundExc - x is not in the collection

isMember

public boolean isMember(int x)
Determine whether or not the integer x is in the collection.
Parameters:
x - the integer to be checked
Returns:
true if and only if x is in the set

size

public int size()
Determine the size of the collection.
Returns:
the size of the collection

numberOfOccurrences

public int numberOfOccurrences(int x)
                        throws NotFoundExc
Determines number of times integer x is found in the collection
Parameters:
x - the integer to find
Returns:
number of times element found in collection
Throws:
NotFoundExc - integer x not found in collection

start

public void start()
Resets current to zero

hasNext

public boolean hasNext()
                throws UnstableIteratorExc
Checks if collection has another element
Returns:
true when collection has next element
Throws:
UnstableIteratorExc - Iterator out of bounds

getNext

public int getNext()
            throws UnstableIteratorExc
Gets next integer from collection
Returns:
next integer from collection
Throws:
UnstableIteratorExc - Iterator out of bounds

findPosition

private int findPosition(int x)
Find the position of x in the first count elements of the array elements.
Returns:
first position element found in

removeOne

private boolean removeOne(int x)
Removes integer x from collection
Returns:
true when integer removed, false otherwise