Question: It is a coding problem with Java, This Project is about array and an abstract class Bag. The capacity of it is fixed, and we

It is a coding problem with Java, This Project is about array and an abstract class Bag. 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 insert part print nothing.

Here are the constructor and field part. I wrote the toString method.

It is a coding problem with Java, This Project is about array

and an abstract class Bag. The capacity of it is fixed, and

So, there is maybe something wrong with it.

And I am confused about the union method.

we need to put the new items into it. If the current

Can anyone help me with it?

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

Here is the test code.

Here is the detailed information:

item 6. The array should change to [2,3,4,5,6]. I complete the toString

part. However, my insert part print nothing. Here are the constructor and

And the main method.

Code in copy and paste:

import java.util.Iterator;

public class Bag implements LoopBag{

private int capacity;

private E[] items;

private int n; // number of elements in bag

/**

*

* @param capacity Fixed size bag capacity

*/

public Bag(int capacity){

this.capacity = capacity;

this.items = (E[])new Object[capacity];

}

@Override

public String toString() {

String str= "";

for(int i = 0; i

if(i == n - 1) {

str += items[i];

}else {

str += items[i];

str += ",";

}

}

return str;

}

public void insert(E item) {

// TODO Auto-generated method stub

E[] cur = (E[])new Object[capacity];

for(int i = 0; i

cur[i] = item;

}

if (n == capacity) {

for( int i = 1; i

cur[i - 1] = item;

}

for(int i = 0; i

cur[i] = items[i];

}

}

for(int i = 0; i

items[i] = item;

}

}

@Override

public void union(LoopBag lb) {

// TODO Auto-generated method stub

}

package loopbag;

import static org.junit.Assert.assertEquals;

import java.util.Iterator;

/**

Main

*/

public class Main {

public static void main(String[] args) {

LoopBag bag = new Bag(5);

LoopBag bag2 = new Bag(5);

for(int i = 1; i

bag.insert(i);

}

System.out.println(bag);

bag2.insert(5);

bag2.insert(6);

bag2.insert(7);

bag2.insert(8);

bag2.insert(1);

bag.union(bag2);

System.out.println(bag);

}

}

/TEST CODE/

public void testUnion1(){

LoopBag bag1 = new Bag(5);

LoopBag bag2 = new Bag(5);

bag1.insert(1);

bag1.insert(2);

bag2.insert(1);

bag2.insert(2);

bag1.union(bag2);

String answer="1,2";

assertEquals(answer, bag1.toString());

}

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!