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 Im 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:
Enter four INTEGER elements in Set B each separated by a space:
Corresponding output:
Set A
Set B
Intersection of Set A and Set B
Union of Set A and Set B
Another example:
Enter four INTEGER elements in Set A each separated by a space:
Enter four INTEGER elements in Set B each separated by a space:
Corresponding output:
Set A
Set B
Intersection of Set A and Set B
Union of Set A and Set B
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 mainString 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 integers into setA:
Scanner inputMachine new ScannerSystemin;
System.out.printEnter four INTEGER elements for Set A each separated by a
"space: ;
for i ; i ; i allow only integers; requires exactly integers
setAInput inputMachine.nextInt;
sA setAInput;
setA.addsA;
enter integers into setB:
System.out.printEnter four INTEGER elements for Set B each separated by a
"space: ;
TYPE YOUR CODE HERE, similar to setA above
for i ; i ; i again, only integers, exactly integers
setBInput inputMachine.nextInt;
sB setBInput;
setB.addsB;
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 HashSetsetA;
HashSet unionOfSets new HashSetsetA;
intersectionOfSets retainAllsetB; find intersection of sets A and B
TYPE YOUR CODE HERE PRINT THE INTERSECTION, which is intersectionOfSets
unionOfSets.addAllsetB; 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
