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

I think you're making it way more complicated than it needs to be. One, I see no eason at all to create any new arrays anywhere other than in the constructors. It seems to me that the "add" function would be nothing more than this.

public void add( int theValue)
{
    if(topPosition == 3)
    {
        // bag is full.  We cannot add.
        return; // do nothing
    }

    topPosition++;
    contents[topPosition] = theValue;
}

That's it!

There is a definite problem in "contains". It can only return true!

Thanks, this is the new piece of code that I came up with my only problem is the piece of code that I highlighted towards the end.

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) {
		// test if still room

		if (topPosition == 4) {
			// no more room
			return;
		}
		// OK still room
		topPosition++;
		contents[topPosition] = theValue;
	}

	// returns true if theValue is in the BoundedBag
	public boolean contains(int theValue) {
		for (int i = 0; i < topPosition; ++i) {
			if (contents[i] == theValue)
				return true;
		}
		// not found
		return false;
	}

	// deletes theValue from the BoundedBag
	// returns true if theValue was in the Bag,
	// otherwise return false.
	public boolean remove(int theValue) {

		for (int i = 0; i < topPosition; i++) {
			if (contents[i] == theValue) {
				// value found move back the other values
			}

			return true;
		}
		for (int j = i; j < topPosition; ++j) {
			if (contents[j] = contents[j + 1]) {				return false;
			}
		}
	}
}

What is the second for loop for? You want to check if the value is found in the contents, so once you make the check

if (contents[i] == theValue)

Then you should return true inside that. Then, at the end of your method, just return false(will mean the if statement wasn't entered, i.e. the value was not found)

for (int i = 0; i < topPosition; i++) {
			if (contents[i] == theValue) {
				// value found move back the other values
                              return true;
			}	
		}
return false;
		
	}

That's the code that I used for the contains method, would it be different for the remove method?

Well, the remove method should look like this no?
1. search array
2. if found, remove
3. return true
4. otherwise return false
The contains method should do exactly the same other than the remove shouldn't it?.

But to 'remove' the elements, you will need another for loop inside that if statement.

Forget the code for a second. Consider the process.

Here's my array before "remove".

  1. 5
  2. 8
  3. 3
  4. 9

topPosition is 3. I want to remove 8, so I need to end up with this:

  1. 5
  2. 3
  3. 9

topPosition is 2.

So I need to first find 8 by going through the loop. I find 8 at index 1.

So I need to "shift" my array. Everything after 8 goes up 1. Pseudocode below

int index = 1; // index is the index where I found 8.
for(int i = index; i < topPosition; i++)
{
    contents[i] = contents[i+1]; // shift up
}
// now adjust topPosition
topPosition--;

It's not really pseudocode actually, bu there's stuff for you to do still.

Ok I understand. Thank you

Would you by any chance be familiar with junit?

Here is my new piece of code

public boolean remove(int theValue) {

		// declare i
		for (int i = 0; i < topPosition; i++) {
			if (contents[i] == theValue) {
				// value found move back the other values

		for (int j = i; j < topPosition - 1; j++) {
			contents[j] = contents[j + 1];
		
		contents[topPosition-1] = null;
		break;
		}
	}
		else
		{
			return false;	
		}
		}
	}
}

Can this function ever return true? I don't see where it would. And does topPosition ever get changed?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.