Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Okay So i had this assignment to do: Display two lists of integers generated by

ID: 3652461 • Letter: O

Question

Okay So i had this assignment to do:
Display two lists of integers generated by a random number generator, and find and display their intersection and union. And we are supposed to use linked lists. I have this so far:

LinkedList<Integer> Set1 = new LinkedList<Integer>(); //This will be the first list
LinkedList<Integer> Set2 = new LinkedList<Integer>(); //This will the second list
LinkedList<Integer> Set3 = new LinkedList<Integer>(); //This list will show the union
LinkedList<Integer> Set4 = new LinkedList<Integer>(); //This list will show the intersection

Set1.add((int) (15*Math.random())+1);
Set1.add((int) (15*Math.random())+1);
Set1.add((int) (15*Math.random())+1);
Set1.add((int) (15*Math.random())+1);
Set1.add((int) (15*Math.random())+1);
Set1.add((int) (15*Math.random())+1);
Set1.add((int) (15*Math.random())+1);
Set1.add((int) (15*Math.random())+1);
Set1.add((int) (15*Math.random())+1);

Set2.add((int) (15*Math.random())+1);
Set2.add((int) (15*Math.random())+1);
Set2.add((int) (15*Math.random())+1);
Set2.add((int) (15*Math.random())+1);
Set2.add((int) (15*Math.random())+1);
Set2.add((int) (15*Math.random())+1);
Set2.add((int) (15*Math.random())+1);
Set2.add((int) (15*Math.random())+1);
Set2.add((int) (15*Math.random())+1);


So now that i have the first two lists, how will i display the union and the list.
Can someone please help me on this ASAP. Just like explain the union and the intersection code for me. Thank you!

Explanation / Answer

The intersection of two lists are all the elements that are in both lists. Thus, if we had the lists A = 1, 2, 3, 4, 5 B = 4, 5, 6, 7, 8 The common elements between A and B would be 4 and 5. Therefore, the intersection would be the list C = 4, 5 You could use a loop to iterate over the first list, and for each number in that list check if Set2.contains(theNumber); The union is a new list that will be all of the numbers in the first list, PLUS, all of the numbers in the second list that are not in the first list (so no duplicates). So if we had the same lists as before A and B, then the union of A and B would be: D = 1, 2, 3, 4, 5, 6, 7, 8 The code for the union would involve making a copy of the first list (you can pass the first list into the second LinkedList constructor, or use the addAll method, or even make a loop and do it manually). The second step would be to iterate through all of the elements in the second list, check if Set1.contains(theSet2Number), and add it to the new list if it doesn't yet contain it. In addition, you may or may not want some more numbers in your lists, which you could do by making a loop such as this: int numberOfNumbers = 15; Random random = new Random(); for (int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote