Question: Create generic collection of references to objects. --Java-- The class header will be defined with a type parameter T that is used for the type
Create generic collection of references to objects. --Java--
The class header will be defined with a type parameter T that is used for the type of the bag elements. The static union() method will be declared with its own generic parameter E.
1. Declare the generic Bag class with the interface clause necessary for the clone method 2. Declare a private field data such that it is an ArrayList object reference with the generic element type T. It is optional to keep or omit the manyItems field, it is not necessary for the ArrayList storage. 3. Add an accessor method (getter) for data 4. Define a constructor such that the field is initialized to the constructors parameter; 5. Define another constructor such that it takes a parameter initialCapacity and data is assigned an ArrayList instantiated to size intitialCapacity 6. add() : takes a T type element for parameter and adds that element to the bag.
Note that adding and removing elements means these same operations executed on the field data.
7. addAll( ) : takes a generic Bag reference for parameter; the method checks if the parameter bag is null or empty, if so the message Nothing was added to the bag is printed to the console and the method returns; otherwise iterate over the parameter and add all its elements to this bag, use an enhanced for loop. 8. addMany(): takes a generic vararg parameter; the method returns if the parameter is null or empty, otherwise runs an enhanced for loop on the parameter and adds its elements to this bag (ignore the compiler warning) 9. union( ): static; declares its own generic parameter E; the method takes two bag references for parameter, creates and returns a new bag that contains all the elements of both parameter bags. Use the constructor with the capacity parameter. 10. clone( ): goes by the template; in the try block once the bag is cloned you must also clone the field data 11. remove( ): returns a Boolean; takes a T type parameter and removes one copy of the parameter value from the bag by calling the remove method with respect to data with the same parameter 12. removeAll( ): returns a Boolean; takes a T type parameter and removes all copies of the parameter value occurring in the bag; calls the remove method with respect to data. Note that this time you cannot simply pass the parameter the removeAll method for data. The ArrayList class requires a Collection object for parameter when its removeAll( ) method is called. Therefore you have to create another ArrayList (a local variable in the method) named, say badBag such that badBag contains exactly as many copies of the target value is it occurs in data; countOccurrences( ) to be called, see below. Once badBag is loaded you can call removeAll( ) with respect to data and with BadBag as parameter. 13. countOccurrences(): takes a target element for parameter and returns the number of occurrences of target in the bag. Run an enhanced for loop for counting. 14. toString( ): the method runs an enhanced for loop concatenating the toString() return values called with respect to all the elements in the bag; the concatenated string is returned. Note: toString does not need a separate documented test since it will be called in the test of the other methods 15. size( ): calls size( ) of the ArrayList class (data). 16. trimToSize( ); calls trimToSize( ) of the ArrayList class (data).
17. Create an application class and test all methods! - Instantiate two objects of the AL_Bag class, both for String type. Accordingly, create two ArrayList objects and use them as parameters in the initializer constructor - Exercise the methods of the AL_Bag class and print the results to the console with short explanations. For printing the bag content use the toString method
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
