The difference of two collections is a new collection of the entries that would be left in one collection after removing those that also occur in the second. Define a method difference for the class...


The difference of two collections is a new collection of the entries that would be left in one collection after removing those that also occur in the second. Define a method difference for the class BagOfStrings that returns as a new bag the difference of the bag receiving the call to the method and the bag that is the method’s one argument. Note that the difference of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the difference of these bags contains x three times. Specifically, suppose that bag1 and bag2 are BagOfStrings objects; bag1 contains the String objects a, b, and c; and bag2 contains the String objects b, b, d, and e. After the statement BagOfStrings leftOver1 = bag1.difference(bag2); executes, the bag leftOver1 contains the strings a and c. After the statement BagOfStrings leftOver2 = bag2.difference(bag1); executes, the bag leftOver2 contains the strings b, d, and e. Note that difference does not affect the contents of bag1 and bag2.



May 18, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here