import java.util.*;
interface ADT {
HashSet newSet (); // create an empty set
HashSet add_element (HashSet s, Object x); // add an element of type X to a set
HashSet union (HashSet s1, HashSet s2); // s1 \/ s2
HashSet intersection (HashSet s1, HashSet s2); // s1 /\ s2
HashSet sub (HashSet s1, HashSet s2); // s1 - s2
HashSet complement (HashSet s, HashSet ss); // ~s
int whatsize (HashSet s); // number of elements of a set
boolean contain_not (HashSet s, Object x); // x\in S?
boolean equal_not (HashSet s1, HashSet s2); // equality testing
boolean empty_not (HashSet s);
}
class myset implements ADT{
public HashSet newSet (){ return new HashSet();}
public HashSet add_element (HashSet ss,Object x) { ss.add(x); return ss; }
public HashSet union (HashSet sa, HashSet sb){HashSet h=new HashSet(); h.addAll(sa); h.addAll(sb); return h;}
public HashSet intersection (HashSet sa, HashSet sb){HashSet h=new HashSet();h.addAll(sa); h.retainAll(sb); return h;}
public HashSet sub (HashSet sa, HashSet sb){HashSet h=new HashSet();h.addAll(sa);h.removeAll(sb);return h;}
public HashSet complement (HashSet s, HashSet ss){ HashSet h=new HashSet();h.addAll(s);h.removeAll(ss);return h;}
public int whatsize (HashSet s){ return s.size(); }
public boolean contain_not (HashSet s, Object x){ return s.contains(x); }
public boolean equal_not (HashSet s1, HashSet s2){return s1.equals(s2);}
public boolean empty_not (HashSet s){ return s.isEmpty();}
}
class check1_myset{
public static void main(String[] args){
myset s=new myset();
HashSet h=s.newSet ();HashSet s1=s.newSet ();HashSet s2=s.newSet ();
//h={a,b,c,d} s1={a,d} s2={b,d}
s.add_element (h,"a"); s.add_element (h,"b"); s.add_element (h,"c"); s.add_element (h,"d");
s.add_element (s1,"a"); s.add_element (s1,"d");
s.add_element (s2,"b"); s.add_element (s2,"d");
System.out.println("全集="+h);
System.out.println("s1="+s1);
System.out.println("s2="+s2);
System.out.println("s1 or s2="+s.union (s1, s2));
System.out.println("s1 and s2="+s.intersection (s2, s1));
System.out.println("s2 - s1="+s.sub (s2, s1));
System.out.println("~ s2="+s.complement(h, s2));
System.out.println("size of s1="+s.whatsize(s1));
System.out.println("<s1 and s2> contains a or not="+s.contain_not(s.intersection (s1, s2),"a"));
System.out.println("s1 equals ~s2 or not="+s.equal_not(s1,s.complement(h, s2)));
System.out.println("s1 is empty or not="+s.empty_not(s1));
}
}
评论0
最新资源