d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Bag Implementation > Java Work For Fg
Add Reply New Topic New Poll
Member
Posts: 3,379
Joined: Sep 2 2006
Gold: 16,244.70
Jan 28 2014 02:03pm
Hey guys I am fairly new to java and am having some issues going about this.. I am trying to use arrays to achieve bag implementation that does all the basic operations.
Some main points to note...
1. I want to have the following basic operations for my bags: add, remove, removeRandom, contains, isEmpty, size, addAll, union, equals.
2. I would like to keep track of complexities for these above operations. I.e. using a counter to record # of comparisons, assignments, operations.
3. I would like 2 different bag sizes (a small one (~10) and a big one (~100,000))
4. I would like for all of this to be possible through the console.
*. Not absolutely needed, however I would like the program to record running times it takes for the machine to complete operations.

Paying 500fg for this to be completed. If you have any further questions, feel free to PM or post here!



Member
Posts: 1,208
Joined: Aug 1 2013
Gold: 50.00
Feb 2 2014 10:04pm
Email me more details on the class and methods, up the payout a bit to make it worth my hours, and assure me that this isn't a graded school assignment then I'll do this.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 3 2014 01:25am
/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
Member
Posts: 2,095
Joined: Apr 15 2003
Gold: 100.08
Feb 13 2014 01:37pm
This would not work out, as you can not initizalize generic arrays in java directly.
The basic constructor would have to look like this:
Code

public Bag(Class<T> clazz, int size) {
contents = (T[]) Array.newInstance(clazz, size);
}


Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 13 2014 02:17pm
Well, like I said. I haven't used Java in a couple years. And I didn't have a way to even compile the above code. Looks like C# has sunk its hooks in me. Now that you bring that up, I recall Java Generics being implemented with Type Erasure. Looking back at the code, I can see there is a bug with the constructor that clones another Bag as well.

This post was edited by Minkomonster on Feb 13 2014 02:21pm
Member
Posts: 3,415
Joined: Jun 17 2006
Gold: 38,177.00
Mar 7 2014 03:01am
ok so here's a bit of advice on making a list of contents :
if you want a bag of items, first decide if you want a set of objects (only one instance of each object) or a list (more than one)
list => 2 lists possible : LinkedList (for a lot of deleting/adding) or ArrayList (for easy access to the objects)
set => 2 again : TreeSet (ordered objects, you'll need a comparator) or HashSet (non-ordered, it will be faster)

pm me for more info
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll