A bag can contain more than one copy of an element. For example, this chapter describes a
Question:
A bag can contain more than one copy of an element. For example, this chapter describes a bag that contains the number 4 and two copies of the number 8. This bag behavior is different from a set, which can contain only a single copy of any given element. Write a new collection class called IntArraySet, which is similar to a bag except that a set can contain only one copy of any given element. You’ll need to change the specification a bit. For example, instead of the bag’s countOccurrences method, you’ll want a method such as this: boolean contains(int target)
// Postcondition: The return value is true if
// target is in the set; otherwise, the return
// value is false.
Make an explicit statement of the invariant of the set ADT. Do a time analysis for each operation. At this point, an efficient implementation is not needed. For example, just adding a new element to a set will take linear time because you’ll need to check that the new element isn’t already present. Later we’ll explore more efficient implementations.
You may also want to add additional methods to your set ADT, such as a method for subtracting one set from another.
Step by Step Answer: