Question: I have this code for ArrayBag, I need to do the last part of my hw which asks for : 4. Create an application class

I have this code for ArrayBag,

I need to do the last part of my hw which asks for :

4. Create an application class and test all methods!

(10pts)Instantiate two instances of the ArrayBag class, both for String type. Apply the constructor which takes a string array to initialize the array field.

(20pts)Exercise the methods and print the results to the console with short explanations. For printing the bag content use the toString method you had to add to the class.

Documented tests are necessary to validate your scores for the ArrayBag implementation

The code:

//ArrayBag.java

import java.util.Arrays; public class ArrayBag  implements Cloneable{ private T[] data;//elements are stored in data[]  private int numberOfElements;//number of elements in bag  public ArrayBag()//default constructor  { final int CAPACITY =1; numberOfElements=0; data= (T[]) new Object[CAPACITY]; } //Overload constructor  public ArrayBag(int initialCapacity, T[] arr) throws IllegalAccessException { if(initialCapacity<0) throw new IllegalAccessException("The initial capacity is negative: "+initialCapacity); data= Arrays.copyOf(arr,arr.length); numberOfElements=arr.length; } //getter methods  public int getNumberOfElements() { return numberOfElements; } public T[] getData() { return data; } //setter methods  public void setData(T[] data) { this.data = data; } public void setNumberOfElements(int numberOfElements) { this.numberOfElements = numberOfElements; } //add elements to arrayBag public void add(T element) { if(numberOfElements == data.length) { ensureCapacity((numberOfElements+1)*2); } data[numberOfElements]=element; numberOfElements++; } public ArrayBag clone() { ArrayBag cloneArray=new ArrayBag<>(); try  { cloneArray= (ArrayBag) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } cloneArray.data=data.clone(); return cloneArray; } //Remove element from array bag public boolean remove(T element) { int location; if(element==null) { location=0; while((location<numberOfElements)&&(data[location]!=null)) location++; } else { location=0; while ((location<numberOfElements)&&(!element.equals(data[location]))) location++; } if(location==numberOfElements) return false; else { numberOfElements--; data[location]=data[numberOfElements]; data[numberOfElements]=null; return true; } } public void addAll(ArrayBag add) { ensureCapacity(numberOfElements+add.numberOfElements); System.arraycopy(add.data,0,data,numberOfElements,add.numberOfElements); numberOfElements+=add.numberOfElements; } public void ensureCapacity(int cap) { if(cap<=data.length) return; T[] longer = (T[]) new Object[cap]; System.arraycopy(data,0,longer,0,numberOfElements); data = longer; } //toString() method to print the object  @Override public String toString() { String result=""; for (int i = 0; i <numberOfElements ; i++) { result+= data[i]+" "; } return result; } } 

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!