Edtera Edtera - 5 months ago 12
Java Question

Cartesian products of two sets in Java

I'm trying to solve a problem from a textbook I'm using that has to do with cartesian products and sets without using built-in java APIs or any fancy function.

For example
Set A contains = {1,3,4}

Set B contains = {2,5}

their products would yield to this result {(1,2),(1,5),(3,2),(3,5),(4,2),(4,5)}

I have written some methods to perform various functions on each set but here is what I came up with. How could I implement this to the sets?

public String cartesian(Set other)
{
String result = "";
int res;

for ( int i = 0; i < this.size; ++i )
{
for ( int j = 0; j < other.size; ++j )
{
//System.out.println("@@@@@"+ other.size);
//result = data[i] + ""+ other[i] + "";
//res = data[i] *= other.data[j];

}
}

return result;

}


The method returns the result as a string. My logic is to got through each set's element at the same time but I get stuck at thinking up a way to cross them together.

Here is the rest of my code.

public class Sets {
public static void main(String[] args)
{

Set set1;
set1 = new Set();


Set set2 = new Set();

set1.add(1);
set1.add(2);
set1.add(3);

set2.add(3);
set2.add(4);
/*set2.add(4);
set2.add(5);*/

//System.out.println(set1.difference(set2));

System.out.println(set1.cartesianReformed(set2));



}
}


User-defined
Set
class

class Set
{
private int[] data;
private int size;

public Set()
{
data = new int[20];
size = 0;

}

public void add(int value)
{
int[] copy;

//avoiding duplicates
if ( !in(value) )
{
if ( size > data.length )
{
copy = new int[data.length * 2];

System.arraycopy(data, 0, copy,0,data.length);

data = copy;
}
data[size] = value;

size++;
}
else
{
System.out.println("You are trying to insert a number that's already here ---> " + value);
}

}

public String toString()
{
String result = "{";
for(int i = 0; i < size; i++)
{
result += "" + data[i];
//Add a comma after all but the last item
if ( i < size - 1 )
{
result += ",";
}

}
result += "}";
return result;
}

public boolean in(int value)
{
boolean result = false;

for(int i = 0; i < size; i++)
{
if ( data[i] == value )
{
result = true;
}
}

return result;
}

public Set intersection(Set other)
{
Set result = new Set();

for ( int i = 0; i < size; ++i )
{
if ( other.in(data[i]) )
{
result.add(data[i]);
}
}
return result;
}

public boolean equals(Set other)
{
boolean result = false;

int count = 0;

for ( int i = 0; i < size; ++i ) //iterating over this
{
if ( other.in(data[i]) )
{
count++;
}

if ( count == size )
{
result = true;
}
}
return result;
}

public Set difference(Set other)
{
Set result = new Set();

for(int i = 0; i < size; ++i)
{
if ( !other.in(data[i]) )
{
result.add(data[i]);
}
}

return result;
}



public String cartesian(Set other)
{
String result = "";
int res;

for ( int i = 0; i < this.size; ++i )
{
for ( int j = 0; j < other.size; ++j )
{
//System.out.println("@@@@@"+ other.size);
//result = data[i] + ""+ other[i] + "";
//res = data[i] *= other.data[j];

}
}

return result;

}

public Set union(Set other) {
Set result = (Set)other.clone();
for (int i = 0; i < size; i++) {
result.add(data[i]);
}
return result;
}

public Object clone() {
Set result = new Set();
for (int i = 0; i < size; i++) {
result.add(data[i]);
}
return result;
}
}

Answer

Something like this will work:

public String cartesian (Set other)
{
    String [] cart = new String [this.size * other.size];

    int k = 0;
    for (int i : this.data)
    {
        for (int j : other.data)
        {
            cart[k++] = "(" + i + "," + j + ")";
        }
    }

    return Arrays.toString(cart);   
}

Returns:

[(1,2), (1,5), (3,2), (3,5), (4,2), (4,5)]

Note:

  • Naming your class Set is a bad idea since it conflicts with java.util.Set.