Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

Students also viewed these Databases questions

Question

How can we visually describe our goals?

Answered: 1 week ago