Question: STARTER CODE import java.lang.UnsupportedOperationException; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class SetUtilities { // constants for parsing input public static final int

 STARTER CODE import java.lang.UnsupportedOperationException; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import

java.util.Set; public class SetUtilities { // constants for parsing input public static

STARTER CODE

import java.lang.UnsupportedOperationException; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set;

public class SetUtilities { // constants for parsing input public static final int INTERSECTION_OPERATION = 0; public static final int UNION_OPERATION = 1; public static void main(String[] args) { Scanner sc = new Scanner(System.in); // parse operation int operation = Integer.parseInt(sc.nextLine()); // variables for processing input String[] numberStrings; Integer[] numbers; String input; // parse first set Set set1; input = sc.nextLine(); if (input.equals("")) { set1 = new HashSet(); } else { numberStrings = input.split(" "); numbers = new Integer[numberStrings.length]; for (int i = 0; i (Arrays.asList(numbers)); } // parse second set Set set2; input = sc.nextLine(); if (input.equals("")) { set2 = new HashSet(); } else { numberStrings = input.split(" "); numbers = new Integer[numberStrings.length]; for (int i = 0; i (Arrays.asList(numbers)); } // find the result set Set result; if (operation == INTERSECTION_OPERATION) { result = findIntersection(set1, set2); } else if (operation == UNION_OPERATION) { result = findUnion(set1, set2); } else { throw new UnsupportedOperationException(); } // convert the result set to a sorted array and print it Integer[] resultInts = result.toArray(new Integer[result.size()]); Arrays.sort(resultInts); StringBuilder resultSb = new StringBuilder(); for (int i = 0; i findIntersection(Set set1, Set set2) { // TODO implement this throw new UnsupportedOperationException(); } private static Set findUnion(Set set1, Set set2) { // TODO implement this throw new UnsupportedOperationException(); } }

Code in Java, with some comments (begginer here)

(20 points) In this problem, we will implement the set operations of inter- section and union. The class SetUtilities contains the starter code for this problem. You may not use the retainAll() or addA11 ) methods of a set in this problem. Input will be given as a line containing a positive integer n, which 0 if we want the intersection of sets or a 1 if we want the union. The next line will be a space-separated list of integers representing the first set, and the following line will be a space-separated list of integers representing the second set. (a) (6 points) Implement the findIntersection (SetInteger> set 1, Set set2) function, which should return a Set containing the integers that are in both set1 and set2. (6 points) Implement the findUnion(SetInteger> set2) function, which should return a Set containing the integers that are in set1 or set2. (b) set 1, SetInteger>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!