ArrayList (Unions, Intersection, Difference, Etc)

I am working on a class assignment and I am having a difficult time understanding how to manipulate multiple arrays. More specifically, Union, Intersection, Difference, etc. of multiple sets.

This is what I have thus far.

public class Set
{
private ArrayList<String>elements;
/**
* creates an empty set
*/
public Set()
{
elements = null;
}
/**
* creates a set using the elements of the ArrayList s.
* @param s the ArrayList whose elements are used to create this set.
* @throws IllegalArgumentException if s contains duplicity.
*/
public Set(ArrayList<String> s)
{
int i;
elements = new ArrayList<String>();
for(i=0;i<s.size();i++)
{
if(elements.contains(s.get(i)))
{throw new IllegalArgumentException("Set(ArrayList<String>)duplicity not allowed in sets");}
elements.add(s.get(i));
}
}
/**
* creates a set using the elements of the array s.
* @param s the array whose elements are used to create this set.
* @throws illegalArgumentException if s contains duplicity.
*/
public Set(String[] s)
{
int i;
elements = new ArrayList<String>();
for(i=0; i<s.length; i++)
{
if (elements.contains(s[i]))
{throw new IllegalArgumentException("Set(String[]):duplicity not allowed in sets");}
elements.add(s[i]);
}
}
/**
* determines whether a set contains the specified element
* @param elt an element
* @return true if elt is an element of this set; otherwise, false
*/
public boolean isElement(String elt)
{
return elements.contains(elt);
}
/**
* determines the size of this set.
* @return the size of this set.
*/
public int cardinality()
{
return elements.size();
}
/**
* computes the intersection of this set and the
* specified set.
* @param s a set
* @return a set representing the intersection of this set
* and s.
*/
public Set intersect(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
for (i=0;i<s.cardinality();i++)
{
if (this.isElement(s.elements.get(i)))
{result.add(s.elements.get(i));}
}
return new Set(result);
}
/**
* computes the union of this set and the specified set.
* @param s a sets
* @return a set representing the union of this set
* and s.
*/
public Set union(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
for (i=0;i<s.cardinality();i++)
{
}
}
/**
* computes the difference between this set and the
* specified set.
* @param s a set
* @return a set representing the difference between
* this set and s.
*/
public Set diff(Set s)
{
}
/**
* computes the symmetric difference between this set
* and the specified set.
* @param s a set
* @return a set representing the symmetrix difference
* between this set and s.
*/
public Set symDiff(Set s)
{
}
/**
* computes the Cartesian product for this set
* and the specified set.
* @param s a set
* @return a set representing the Cartesian product
* of this set and s.
*/
public Set xProduct(Set s)
{
}
/**
* determines whether a set is empty
* @return true if this set is empty; otherwise, false
*/
public boolean isEmpty()
{
return elements.isEmpty();
}
/**
* determines whether this set is equal to the specified
* set.
* @param s a set
* @return true if this set is equal to s; otherwise, false
*/
public boolean equals(Set s)
{
}
/**
* determines whether this set is a subset of the specified set.
* @param s a set
* @return true if this set is a subset of s; otherwise, false
*/
public boolean subset(Set s)
{
}
/**
* determines whether this set is a proper subset of the specified set.
* @param s a set
* @return true if this set is a proper subset of s; otherwise, false
*/
public boolean properSubset(Set s)
{
}
/**
* returns a string {x1,x2,...,xn} representing this set,
* where x1,x2,...,xn are elements of this set.
* @return a string representation of this set formatted
* as specified.
*/
@Override
public String toString()
{
return "Set="++"";
}

Basically I need to implement all the methods. The public Set intersect(Set s) was given as a guide. I'm simply looking for guidance as I am new to Java. I know I need to use for loops and if statements and to check the boolean for each Array.

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 15 November 2012 - 09:34 PM

Ok, this is what I have so far and it's not working.

public Set union(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
for(i=0;i<s.cardinality();i++)
{
if (this.isElement(s.elements.get(i)))
{result.remove(s.elements.get(i));}
}
return new Set(result);
}
/**
* computes the difference between this set and the
* specified set.
* @param s a set
* @return a set representing the difference between
* this set and s.
*/
public Set diff(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
for(i=0;i<s.cardinality();i++)
{
if (this.isElement(s.elements.get(i)) && s.isElement(s.elements.get(i)))
{result.remove(s.elements.get(i));}
}
return new Set(result);
}
/**
* computes the symmetric difference between this set
* and the specified set.
* @param s a set
* @return a set representing the symmetrix difference
* between this set and s.
*/
public Set symDiff(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
for(i=0;i<s.cardinality();i++)
{
if (this.isElement(s.elements.get(i)) && !s.isElement(s.elements.get(i)))
{result.add(s.elements.get(i));}
}
return new Set(result);
}
/**
* computes the Cartesian product for this set
* and the specified set.
* @param s a set
* @return a set representing the Cartesian product
* of this set and s.
*/
public Set xProduct(Set s)
{
//implement this method
}
/**
* determines whether a set is empty
* @return true if this set is empty; otherwise, false
*/
public boolean isEmpty()
{
return elements.isEmpty();
}
/**
* determines whether this set is equal to the specified
* set.
* @param s a set
* @return true if this set is equal to s; otherwise, false
*/
public boolean equals(Set s)
{
return elements.equals(s.elements);
}
/**
* determines whether this set is a subset of the specified set.
* @param s a set
* @return true if this set is a subset of s; otherwise, false
*/
public boolean subset(Set s)
{
return elements.containsAll(s.elements);
}
/**
* determines whether this set is a proper subset of the specified set.
* @param s a set
* @return true if this set is a proper subset of s; otherwise, false
*/
public boolean properSubset(Set s)
{
if(elements.equals(s.elements) && elements.containsAll(s.elements))
{return false;}
else{
return true;
}
}
/**
* returns a string {x1,x2,...,xn} representing this set,
* where x1,x2,...,xn are elements of this set.
* @return a string representation of this set formatted
* as specified.
*/
@Override
public String toString()
{
//implement this method
}

Re: ArrayList (Unions, Intersection, Difference, Etc)

I have to implement a method to calculate the Cartesian product of this array and a specified array.

I assume I have to create a new arraylist.

Like

int i;
ArrayList<String> result = new ArrayList<String>();
for (i=0;i<s.cardinality();i++)

This is where I'm stuck. I know there are methods available to simply calculate the Cartesian Product, but I'm not allowed to use them. I have to figure out how to iterate through each array and put them in a specific format.

Re: ArrayList (Unions, Intersection, Difference, Etc)

That's not code you need help with but an empty method you (apparently) want someone to write for you unless you're confused about what the Cartesian Product is and have questions about that.

Wikipedia gives a good example using the standard deck of cards: the X-product of a set of 13 card ranks and another set of 4 card suits results in the standard deck of 52 playing cards (13 x 4 members in the result).

Beyond the definition, what do you need help with? You mention iterating through the arrays and putting them in a specific format. What do you mean?

Re: ArrayList (Unions, Intersection, Difference, Etc)

I'm just trying to understand what you need help with. Based on your latest, it seems you have it all figured out, except <blank>, and I don't know what the <blank> is.

Yes, two for loops that pair each element of one array with each element of the other then storing the resulting pairing in a new array is an approach that should work. What's preventing you from writing that?