// TRAI_24_4_5_skeleton.java SJ // Main program and skeleton for week 1 tasks 5 and 6 import java.util.ArrayList; public class TRAI_24_4_5_skeleton { // Main program usage: // java TRAI_24_4_5_skeleton [N] [N2] [S] // where N is the number of elements, N2 is the number of elements of the second array // and S is the seed for random number generation public static void main(String[] args) { // input sizes int N1 = 10; if (args.length > 0) N1 = Integer.parseInt(args[0]); int N2 = N1; if (args.length > 1) N2 = Integer.parseInt(args[1]); // random number seed int seed = 2024; if (args.length > 2) seed = Integer.parseInt(args[2]); // create input arrays Integer[] T1 = new Integer[N1]; Integer[] T2 = new Integer[N2]; // fill with elements java.util.Random r = new java.util.Random(seed); for (int i = 0; i < N1; i++) { T1[i] = r.nextInt(N1); } for (int i = 0; i < N2; i++) { T2[i] = r.nextInt(N2 * 2); } // print arrays (unless there are a lot of elements) if (N1 <= 20 && N2 <= 20) { System.out.print("T1: "); for (int i = 0; i < N1; i++) System.out.print(" " + T1[i]); System.out.println(); System.out.print("T2: "); for (int i = 0; i < N2; i++) System.out.print(" " + T2[i]); System.out.println(); } // call task 4 Integer[] U4 = union4(T1, T2); System.out.print("Task 4, union = "); if (N1 <= 20 && N2 <= 20) { for (Integer i : U4) System.out.print(" " + i); System.out.println(); } else { System.out.println(U4.length + " element array"); // notice: this prints array size, not the number of real elements } // Copy arrays into ArrayLists ArrayList L1 = new ArrayList<>(T1.length); ArrayList L2 = new ArrayList<>(T2.length); for (Integer x : T1) L1.add(x); for (Integer x : T2) L2.add(x); // call task 5 ArrayList U5 = union5(L1, L2); System.out.print("Task 5, union = "); if (N1 <= 20 && N2 <= 20) { for (Integer i : U5) System.out.print(" " + i); System.out.println(); } else { System.out.println(U5.size() + " elements"); } } // main() /** * 4. Write an algorithm (Java method) that takes as parameters two integer arrays (Integer[] * A, Integer[] B) and creates and returns a new integer array containing all elements that * are found in either of the arrays (i.e., their union). However, each element (.equals() * returns true) should appear in the resulting array only once, even if it appears multiple * times in one or both of the input arrays. What is the time complexity of your algorithm? * Could it be improved? *

* Returns the union of the arrays T1 and T2. * null elements are ignored. * * @param T1 first array * @param T2 secord array * @return new array containing elements that are in T1 and/or T2 */ public static Integer[] union4(Integer[] T1, Integer[] T2) { // TODO return new Integer[0]; // replace with real result array } // union4 () /** * 5. Review the ArrayList class covered in the Programming II course. For more infor- * mation, see: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList. * html. Modify the algorithm from the previous task to work with ArrayList collections * instead of arrays. What is the time complexity of your algorithm? Could it be improved? *

* Returns the union of the lists L1 and L2. * null elements are ignored. * * @param L1 first ArrayList * @param L2 secord ArrayList * @return new ArrayList containing elements are in L1 and/or L2 */ public static ArrayList union5(ArrayList L1, ArrayList L2) { ArrayList union = new ArrayList(); // TODO return union; } // union5() } // class