Question: 2) .(10 points) Write a generic class called Bag. It should have three methods as follows: The add method should take in two generic values
2) .(10 points) Write a generic class called Bag. It should have three methods as follows: The add method should take in two generic values and save them into the theoretical bag, returning nothing from the method. The contains method should take the second type of generic parameter, and return whether or not that value is located in the theoretical bag. The isEmpty method should take no parameters and return true if the bag is empty, and false otherwise. For example, the class should support the following behavior:
Bag
Bag
Bag
bag1.add( 20, 92 );
bag2.add( 20, A- );
bag3.add( B", 84.5 );
// prints false
System.out.printf( Bag 1 is empty? %s , bag1.isEmpty() );
// prints false
System.out.printf( Found B in bag 2? %s , bag2.contains(B) );
// prints true
System.out.printf( Found 84.5 in bag 3? %s , bag3.contains(84.5) );
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
