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

It is a coding problem with Java, This Project is about array and an abstract cl

ID: 3909325 • Letter: I

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 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.

So, there is maybe something wrong with it.

And I am confused about the union method.

Can anyone help me with it?

Here is the test code.

Here is the detailed information:

And the main method.

Code in copy and paste:

import java.util.Iterator;

public class Bag<E> implements LoopBag<E>{

   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 < n-1; 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 < capacity; i++) {

           cur[i] = item;

       }

       if (n == capacity) {

           for( int i = 1; i < capacity; i++ ) {

               cur[i - 1] = item;

           }

           for(int i = 0; i < capacity; i++) {

               cur[i] = items[i];

           }

       }

       for(int i = 0; i < n; i ++) {

           items[i] = item;

       }

   }

@Override

   public void union(LoopBag<E> 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<Integer> bag = new Bag(5);  

LoopBag<Integer> bag2 = new Bag(5);  

for(int i = 1; i <= 7; 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<Integer> bag1 = new Bag(5);

       LoopBag<Integer> 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

Explanation / Answer


I have completed the insert() and toString() in Bag. To complete union() , the LoopBag interface methods are needed.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


package loopbag;
import java.util.Iterator;

public class Bag<E> implements LoopBag<E>{

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];
n = 0;

}

@Override

public String toString() {

String str= "";
if(n > 0){
str += items[0];
for(int i = 1; i < n; i++)
str += ", " + items[i];
}
return str;

}
@Override
public void insert(E item) {
//check if full
if(n == capacity){
//shift all starting from index 1 to one position back
for(int i = 1; i < n; i++)
items[i-1] = items[i];
n--; //reduce count;
}

items[n] = item;
n++;
}

@Override

public void union(LoopBag<E> lb) {

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote