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 bag1 = new Bag();

Bag bag2 = new Bag();

Bag bag3 = new 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!