Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am confused about my current project. It is about the array. The capacity of i

ID: 3909320 • Letter: I

Question

I am confused about my current project. It is about the array. The capacity of it is fixed, and we need to put the new items into it. If the current number of items larger than the capacity, for example, the capacity is 5, and the current array is [1,2,3,4,5], we want to put new item 6. The array should change to [2,3,4,5,6]. I complete the toString part.

However, my code print nothing.

Here is my insert part.

It is the test:

My result:

Another test:

3 import java.util.Iterator; 4 5 public class Bag implements LoopBag 6 7 the capacity of bag private int capacity, private EL 1tems; private int n; // number of elements in bag 10 12e 13 14 15 16e 17 18 19 20 21 22e 23 24 25 26 27 28 29 30 31 32 *@param capacity Fixed size bag capacity public Bag(int capacity)i this.capacitycapacity; this. items = CEDnew 0bject [capacity]'; @Override public String toStringO String str- ""; for(int i - 0; i

Explanation / Answer

Do the Changes I am Suggesting , You should have given the Code . Anyways


=> public Bag(int capacity) {
n = 0 ; //Add this LINE
this.capacity = capacity;
this.items = (E[ ]) new Object[capacity];
}


//Remove all the contents Inside whatever you wrote and Just Copy the contents and paste it

=> public void insert(E item) {

if(n >=capacity ) {

for(int i=1 ; i<capacity ; ++i)
items[i-1] = items[i];

  items[n-1] = item;

}else{
   items[n] = item;
n = n + 1;
}
}



//Change the LOOP to i<n


public String toString() {

for(int i=0 ;i<n; ++i) //Change it to i<n

}

Let me know if it works, I will make sure it works. Comment and let me know