Question: In Java, which I ' m learning simultaneously and need help, please. Assignment: Sets can be combined in different ways to define new sets. The

In Java, which I'm learning simultaneously and need help, please.
Assignment: Sets can be combined in different ways to define new sets. The intersection of set A and set B, denoted Aand read "A intersect B", is the set of all elements that are elements of both A and B. The union of two sets, A and B, denoted A
and read "A union B", is the set of all elements that are elements of A or B.
Example input (the program should prompt the user to input the below values - these are example values only):
Enter four INTEGER elements in Set A, each separated by a space: 1234
Enter four INTEGER elements in Set B, each separated by a space: 3456
Corresponding output:
Set A =[1,2,3,4]
Set B =[3,4,5,6]
Intersection of Set A and Set B =[3,4]
Union of Set A and Set B =[1,2,3,4,5,6]
Another example:
Enter four INTEGER elements in Set A, each separated by a space: 1234
Enter four INTEGER elements in Set B, each separated by a space: 6789
Corresponding output:
Set A =[1,2,3,4]
Set B =[6,7,8,9]
Intersection of Set A and Set B =[]
Union of Set A and Set B =[1,2,3,4,6,7,8,9]
Below is what I have so far. I can get the sets to print, but don't know how to do it so that each set prints as a whole and not one input at a time, that alternates between setA and setB.
import java.util.Scanner;
import java.util.*;
public class Sets {
public static void main(String[] args){
// declare variables for user input:
// TYPE YOUR CODE HERE >
int i;
int sA, sB;
int setAInput, setBInput;
// define setA and setB:
HashSet setA = new HashSet();
HashSet setB = new HashSet();
// enter 4 integers into setA:
Scanner inputMachine = new Scanner(System.in);
System.out.print("Enter four INTEGER elements for Set A, each separated by a "+
"space: ");
for (i =0; i <4; ++i){// allow only 4 integers; requires exactly 4 integers
setAInput = inputMachine.nextInt();
sA = setAInput;
setA.add(sA);
}
// enter 4 integers into setB:
System.out.print("Enter four INTEGER elements for Set B, each separated by a "+
"space: ");
//< TYPE YOUR CODE HERE, similar to setA above >
for (i =0; i <4; ++i){// again, only 4 integers, exactly 4 integers
setBInput = inputMachine.nextInt();
sB = setBInput;
setB.add(sB);
// print contents of sets A and B:
System.out.println ("Set A: "+ setA);
System.out.println ("Set A: "+ setA);
// create copy of setA to retain its structure (use your own variable names):
HashSet intersectionOfSets = new HashSet(setA);
HashSet unionOfSets = new HashSet(setA);
intersectionOfSets .retainAll(setB); // find intersection of sets A and B
//< TYPE YOUR CODE HERE (PRINT THE INTERSECTION, which is intersectionOfSets)>
unionOfSets.addAll(setB); // find union of sets A and B
//< TYPE YOUR CODE HERE (PRINT THE UNION, which is unionOfSets)
}
}
}

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 Programming Questions!