Hi, I am having problems with this piece of code and maybe I am just not understanding how to do it, but can you please help me out or give me some tips. This is the description:
You will implement your BoundedBag as a partially filled array. The array contents holds the elements of the bag, but only the low order elements contain valid data - the remaining array elements are available for adding more elements. For example, with the array contents of size 4, a 2 element bag {31,17} would have contents[0] = 33 and contents[1] = 17 with array positions indexed by 2 and 3 being unused (the contents of unused array positions are irrelevant, there could be any value in them). We keep track of the highest used index in the integer variable topPosition. For {31, 17}, topPosition = 1. An empty bag is represented by topPosition = -1.
Here is the piece of code that I have:
public class BoundedBag {
// Data members
private int [] contents; // Elements of the BoundedBag.
private int topPosition; // Index of highest used position in contents.
/**
* Default constructor - creates an empty BoundedBag.
*/
public BoundedBag() {
contents = new int [4];
topPosition = -1;
}
public void add( int theValue) {
int contents = 4;
int[] inttopPosition = new int[contents];
for (int index = 0; index < contents; index ++)
{ inttopPosition[index] = index; }
}
// returns true if theValue is in the BoundedBag
public boolean contains(int theValue ) {
int contains = contents[4];
int indexSoFar = 0;
for (int index = 1; index < contents.length; index++) {
if (contents[index] > topPosition) {
topPosition = contents[index];
indexSoFar = index;
}
}
return true;
}
// deletes theValue from the BoundedBag
// returns true if theValue was in the Bag,
// otherwise return false.
public boolean remove(int theValue) {
boolean [] deleteItem = new boolean[contents.length];
int size=0;
for(int i=0;i<contents.length;i++){
if(contents[i].equals("theValue")){
deleteItem[i]=true;
}
else{
deleteItem[i]=false;
size++;
}
}
}
}
Thank you for your time