Here is what I got so far I dont know how to write union int
Here is what I got so far, I don\'t know how to write union, intersection, and minus. Please complete this code in Java.
do not use iterator
public class IntArraySet extends IntSet
{
private int[ ] data;
private int manyItems;
public IntArraySet( )
{
final int INITIAL_CAPACITY = 10;
manyItems = 0;
data = new int[INITIAL_CAPACITY];
}
public IntArraySet(int initialCapacity)
{
if (initialCapacity < 0)
throw new IllegalArgumentException
(\"initialCapacity is negative: \" + initialCapacity);
data = new int[initialCapacity];
manyItems = 0;
}
public void ensureCapacity(int minimumCapacity)
{
int[ ] biggerArray;
if (data.length < minimumCapacity)
{
biggerArray = new int[minimumCapacity];
System.arraycopy(data, 0, biggerArray, 0, manyItems);
data = biggerArray;
}
}
public int getCapacity( )
{
return data.length;
}
public void trimToSize( )
{
int[ ] trimmedArray;
if (data.length != manyItems)
{
trimmedArray = new int[manyItems];
System.arraycopy(data, 0, trimmedArray, 0, manyItems);
data = trimmedArray;
}
}
public void add(int... elements)
{
if (manyItems + elements.length > data.length)
{ // Ensure twice as much space as we need.
ensureCapacity((manyItems + elements.length)*2);
}
for (int element : elements)
{
if ( ! this.contains(element) )
{
data[manyItems] = element;
manyItems++;
}
}
}
public void add(IntSet set2)
{
int[] arr = set2.toArray();
for( int element: arr)
{
if ( ! this.contains(element) )
{
this.add(element);
}
}
}
public void subtract(IntSet set2)
{
int[] arr = set2.toArray();
for (int element : arr)
{
if (this.contains(element) )
{
this.remove(element);
}
}
}
public void keepCommonElements(IntSet set2)
{
if (set2 != null)
{
int [] arr = set2. toArray();
for (int element: arr)
{
if(! this. contains(element))
{
this.remove(element);
}
}
}
}
public boolean contains(int target)
{
for (int n : data)
{
if( target == n)
{
return true;
}
}
return false;
}
public boolean remove(int target)
{
int index; // The location of target in the data array.
for (index = 0; (index < manyItems) && (target != data[index]); index++)
// No work is needed in the body of this for-loop.
;
if (index == manyItems)
// The target was not found, so nothing is removed.
return false;
else
{ // The target was found at data[index].
// So reduce manyItems by 1 and copy the last element onto data[index].
manyItems--;
data[index] = data[manyItems];
return true;
}
}
/**
* Determine the number of elements in this set.
* @return
* the number of elements in this set
*/
public int size( )
{
return manyItems;
}
/**
* Create a new set that contains all the elements from this set and the other set.
* @param set2
* the second set in the union
* @precondition
* set2 is not null, and
* getCapacity( ) + set2.getCapacity( ) <= Integer.MAX_VALUE.
* @return
* the union of this set and set2
* @exception NullPointerException
* Indicates that the argument is null.
* @exception OutOfMemoryError
* Indicates insufficient memory for the new set.
* @note
* An attempt to create a set with a capacity beyond
* Integer.MAX_VALUE will cause an arithmetic overflow
* that will cause the set to fail. Such large collections should use
* a different set implementation.
*/
public IntSet union(IntSet set2)
{
}
/**
* Create a new set that contains all the elements that are in both this set and the other set.
* @param set2
* the second set in the intersection
* @precondition
* set2 is not null
* @postcondition
* the returned set is smaller than either this set or set2
* @return
* the intersection of this set and set2
* @exception NullPointerException
* Indicates that the argument is null.
* @exception OutOfMemoryError
* Indicates insufficient memory for the new set.
*/
public IntSet intersection(IntSet set2)
{
// If set2 is null, then a NullPointerException is thrown.
}
/**
* Create a new set that contains all the elements from this set except those from the other set.
* @param set2
* the second set in the subtraction
* @precondition
* set2 is not null
* @postcondition
* the returned set is smaller than this set
* @return
* the subtraction of set2 from this set
* @exception NullPointerException
* Indicates that the argument is null.
* @exception OutOfMemoryError
* Indicates insufficient memory for the new set.
*/
public IntSet minus(IntSet set2)
{
}
public int[] toArray()
{
int[] arr = new int[this.size()]; //initialize the int array to store the set elements
for (int element : arr)
{
arr[element]= this.manyItems;
}
return arr; //return the int array
}
public String toString()
{
String result = \"{\";
for (int i = 0; i< this.size(); i++)
{
result +=\" \" + data[i];
//Add a comma after all but the last item
if (i < this.size()-1)
result += \",\";
}
result += \"}\";
return result;
}
}//IntArraySet
Solution
The following are the methods for union, intersection and minus
public IntSet union(IntSet set2)
{
List<IntSet> array=new ArrayList<IntSet>();
for(int num:set2)
{
array.add(num);
}
for(int num:this)
{
array.add(num);
}
return array;
}
public IntSet intersection(IntSet set2)
{
// If set2 is null, then a NullPointerException is thrown.
int size=set2.size()+this.size();
List<IntSet> array=new ArrayList<IntSet>(size);
for(int num:set2)
{
if(this.contains(num))
{
array.add(num);
}
}
return array;
}
public IntSet minus(IntSet set2)
{
ArrayList<IntSet> array=new ArrayList<IntSet>();
array.add(this);
array.removeAll(set2);
return array;
}




