SE251Ex:Set Operations

From Marks Wiki
Jump to navigation Jump to search

Writing set operations in Java

Prerequisite

This exercise requires you to have completed the exercise SE251Ex:Generic_Pair, as it makes use of the Pair<E1,E2> class from the exercise.

Description

You learnt about the Set<E> interface from java.util, and the operations supported by it. What the Java API doesn't provide however are the mathematical set operations including union, intersection, difference and cartesian product (edit: actually it kind of does with the methods addAll(), removeAll() and retainAll(), but still performs the operations destructively, that is, by modifying the original set). These operations are quite straightforward, but you may want to brush up on them with the help of Wikipedia. Below is a skeleton for the class SetOperations providing four static generic methods, each for one of the operations. The Javadoc comments for each method should provide enough information about what exactly it should do. Note: each method is declared to return a Set<E>, which means it is up to you to chose which implementation of Set (i.e. HashSet or a TreeSet) to return.

import java.util.Set;

public class SetOperations {
	/**
	 * Returns the union of two given sets. E.g. given sets {a,c,d,g}
	 * and {b,d,e,f} the method should return {a,b,c,d,e,f,g}
	 * @param <E> the type of elements in each of the sets
	 * @param sets the sets to perform union on
	 * @return the union of all given sets
	 */
	public static<E> Set<E> union(Set<E> set1, Set<E> set2) {
		
	}
	
	/**
	 * Return the intersection between the two given sets. E.g. given
	 * set1 = {a,b,c,d} and set2 = {a,d,e,f,g}, the method should
	 * return {a,d} 
	 * @param <E> the type of elements in each of the sets
	 * @param set1 the first set
	 * @param set2 the second set
	 * @return the intersection between set1 and set2
	 */
	public static<E> Set<E> intersection(Set<E> set1, Set<E> set2) {
		
	}
	
	/**
	 * Returns the set difference between set1 and set2. Specifically, returns
	 * a set of all items in set1 that set2 doesn't contain. E.g. given
	 * set1 = {a,b,c,d} and set2 = {a,d,e,f,g}, the method should return
	 * {b,c}
	 * @param <E> the type of elements in each of the sets
	 * @param set1 the first set
	 * @param set2 the second set
	 * @return The mathematical function <code>set1 \ set2</code>
	 */
	public static<E> Set<E> difference(Set<E> set1, Set<E> set2) {
		
	}
	
	/**
	 * Return the cartesian product between the two given sets, i.e. set1 x set2.
	 * The result is effectively a set of all pairs <x,y> such that x is a member of
	 * set1 and y is a member of y. E.g. given set1 = {a,b,c} and set2 = {1,2,3}
	 * the result would be {(a,1),(a,2),(a,3),(b,1),(b,2),(b,3),(c,1),(c,2),(c,3)}
	 * @param <E> the type of elemets in set1
	 * @param <F> the type of elemets in set2
	 * @param set1 the first set
	 * @param set2 the second set
	 * @return the cartesian product as a set of Pair objects
	 */
	public static<E,F> Set<Pair<E,F>> cartesianProduct(Set<E> set1, Set<F> set2) {
		
	}
}

Test Cases

Tips

All classes implementing Collection defines a method called addAll(Collection) method. You may find this useful. What this does is adds all elements from the given Collection to the current collection. For example we can do this:

List<String> original = new ArrayList<String>();
original.add("A");
original.add("B");
original.add("C");
original.add("B");
Set<String> words = new HashSet<String>();
words.addAll(original);

The contents of words will become {"A", "B", "C"} as a result.

Discussion

Here you can ask questions and discuss stuff