I have this code for ArrayBag, I need to do the last part of my hw which asks fo
ID: 3889244 • Letter: I
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 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
Explanation / Answer
Test.java
public class Test {
public static void main(String[] args) throws IllegalAccessException {
//Creating an Array
String arr1[] = {
"Pen",
"Pencil",
"Eraser",
"Book",
"Bag",
"Brush",
"Mobile",
"Can",
"Bottle",
"Mat"
};
//Adding that Array to the ArrayBag#1
ArrayBag < String > ab1 = new ArrayBag < String > (20, arr1);
//Creating an Array
String arr2[] = {
"Lion",
"Peacock",
"Hen",
"Duck",
"Crocodile",
"Rabbit",
"Ant",
"Beetle",
"Rat",
"Bat"
};
//Adding that Array to the ArrayBag#2
ArrayBag < String > ab2 = new ArrayBag < String > (20, arr2);
//Displaying the no of Elements ArrayBag#1 and ArrayBag#2
System.out.println("No of Elements in the ArrayBag#1 is :" + ab1.getNumberOfElements());
System.out.println(" No of Elements in the ArrayBag#2 is :" + ab2.getNumberOfElements());
//Adding two elements to the ArrayBag#1
System.out.println("Adding An element to the ArrayBag#1 ::");
ab1.add("Ball");
ab1.add("Chair");
//Displaying the no of Elements ArrayBag#1
System.out.println(" After adding No of Elements in the ArrayBag#1 is :" + ab1.getNumberOfElements());
//Adding two elements to the ArrayBag#2
System.out.println("Adding An element to the ArrayBag#2 ::");
ab2.add("Butterfly");
ab2.add("Horse");
//Displaying the no of Elements ArrayBag#2
System.out.println(" After adding No of Elements in the ArrayBag#2 is :" + ab2.getNumberOfElements());
//Cloning the ArrayBag#1
ArrayBag < String > ab3 = ab1.clone();
//Displaying the no of Elements ArrayBag#3
System.out.println(" No of Elements in the ArrayBag#3 is :" + ab3.getNumberOfElements());
//Removing the element From the ArrayBag#1
boolean bool1 = ab1.remove("Book");
if (bool1) {
System.out.println(" " Book" element removed from the ArrayBag#1");
}
//Displaying the no of Elements ArrayBag#1 after removing an element
System.out.println(" No of Elements in the ArrayBag#1 is :" + ab1.getNumberOfElements());
//Adding the ArrayBag#1 to the ArrayBag#2
ab2.addAll(ab1);
//Displaying the no of Elements in ArrayBag#2
System.out.println(" No of Elements in the ArrayBag#2 is :" + ab2.getNumberOfElements());
//Displaying the Elements in ArrayBag#1
System.out.println(" Dispalying the elements in the ArrayBag#1 :");
System.out.println(ab1.toString());
//Displaying the Elements in ArrayBag#2
System.out.println(" Dispalying the elements in the ArrayBag#2 :");
System.out.println(ab2.toString());
//Displaying the Elements in ArrayBag#3
System.out.println(" Dispalying the elements in the ArrayBag#3 :");
System.out.println(ab3.toString());
}
}
_____________________
Output:
No of Elements in the ArrayBag#1 is :10
No of Elements in the ArrayBag#2 is :10
Adding An element to the ArrayBag#1 ::
After adding No of Elements in the ArrayBag#1 is :12
Adding An element to the ArrayBag#2 ::
After adding No of Elements in the ArrayBag#2 is :12
No of Elements in the ArrayBag#3 is :12
" Book" element removed from the ArrayBag#1
No of Elements in the ArrayBag#1 is :11
No of Elements in the ArrayBag#2 is :23
Dispalying the elements in the ArrayBag#1 :
Pen
Pencil
Eraser
Chair
Bag
Brush
Mobile
Can
Bottle
Mat
Ball
Dispalying the elements in the ArrayBag#2 :
Lion
Peacock
Hen
Duck
Crocodile
Rabbit
Ant
Beetle
Rat
Bat
Butterfly
Horse
Pen
Pencil
Eraser
Chair
Bag
Brush
Mobile
Can
Bottle
Mat
Ball
Dispalying the elements in the ArrayBag#3 :
Pen
Pencil
Eraser
Book
Bag
Brush
Mobile
Can
Bottle
Mat
Ball
Chair
_____________Could you rate me well.Plz .Thank You