Question

In: Computer Science

Java Write a method intersect_or_union_fcn() that gets vectors of type integer v1, v2, and v3 and...

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).

Solutions

Expert Solution

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;
    }
}


Related Solutions

if {v1,v2,v3} is a linearly independent set of vectors, then {v1,v2,v3,v4} is too.
if {v1,v2,v3} is a linearly independent set of vectors, then {v1,v2,v3,v4} is too.
v1=[0,1,4] v2=[-4,-5,7] v3=[14,10,8] b=[16,18,19]. Let v1,v2, and v3 be three nonzero vectors in R3. Suppose v2...
v1=[0,1,4] v2=[-4,-5,7] v3=[14,10,8] b=[16,18,19]. Let v1,v2, and v3 be three nonzero vectors in R3. Suppose v2 is not a scalar multiple of either v1 or v3 and v3 is not a scalar multiple of either v1 or v2. Does it follow that every vector in R3 is in span{v1,v2,v3}?
Are the vectors v1 = (1 , 2, 3), v2 = (2, 4, 6), and v3...
Are the vectors v1 = (1 , 2, 3), v2 = (2, 4, 6), and v3 = (1, 1, 3) linearly independent or dependent? Since v2 is a scalar multiple of v1, both v1 and v2 are linearly dependent, but what does that say about the linear dependence of the three vectors as a whole?
consider the vectors: v1=(1,1,1) v2=(2,-1,1) v3=(3,0,2) v4=(6,0,4) a)find the dimension and a basis W=Span(v1,v2,v3,v4) b) Does...
consider the vectors: v1=(1,1,1) v2=(2,-1,1) v3=(3,0,2) v4=(6,0,4) a)find the dimension and a basis W=Span(v1,v2,v3,v4) b) Does the vector v=(3,3,1) belong to W. Justify your answer c) Is it true that W=Span(v3,v4)? Justify your answer
V1 V2 V3 V4 V1 1.0 V2 .27 1.0 V3 -.13 .65 1.0 V4 .20 -.15...
V1 V2 V3 V4 V1 1.0 V2 .27 1.0 V3 -.13 .65 1.0 V4 .20 -.15 -.72 1.0 IN THIS EXERCISE, YOU WILL SEE A CORRELATION MATRIX. EXAMINE THE MATRIX AND ANSWER THE QUESTIONS THAT FOLLOW. 1. Which two variables have the strongest (largest) relationship? 2. Which two variables have the weakest (smallest) relationship? 3. Which two variables have the strongest positive relationship? 4. which two variables have the stronger negative relationship? 5. Which two variables have the weakest positive...
#1 Let H= Span{v1,v2,v3,v4}. For each of the following sets of vectors determine whether H is...
#1 Let H= Span{v1,v2,v3,v4}. For each of the following sets of vectors determine whether H is a line, plane ,or R3. Justify your answers. (a)v1= (1,2,−2),v2= (7,−7,−7),v3= (16,−12,−16),v4= (0,−3,−3) (b)v1= (2,2,2),v2= (6,6,5),v3= (−16,−16,−14),v4= (28,28,24) (c)v1= (−1,3,−3),v2= (0,0,0),v3= (−2,6,−6),v4= (−3,9,−9) #2 Plot the linesL1: x= t[4−1] and L2: x= [−4−2] + t[4−1] using their vector forms. If[12k]is onL2. What is the value of k?
Let v1 = [-0.5 , v2 = [0.5 , and v3 = [-0.5 -0.5 -0.5   ...
Let v1 = [-0.5 , v2 = [0.5 , and v3 = [-0.5 -0.5 -0.5    0.5 0.5    0.5    0.5 -0.5]    0.5] 0.5] Find a vector v4 in R4 such that the vectors v1, v2, v3, and v4 are orthonormal.
If {v1, v2, v3, v4} is a linearly-independent subset of a vector space V over the...
If {v1, v2, v3, v4} is a linearly-independent subset of a vector space V over the field Q, is the set {3v1 + 2v2 + v3 + v4, 2v1 + 5v2, 3v3 + 2v4, 3v1 + 4v2 + 2v3 + 3v4} linearly independent as well?
a, The vectors v1 = < 0, 2, 1 >, v2 = < 1, 1, 1...
a, The vectors v1 = < 0, 2, 1 >, v2 = < 1, 1, 1 > , v3 = < 1, 2, 3 > , v4 = < -2, -4, 2 > and v5 = < 3, -2, 2 > generate R^3 (you can assume this). Find a subset of {v1, v2, v3, v4, v5} that forms a basis for R^3. b. v1 = < 1, 0, 0 > , v2 = < 1, 1, 0 > and v3...
a. Using KVL, KCL, and Ohm's Law, find v1, v2, v3, i1, i2, andi3. Hint:...
For the circuit below, a. Using KVL, KCL, and Ohm's Law, find v1, v2, v3, i1, i2, and i3. Hint: solve for the current first by using 1 KCL and 2 KVL equations.b. Find the power associate with each circuit element (passive sign convention, include sign in the answer) and verify that total power is conserved
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT