Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Support For Data Mining Applications Discovering Knowledge With Inductive Queries Lnai 2682

Authors: Rosa Meo ,Pier L. Lanzi ,Mika Klemettinen

2004th Edition

3540224793, 978-3540224792

More Books

Students also viewed these Databases questions