Question: Data structure with c++ class. Exercises Exercise 6: The union of two bags is a new bag containing the combined contents of the original two
Data structure with c++ class.
Exercises
Exercise 6: The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method unionWithBag for the ADT bag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the methods one argument. Include sufficient comments to fully specify the method. Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. Specifically, suppose that bag1 and bag2 are bags; bag1 contains the strings a, b, and c; and bag2 contains the strings b, b, d, and e. The expression bag1.unionWithBag(bag2) returns a bag containing the strings a, b, b, b, c, d, and e. Note that unionWithBag does not affect the contents of bag1 and bag2.
Exercise 7: The intersection of two bags is a new bag containing the entries that occur in both the original two bags. Design and specify a method intersectionWithBag for the ADT bag that returns as a new bag the intersection of the bag receiving the call to the method and the bag that is the methods one argument. Include sufficient comments to fully specify the method. Note that the intersection of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the intersection of these bags contains x two times. Specifically, suppose that bag1 and bag2 are bags; bag1 contains the strings a, b, and c; and bag2 contains the strings b, b, d, and e. The expression bag1.intersectionWithBag(bag2) returns a bag containing only the string b. Note that intersectionWithBag does not affect the contents of bag1 and bag2.
Exercise 8: The difference of two bags is a new bag containing the entries that would be left in one bag after removing those that also occur in the second. Design and specify a method differenceWithBag for the ADT bag that returns as a new bag the difference of the bag receiving the call to the method and the bag that is the methods one argument. Include sufficient comments to fully specify the method. 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 bags; bag1contains the strings a, b, and c; and bag2 contains the strings b, b, d, and e. The expression bag1.differenceWithBag(bag2) returns a bag containing only the strings a and c. Note that differenceWithBag does not affect the contents of bag1 and bag2.
Programming Problems
Programming Problem 6: Exercises 6, 7, and 8 ask you to specify methods for the ADT bag that return the union, intersection, and difference of two bags. Define each of these methods independently of the bags implementation by using only ADT bag operations.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
