// h6_t1_2.java SJ import java.util.Random; import java.util.LinkedList; import fi.joensuu.cs.tra.*; public class h6_t1_2 { public static void main(String[] args) { int N = 10; if (args.length > 0) N = Integer.valueOf(args[0]); LinkedList omalista = randomList(N, N*3, 2); System.out.println("Alkuperäiset"); System.out.println(omalista); LinkedList omalista3 = randomList(N, N*3, 1); System.out.println(omalista3); System.out.println("XOR33" + xor33(omalista, omalista3)); System.out.println("XOR34" + xor34(omalista, omalista3)); } // main() // returns a new LinkedList public static LinkedList randomList(int N, int M, int seed) { LinkedList L = new LinkedList(); Random r = new Random(seed); Integer x = new Integer(0); for (int i = 0; i < N; i++) { x = r.nextInt(M); L.add(x); } return L; } // randomList() // XOR two lists public static LinkedList xor33(LinkedList L1, LinkedList L2) { Set S1 = new Set(L1); Set S2 = new Set(L2); Set S = S1.union(S2).difference(S1.intersection(S2)); return new LinkedList(S); } // xor33 public static LinkedList xor34(LinkedList L1, LinkedList L2) { LinkedList L = new LinkedList(); Set S1 = new Set(L1); Set S2 = new Set(L2); for (Object x : L1) if (! S2.contains(x)) L.add(x); for (Object x : L2) if (! S1.contains(x)) L.add(x); return L; } // xor34 } // class h6_t1_2 ================================================================ // h6_t3.java SJ import fi.joensuu.cs.tra.*; import java.util.Random; public class h6_t3 { public static void main(String[] args) { int N = 10; if (args.length > 0) N = Integer.valueOf(args[0]); Set S1 = new Set(); Set S2 = new Set(); Set S3 = new Set(); Random r = new Random(42); Integer x, y; for (int i = 0; i < N; i++) { x = r.nextInt(N*2); S1.add(x); x = r.nextInt(N*2); S2.add(x); x = r.nextInt(N*2); S3.add(x); } System.out.println("S1: " + S1); System.out.println("S2: " + S2); System.out.println("S3: " + S3); System.out.println("Kaksikolmesta: " + kaksiKolmesta(S1, S2, S3)); } // main() public static Set kaksiKolmesta(Set A, Set B, Set C) { Set AB = A.intersection(B).difference(C); Set AC = A.intersection(C).difference(B); Set BC = B.intersection(C).difference(A); System.out.println("AB: " + AB); System.out.println("AC: " + AC); System.out.println("BC: " + BC); return AB.union(AC).union(BC); } // kaksiKolmesta() } // class h6_t3