Create an application class and test all methods! Instantiate two instances of t
ID: 3784284 • Letter: C
Question
Create an application class and test all methods!
Instantiate two instances of the ArrayBag class, both for String type. Apply the constructor which takes a string array to initialize the array field.
here are my methods from the ArrayBag class that are to be used...
private T[] data;
private int 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];
}
}
public ArrayBag(T[] initialCapacity)
{
if (initialCapacity == null)
throw new IllegalArgumentException
("The initialCapacity is null");
data = (T []) new Object[manyItems];
manyItems = 0;
}
I have an idea about how to instantiate them in the application class and this was the implementation i was using...
ArrayBag<String> a = new ArrayBag<String>(s_array); //s_array is a string array
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Arrays;
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];
}
public ArrayBag(T[] initialCapacity)
{
if (initialCapacity == null)
throw new IllegalArgumentException();
manyItems = initialCapacity.length;
data = (T []) new Object[manyItems];
for(int i=0; i<initialCapacity.length; i++){
data[i] = initialCapacity[i];
}
}
// getter for array
public T[] getArrayBag(){
return data;
}
// setter for array
public void setArrayBag(T[] arr){
if (arr == null)
throw new IllegalArgumentException();
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;
}
public static void main(String[] args) {
// creating two string array
String[] strArr1 = {
"Apple",
"Banana",
"Potato",
"Tomato",
"Carrot"
};
String[] strArr2 = {
"Alex",
"Bob",
"Mukesh",
"Pardeep",
"Ramesh"
};
// creating two objects of ArrayBag
ArrayBag<String> bag1 = new ArrayBag<String>(strArr1);
System.out.println("Bag1 : "+Arrays.toString(bag1.getArrayBag()));
System.out.println("Element at index 4: "+bag1.getArrElement(4));
ArrayBag<String> bag2 = new ArrayBag<>(strArr2);
System.out.println("Bag1 : "+Arrays.toString(bag1.getArrayBag()));
System.out.println("Element at index 3: "+bag1.getArrElement(3));
}
}
/*
Sample run:
Bag1 : [Apple, Banana, Potato, Tomato, Carrot]
Element at index 4: Carrot
Bag1 : [Apple, Banana, Potato, Tomato, Carrot]
Element at index 3: Tomato
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.