105k views
3 votes
Add the following methods to the ArrayCollection class, and create a test driver for each to show that they work correctly. In order to practice your array coding skills, code each of these methods by accessing the internal variables of the ArrayCollection, not by calling the previously defined public methods of the class.

String toString() creates and returns a string that correctly represents the current collection. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stored element already provides its own reasonable toString method.

int count(T target) returns a count of the number of elements e in the collection such that e.equals(target) is true.

void removeAll(T target) removes all elements e from the collection such that e.equals(target) is true.

ArrayCollection combine(ArrayCollection other) creates and returns a new ArrayCollection object that is a combination of this object and the argument object.

1 Answer

5 votes

Answer:

Attached are screenshots of the working code - baring in mind this only works on mutable Collection types. (ones that can be changed) if you use Collections that don't support this you may experience an Unsupported Operation Exception - but this is expected.

Step-by-step explanation:

Using Java streams as an alternative for some answers.

Add the following methods to the ArrayCollection class, and create a test driver for-example-1
Add the following methods to the ArrayCollection class, and create a test driver for-example-2
Add the following methods to the ArrayCollection class, and create a test driver for-example-3
Add the following methods to the ArrayCollection class, and create a test driver for-example-4
Add the following methods to the ArrayCollection class, and create a test driver for-example-5
User Samjewell
by
4.6k points