import java.util.*; import de.tu_darmstadt.es.sopra.exercises.*; public class Solution06 extends UnionFind { Vector> h = new Vector>(); public void makePartition(java.lang.Object obj) { if (obj != null) { HashSet h2 = new HashSet(); h2.add(obj); h.add(h2); } } public java.util.Collection find(java.lang.Object obj) { if (obj != null) { Enumeration> i = h.elements(); while (i.hasMoreElements()) { HashSet hs = i.nextElement(); Iterator it = hs.iterator(); while (it.hasNext()) { if (it.next() == obj) { return hs; } } } } return null; } public int getNrObjects() { int i = 0; Enumeration> e = h.elements(); while (e.hasMoreElements()) { try { System.out.println("getNrObjects : " + h.get(i)); } catch (Exception exception) { } i += e.nextElement().size(); } return i; } public int getNrPartitions() { return h.size(); } public void merge(java.lang.Object obj1, java.lang.Object obj2) { if (obj1 != null && obj2 != null) { HashSet g = new HashSet(); if (obj1 instanceof HashSet && obj2 instanceof HashSet == false) { g = (HashSet) obj2; g.add(obj2); h.add(g); } if (obj1 instanceof HashSet == false && obj2 instanceof HashSet) { g = (HashSet) obj2; g.add(obj1); h.add(g); } if (obj1 instanceof HashSet == false && obj2 instanceof HashSet == false) { g.add(obj1); g.add(obj2); h.add(g); } if (obj1 instanceof HashSet && obj2 instanceof HashSet) { g = (HashSet) obj2; HashSet h1 = (HashSet) obj1; g.addAll(h1); h.add(g); } h.removeElementAt(h.indexOf(obj1)+1); } } /** # * Returns information of the team.

# * # * @return text with team information # * @since 08.11.2006 # */ public String getTeam() { return "Team4"; } /** # * Returns authors of this solution.

# * # * @return String array with author names # * @since 08.11.2006 # */ public String[] getAuthors() { String[] Authors = {"Ben Ghozi", "Mahjoub", "Dhif", "Al Gauabra"}; return Authors; } /** # * Returns version of this implementation.

# * # * @return version as text # * @since 08.11.2006 # */ public String getVersion() { return "3.11.07"; } public static void main(String[] args) { Object EINS = "EINS"; Object ZWEI = "ZWEI"; Object DREI = "DREI"; Object VIER = "VIER"; Object FUENF = "FUENF"; Solution06 uf = new Solution06(); uf.makePartition(EINS); uf.makePartition(ZWEI); uf.makePartition(DREI); uf.makePartition(VIER); uf.makePartition(FUENF); uf.merge(DREI, ZWEI); uf.merge(FUENF, VIER); Collection p1 = uf.find(EINS); Collection p2 = uf.find(FUENF); uf.merge(FUENF, EINS); System.out.println("partition : " + uf.getNrPartitions() + " objects : " + uf.getNrObjects()); } }