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

INSTRUCTIONS: (Generic ArrayBag class) In this assignment your are to implement

ID: 3784197 • Letter: I

Question

INSTRUCTIONS: (Generic ArrayBag class)

In this assignment your are to implement ArrayBag as a generic class with a parameter T for the elements in the bag

1) keep two private data fields, each with the properly seleced type

2) Add a getter and setter method for the array, as well as getter and setter method for the array elements of index k

here is my code so far....

public class ArrayBag<T> implements Cloneable
{
private Object[ ] data;
private int manyItems;

public <T>Object getArrayBag(){ //need help here, do not know if correct, researched textbook and interent
data = (T[]) new Object[manyItems];
return data;
}

public void setArrayBag(Object arr){ //Do not know how to return the array without errors

}

public getArrElements(){ //Very stuck with get and set methods for array elements of index k

}

public setArray(){

}

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

public class ArrayBag<T> implements Cloneable

{

   // instance variables

   private T[] data;

   private int manyItems;

  

   // parameterized constructor, that initializes data array and size

   public ArrayBag(int size) {

       manyItems = size;

       data = (T []) new Object[manyItems];

   }

   // getter for array

   public T[] getArrayBag(){

       return data;

   }

   // setter for array

   public void setArrayBag(T[] arr){

       manyItems = arr.length;

       data = (T []) new Object[manyItems];

       for(int i=0; i<arr.length; i++){

           data[i] = arr[i];

       }

   }

  

   // getter for an element at index k

   public T getArrElement(int k){

       return data[k];

   }

  

   // setter for an element at index k

   public void setArrayElement(T e, int k){

       data[k] = e;

   }

}

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