/shrug was bored so I figured what the hell. Here is your bag class.
Now 3 things to note:
1) This is my interpretation of your problem description
2) I did the record run time thing, but fuck counting each individual operation and assignment. You can do that yourself.
3) I haven't used Java in like 5 years, and I don't even have the SDK installed. In other words, I don't know if this compiles. I wrote it in Notepad.
Code
public class Bag<T>
{
private int currrentSize;
private T[] contents;
private long lastOperationTime;
//Initialize a new bag with a default capacity of 10
public Bag()
{
this(10);
}
//Initialize a new bag of a set capacity
public Bag(int capacity)
{
currentSize = 0;
contents = new T[capacity];
}
//Initialize a new bag from the contents of previous bag
public Bag(Bag<T> other)
{
this.currentSize = other.currentSize;
this.contents = new T[other.contents.length];
addAll(other.contents);
}
//Adds an item to the Bag
public void add(T item)
{
long currentTime = System.currentTimeMillis();
if(currentSize == contents.length)
throw new Exception("Maximum size reached");
contents[currentSize++] = item;
lastOperationTime = System.currentTimeMillis() - currentTime;
}
//Removes an item from the Bag
public void remove(T item)
{
long currentTime = System.currentTimeMillis();
int index = locateItem(item);
if(index != -1)
removeIndex(index);
lastOperationTime = System.currentTimeMillis() - currentTime;
}
//Removes a random item from the Bag
public void removeRandom()
{
long currentTime = System.currentTimeMillis();
removeIndex(Math.random() * currentSize);
lastOperationTime = System.currentTimeMillis() - currentTime;
}
//Determines if the Bag contains an item
public boolean contains(T item)
{
long currentTime = System.currentTimeMillis();
boolean found = locateItem(item) != -1;
lastOperationTime = System.currentTimeMillis() - currentTime;
return found;
}
//Determines if the Bag is empty
public boolean isEmpty()
{
long currentTime = System.currentTimeMillis();
bool empty = currentSize == 0;
lastOperationTime = System.currentTimeMillis() - currentTime;
return empty;
}
//Returns the current size of the Bag
public int size()
{
long currentTime = System.currentTimeMillis();
//silly?
lastOperationTime = System.currentTimeMillis() - currentTime;
return currentSize;
}
//Adds all items to the Bag
public void addAll(T[] items)
{
long currentTime = System.currentTimeMillis();
if(items.length + currentSize > contents.length)
throw new Exception("Additional items exceed capacity");
for(int i = 0; i < items.length; i++)
{
contents[currentSize++] = items[i];
}
lastOperationTime = System.currentTimeMillis() - currentTime;
}
//Creates a Bag that is a Union of the two
public Bag<T> union(Bag<T> other)
{
long currentTime = System.currentTimeMillis();
Bag<T> union;
int size;
size = this.contents.length > other.contents.length ? this.contents.length : other.contents.length;
union = new Bag<T>(size);
union.addDistinct(this);
union.addDistinct(other);
lastOperationTime = System.currentTimeMillis() - currentTime;
return union;
}
//Determines if the contents of this Bag equals the contents of another
public boolean equals(Bag<T> other)
{
long currentTime = System.currentTimeMillis();
boolean same = true;
for(int i = 0; same && i < other.size(); i++)
{
same = contains(other.contents[i]);
}
for(int i = 0; same && i < size(); i++)
{
same = other.contains(contents[i]);
}
lastOperationTime = System.currentTimeMillis() - currentTime;
return same;
}
public long getLastOperationTime()
{
return lastOperationTime;
}
//Locates the specified item
private int locateItem(T item)
{
bool found = false;
int index;
for(index = currentSize-1; !found && index >= 0; index--)
found = contents[index] == item;
return index;
}
//Adds only the items that it doesn't already have
private void addDistinct(Bag<T> other)
{
for(int i = 0; i < other.size(); i++)
if(!contains(other.contents[i]))
add(items[i]);
}
//Removes an item at the specified index
private void removeIndex(int index)
{
for(int i = index; i < currentSize-1; i++)
{
contents[i] = contents[i+1];
}
currentSize--;
}
}
This post was edited by Minkomonster on Feb 3 2014 01:29am