The purpose of this project is:
Create a class called IntegerSet that implements Comparable interface to be used in simple set applications. Each object of class IntegerSet can hold positive integers in the range 0 through 255 and most common set operations are available. A set must be represented internally as an int array of ones and zeros (or a boolean array). Array element a[ i ] is 1 if integer i is in the set and array element a[ j ] is 0 if integer j is not in the set.
and then add some member method to modify it.
I am not familiar with "implements Comparable interface," but here is what I got so far. The driver cannot test it, it shows "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 256"
Can someone help me find out what the problem is? thanks!
import java.util.Arrays;
public class IntegerSet implements Comparable
{
private int L = 256;
private int[] a = new int[L];
public IntegerSet()
{
Arrays.fill(a, 0);
}
public IntegerSet(int n)
{
a[n] = 1;
}
public IntegerSet(int[] n)
{
a = n;
}
public IntegerSet union(IntegerSet other)
{
int[] c = new int[L];
for(int i = 0; i < L; i++)
{
if(this.a[i]==1 || other.a[i] ==1)
c[i] = 1;
else
c[i] = 0;
}
IntegerSet temp = new IntegerSet(c);
return temp;
}
public IntegerSet intersection(IntegerSet other)
{
int[] c = new int[L];
for(int i = 0; i < L; i++)
{
if(this.a[i]==1 && other.a[i] ==1)
c[i] = 1;
else
c[i] = 0;
}
IntegerSet temp = new IntegerSet(c);
return temp;
}
public IntegerSet difference(IntegerSet other)
{
int[] c = new int[L];
for(int i = 0; i < L; i++)
{
if(this.a[i] != other.a[i])
c[i] = 1;
else
c[i] = 0;
}
IntegerSet temp = new IntegerSet(c);
return temp;
}
public IntegerSet insertElement(int k)
{
a[k] = 1;
IntegerSet temp = new IntegerSet(a);
return temp;
}
public IntegerSet removeElement(int k)
{
a[k] = 0;
IntegerSet temp = new IntegerSet(a);
return temp;
}
public boolean isElement(int k)
{
return(a[k] == 1);
}
public String toString()
{
String str = "";
for(int i = 0; i < L; i++)
{
if(a[i] == 1)
str += (i + ", ");
}
return "{" + str + "}";
}
public IntegerSet copy()
{
IntegerSet temp = new IntegerSet(a);
return temp;
}
public boolean subset(IntegerSet other)
{
boolean sub = true;
int i = 0;
while(sub)
{
if(this.a[i] == 1)
{
if(other.a[i] == 1)
sub = true;
else
sub = false;
}
i++;
}
return sub;
}
public boolean superset(IntegerSet other)
{
boolean sup = true;
int i = 0;
while(sup)
{
if(other.a[i] == 1)
{
if(this.a[i] == 1)
sup = true;
else
sup = false;
}
i++;
}
return sup;
}
public void addAll()
{
Arrays.fill(a, 1);
}
public void removeAll()
{
Arrays.fill(a, 0);
}
public int compareTo(Object other)
{
// TODO Auto-generated method stub
return 0;
}
}
and the driver:
public class IntegerSetDriver
{
public static void main(String[] args)
{
IntegerSet s1, s2, s3, s4, s5;
s1 = new IntegerSet(); // s1 is an empty set, {}
s2 = new IntegerSet(5); // s2 is a set with one element, {5}
s1.insertElement(1); // s1 is now {1}
s3 = s1.copy(); // s3 is now {1}
s4 = s1.union(s2); // s4 is now {1, 5} and s1 is still {1}
s5 = s4.insertElement(8).removeElement(5); // s4 is now {1, 8} // s5 references s4
int result = s3.compareTo(s4); // result is -1 (or < 0)
boolean yes = s3.subset(s4); // yes should be true
s5.removeAll(); // s4 and s5 both reference same empty set, {}
s1.removeElement(500); // invalid element so ignore, s1 is still {1}
}
}