In: Computer Science
Java
Write a method intersect_or_union_fcn() that gets vectors of type integer v1, v2, and v3 and determines if the vector v3 is the intersection or union of vectors v1 and v2.
Example 1: If v1 = {2, 3, 1, 5}, v2 = {3, 4, 5} and v3 = {3, 5}, then:
intersect_or_union_fcn(v1, v2, v3) will print:
v3 is the intersection of v1 and v2
Example 2: If v1 = {2, 3, 1, 5}, v2 = {3, 4, 5} and v3 = {2, 3, 1, 5, 4}, then:
intersect_or_union_fcn(v1, v2, v3) will print:
v3 is the union of v1 and v2
Example 3: If v1 = {2, 3, 1, 5}, v2 = {3, 4, 5} and v3 = {2, 3, 1, 5, 4, 6}, then:
intersect_or_unition_fcn(v1, v2, v3) will print:
v3 is neither the intersection nor the union of v1 and v2.
Write a test program that prompts the user to enter three vectors and test your program with all the three examples above.
Your code for this problem |
-- Copy and paste your code here |
Run the code and insert the result in the following box.
The result of the query |
Copy and paste the result here (e.g. the screen shot of the result you get by running the code). |
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thank You ! =========================================================================== import java.util.Arrays; import java.util.Scanner; public class IntersectionUnion { public static String intersect_or_union_fcn(int[] v1, int[] v2, int[] v3) { // first search v3 elements in v1 boolean containsAllInV1 = true; for (int num : v3) { boolean flag = false; for (int numv1 : v1) { if (num == numv1) { flag = true; break; } } if (!flag) { containsAllInV1 = false; break; } } // second search v3 elements in v2 boolean containsAllInV2 = true; for (int num : v3) { boolean flag = false; for (int numv2 : v2) { if (num == numv2) { flag = true; break; } } if (!flag) { containsAllInV2 = false; break; } } // when each element in v3 found in both v1 and v2 if (containsAllInV1 && containsAllInV2) return "v3 is the intersection of v1 and v2"; // when its not an intersection int[] unionArray = new int[v1.length+v2.length]; System.arraycopy(v1,0,unionArray,0,v1.length); System.arraycopy(v2,0,unionArray,v1.length,v2.length); //System.out.println(Arrays.toString(unionArray)); for(int num: v3){ boolean found = false; for(int numUnion: unionArray){ if(num==numUnion){ found=true; break; } } if(!found)return "v3 is neither the intersection nor the union of v1 and v2"; } return "v3 is the union of v1 and v2"; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Vector 1"); int v1[] = getVector(scanner); System.out.println("Vector 2"); int v2[] = getVector(scanner); System.out.println("Vector 3"); int v3[] = getVector(scanner); System.out.println("Result = " + intersect_or_union_fcn(v1, v2, v3)); } public static int[] getVector(Scanner scanner) { System.out.print("Input number of elements in the vector: "); int size = scanner.nextInt(); int[] vector = new int[size]; for (int i = 0; i < size; i++) { System.out.print("Enter element #" + (i + 1) + ": "); vector[i] = scanner.nextInt(); } return vector; } }