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.

[10580 byte] By [asian_cajun_2000a] at [2007-10-3 6:54:39]
# 1

Once you return from a method, you can't continue in the method. Hence the "unreachable statement" errors.

Maybe you want (I'm not sure what you want "isSet" to mean--the following means that both a and b are set at index i):

public boolean isSet(int i) {

return ((a[i] == 1) && (b[i] == 1));

}

doremifasollatidoa at 2007-7-15 1:46:14 > top of Java-index,Java Essentials,Java Programming...
# 2

You didn't say where the error was occurring (you should post the entire error message), but this is wrong:

public boolean isSet(int i) {

return (a[i] == 1);

return (b[i] == 1);

}

Execution of code in that method ends at the first return statement, so the second one can never do anything.

You probably want an isSet method for each array, or maybe you want isSet to be true only if both arrays hold a 1 at that position?

Mr_Evila at 2007-7-15 1:46:14 > top of Java-index,Java Essentials,Java Programming...
# 3
>public boolean isSet(int i) {>return (a == 1);>return (b == 1); > }>Only use one return per method
hedya at 2007-7-15 1:46:14 > top of Java-index,Java Essentials,Java Programming...
# 4
Multi-post!
Mr_Evila at 2007-7-15 1:46:14 > top of Java-index,Java Essentials,Java Programming...
# 5

All right, I edited part of the code, and put in comments to where the error occurs.

public class IntegerSet {

private int [] a; // holds a set of numbers from 0 - 100

private int [] b; // also holds numbers 0 - 100

public IntegerSet () {

// an empty set, all a[i] are set to 0

a = new int [101];

b = new int [101];

}

// A constructor that copies from an existing set.

public IntegerSet (IntegerSet existingSet) {

a = new int [101];

b = new int [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];

}

public void deleteElement(int i) {

if ((i >= 0) && (i < a.length))

a[i] = 0; // set to 1

if ((i >= 0) && (i < b.length))

b[i] = 0;

}

public void 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

}

public boolean isSet(int i) {

return (a[i] == 1 && 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

public boolean isEmpty() {

for (int i=0; i><a.length; i++)

if (isSet(i)) return false;

return true;

for (int i=0; i><b.length; i++) // unreachable statement

if (isSet(i)) return false;

return true;

}

// return the 'length' of a set

public int returnLength() {

int count = 0;

for (int i=0; i><a.length; i++)

if (isSet(i))

count++;

for (int i=0; i><b.length; i++) // unreachable statement

if (isSet(i))

count++;

return count;

}

// Print a set to System.out

public void 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

public boolean isEqualTo(IntegerSet otherSet) {

for(int i=0; i><a.length; i++) {

if (otherSet.isSet(i) != isSet(i))

return false;

}

return true;

for(int i=0; i><b.length; i++) {

if (otherSet.isSet(i) != isSet(i))

return false;

}

return true;

}

//

// Small test program to verify that IntegerSet works!

public static void 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:]

}

asian_cajun_2000a at 2007-7-15 1:46:14 > top of Java-index,Java Essentials,Java Programming...
# 6

Same thing again: You have a return statement that will always execute, then you have some more code. The code after that return statement can never be reached because the method has already returned.

Same solution again: You either need separate methods for each array, or you need to check both arrays for (whatever) before returning.

Mr_Evila at 2007-7-15 1:46:14 > top of Java-index,Java Essentials,Java Programming...
# 7
Ok I think I know what to do now. Thanks.
asian_cajun_2000a at 2007-7-15 1:46:14 > top of Java-index,Java Essentials,Java Programming...