Addall adjust the type of the parameter it is an arraybag


Problem: implement the method and constructor with added requirements

addAll( ) : adjust the type of the parameter, it is an ArrayBag with the same generic type parameter; the method checks if the parameter bag is null or the array field of the parameter is empty, in either case send a message "Nothing was added to the bag" to the console and return the method; otherwise, use the code deemed as wrong implementation on page 142: find a small modification to avoid the trap of an infinite loop as we discussed in class. Do not use the arraycopy method!

Here is my code so far...having issues with dealing with the inifinte loop, for example suppose we have a bag 'b' and activate it with b.addAll(b).

Then the private instance variable 'manyItems' is the same as addend.manyItems. Each iteration of the loop adds 1 to manyItems; so addend.manyItems is also increasing making an infinite loop

public void addAll(ArrayBag addend)
{

if (addend == null){
throw new IllegalArgumentException("The bag is empty.");
}
else{
int i;

ensureCapacity(manyItems + addend.manyItems);
for (i = 0; i < addend.manyItems; i++){
add(addend.data[i]);
}
}

code of ensure capacity method...probably irrelevant, but just to be sure...

public void ensureCapacity(int cap)
{
if(cap <= data.length)
return;
T[] longer = (T[]) new Object[cap];
System.arraycopy(data, 0, longer, 0, manyItems);
data = longer;

}

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: Addall adjust the type of the parameter it is an arraybag
Reference No:- TGS02898438

Expected delivery within 24 Hours