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

Project Write a class called IntSet, which represents a mathematical set of inte

ID: 3765739 • Letter: P

Question

Project Write a class called IntSet, which represents a mathematical set of integers. It should have the following

data members:

a pointer-to-int that will point to a dynamically allocated array that holds the values currently in the IntSet

an int that holds the current size of the array - it will need to be updated whenever the add() method creates a larger array

an int that holds the number of values currently in the IntSet - it will need to be updated in the add() and remove() methods

The IntSet class should have the following methods:

a default constructor that initializes the first data member to point to an array of 10 ints, initializes the size of the array to 10, and initializes the number of values in the set to zero

a destructor that deallocates the dynamically allocated array

the size() method should return the number of values currently in the IntSet

the isEmpty() method should return true if the IntSet contains no integers, and return false otherwise

the contains() method should take an int parameter and return true if that value is in the IntSet, and return false otherwise - you will find this function useful

the add() method should take an int parameter and add that int to the IntSet (if that value is not already in the IntSet) - if the array is currently full and you need to add another int, then you must first increase the size of the array by allocating a new array that is twice as large, copying the contents of the old array into the new array, redirecting the data member pointer to the new array, and deallocating the old array

the remove() method should take an int parameter and remove it from the IntSet (if that value is in the IntSet) by shifting over all of the subsequent elements of the array

the addAll() method: setA.addAll(setB) should add to setA any values from setB that were not already in setA (i.e. setA becomes the union of the two sets) - the parameter should be a const reference

the removeDifferent() method: setA.removeDifferent(setB) should remove from setA any values that are not also in setB (i.e. setA becomes the intersection of the two sets) - the parameter should be a const reference

the removeSame() method: setA.removeSame(setB) should remove from setA any values that are also in setB (i.e. setA becomes the relative complement of setA in setB) - the parameter should be a const reference

Note: When designing a class that has dynamically allocated data members, you would normally overload the copy constructor, the assignment operator, and possibly the equality operator (you'll see these in CS 162). The copy constructor for a class is invoked any time you:

pass an object of that class by value

return an object of that class from a function

declare an object of that type and assign a value to it in the same line (if you assign a value on a different line then the assignment operator is invoked)

Explanation / Answer

using System;
using System.Collections;
public class
IntSet
{
private int capacity;
private static int DefaultCapacity = 10;
private
int[]
store;
private int next;
public
IntSet
(int capacity){
    this.capacity = capacity;
    store = new
int[capacity]
;
    next = 0;
}
public
IntSet
(): this(DefaultCapacity){
}
public
IntSet
(
int[]
elements): this(elements.Length){
    foreach(
int
el in elements) this.Insert(el);
}
// Copy constructor
public
IntSet
(
IntSet
s): this(s.capacity){
    foreach(
int
el in s) this.Insert(el);
}
public bool Member(
int
element){
    for(int idx = 0; idx < next; idx++)
      if (element.Equals(store[idx]))
        return true;
    return false;
}
public void Insert(
int
element){
    if (!this.Member(element)){
      if (this.Full){
        Console.WriteLine("[Resize to {0}]", capaci
ty * 2);
        Array.Resize
<int>
(ref store, capacity * 2);
        capacity = capacity * 2;
      }
      store[next] = element;
      next++;
    }
}
public void Delete(
int
element){
    bool found = false;
    int foundIdx = 0;
    for(int idx = 0; !found && (idx < next); idx++)
{
      if (element.Equals(store[idx])){
         found = true;
         foundIdx = idx;
      }
    }
    if (found){   // shift remaining elements left
      for(int idx = foundIdx+1; idx < next; idx++)
        store[idx-1] = store[idx];
      store[next-1] = default(
int
);
      next--;
    }