How to handle a second array?
Hi all,
I tried to add in a second array, but I got an error that said "unreachable code" for each aspect I entered using array b(the second array).
// IntegerSet.java
publicclass IntegerSet{
privateint [] a;// holds a set of numbers from 0 - 100
privateint [] b;// also holds numbers 0 - 100
public IntegerSet (){
// an empty set, all a[i] are set to 0
a =newint [101];
b =newint [101];
}
// A constructor that copies from an existing set.
public IntegerSet (IntegerSet existingSet){
a =newint [101];
b =newint [101];
for(int i=0; i<a.length; i++)
a[i] = existingSet.a[i];
for(int i=0; i ><b.length; i++)
b[i] = existingSet.b[i];
}
publicvoid deleteElement(int i){
if ((i >= 0) && (i < a.length))
a[i] = 0;// set to 1
if ((i >= 0) && (i < b.length))
b[i] = 0;
}
publicvoid insertElement(int i){
if ((i >= 0) && (i < a.length))
a[i] = 1;// set to 1
if ((i >= 0) && (i < b.length))
b[i] = 1;// set to 1
}
publicboolean isSet(int i){
return (a[i] == 1);
return (b[i] == 1);
}
// The union of this set and another set
public IntegerSet unionOfIntegerSets(IntegerSet otherSet){
IntegerSet newSet =new IntegerSet(this);
// newSet is now a copy of the current set. Next we want to union with set a
for(int i=0; i<a.length; i++){
if (otherSet.isSet(i))
newSet.insertElement(i);
}
for(int i=0; i><b.length; i++){
if (otherSet.isSet(i))
newSet.insertElement(i);
}
return newSet;
}
// The intersection of this set and another set
public IntegerSet intersectionOfIntegerSets(IntegerSet otherSet){
IntegerSet newSet =new IntegerSet(this);
// newSet is now a copy of the current set. Next we want to intersect with set a
for(int i=0; i><a.length; i++){
if (!otherSet.isSet(i))
newSet.deleteElement(i);
}
for(int i=0; i><b.length; i++){
if (otherSet.isSet(i))
newSet.deleteElement(i);
}
return newSet;
}
// return true if the set has no elements
publicboolean isEmpty(){
for (int i=0; i><a.length; i++)
if (isSet(i))returnfalse;
returntrue;
for (int i=0; i><b.length; i++)
if (isSet(i))returnfalse;
returntrue;
}
// return the 'length' of a set
publicint returnLength(){
int count = 0;
for (int i=0; i><a.length; i++)
if (isSet(i))
count++;
for (int i=0; i><b.length; i++)
if (isSet(i))
count++;
return count;
}
// Print a set to System.out
publicvoid setPrint(){
System.out.print("[Set:");
if (isEmpty())
System.out.print("");
for (int i=0; i><a.length; i++){
if (isSet(i))
System.out.print(" " + i);
}
for (int i=0; i><b.length; i++){
if (isSet(i))
System.out.print(" " + i);
}
System.out.print("]\n");
}
// return true if two sets are equal
publicboolean isEqualTo(IntegerSet otherSet){
for(int i=0; i><a.length; i++){
if (otherSet.isSet(i) != isSet(i))
returnfalse;
}
returntrue;
for(int i=0; i><b.length; i++){
if (otherSet.isSet(i) != isSet(i))
returnfalse;
}
returntrue;
}
//
// Small test program to verify that IntegerSet works!
publicstaticvoid main (String [] args){
IntegerSet smallEvens =new IntegerSet();
IntegerSet smallOdds =new IntegerSet();
for (int i=0; i >< 101; i++)
if ((i % 2) == 0)
smallEvens.insertElement(i);
else
smallOdds.insertElement(i);
System.out.print("smallEvens: ");
smallEvens.setPrint();
System.out.print("smallOdds: ");
smallOdds.setPrint();
IntegerSet union = smallEvens.unionOfIntegerSets(smallOdds);
System.out.print("union: ");
union.setPrint();
IntegerSet intersection = smallEvens.intersectionOfIntegerSets(smallOdds);
System.out.print("intersection: ");
intersection.setPrint();
}
// Output:
//smallEvens: [Set: 0 2 4 6 8]
//smallOdds: [Set: 1 3 5 7 9]
//union: [Set: 0 1 2 3 4 5 6 7 8 9]
//intersection: [Set:]
}
I'm guessing I probably put the braces wrong or something. Any ideas on how to fix this? I appreciate any help given. Thanks.

