Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Java Language)A bag can contain more than one copy of an element. For example,

ID: 3886472 • Letter: #

Question

(Java Language)A bag can contain more than one copy of an element. For example, this 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.

In the zip file hw2.zip there is an abstract base class, IntSet.java and its Javadoc html file, IntSet.html. Also in the zip file there is a class IntArraySet.java that should implement IntSet.java. In this assignment you will complete the source file IntArraySet.java so that it implements the interface specified in IntSet.java, IntSet.html, and IntArraySet.html.

The IntSet abstract data type is very similar to the IntBag abstract data type (from Section 3.2 of the textbook), so quite a bit of the implementation of IntArraySet is similar to the equivalent implementation of IntArrayBag. The main difference between the two abstract data types is that a set can contain only one instance of any particular number, but a bag can contain an arbitrary number of copies of any particular number. Also, the set interface contains a few new methods that are not equivalent to any method from the bag interface. These new methods implement the mathematical operations of set union, set intersection, and set subtraction.

In the zip file hw2.zip there is a Java file, TestIntArraySet.java that you can use to test your implementation of IntArraySet.

Explanation / Answer

#ifndef SET_H #define SET_H #include class set { public: static const size_t CAPACITY = 30; typedef int Item; set(){used=0;} void insert(const Item& target); void remove(const Item& target); size_t size() const {return used;}; bool contains(const Item& target)const; void display(); private: Item data[CAPACITY]; size_t used; }; #endif