Question

The question exercises the union, intersection, and the intersection operations of two set of strings (names)....

The question exercises the union, intersection, and the intersection operations of two set of strings (names). For this type of problem, you need to preserve the original sets from being modified by some of the set method. You can used the clone( ) method to copy the sets or simply create another set using the same array contents as the original. You can find an example of the clone() for the LinkedHashSet set in   the provided “skeleton” program (assign8_Q3.java) code to start.

your code should display the following;

set1 = [Greg, Joyce, Janette, Bob, Kevin, Michael]

set2 = [George, Katie, Kevin, Maddie, Ryan]

before AddAll set2[Greg, Joyce, Janette, Bob, Kevin, Michael]

after AddAll set2[Greg, Joyce, Janette, Bob, Kevin, Michael, George, Katie, Maddie, Ryan]

The union of the two sets is [Greg, Joyce, Janette, Bob, Kevin, Michael, George, Katie, Maddie, Ryan]

The difference of the two sets is [Greg, Joyce, Janette, Bob, Michael]

The intersection of the two sets is [Kevin]

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Assign8_Q3.java file:

package Com.Exercise;

import java.util.*;

public class Assign8_Q3 {
public static void main(String[] args) {
     
   String SArray1[] = {"Greg", "Joyce", "Janette", "Bob", "Kevin", "Michael"};
   String SArray2[] = {"George", "Katie", "Kevin", "Maddie", "Ryan"};
  
  
   LinkedHashSet<String> set1 = convertArrayToSet(SArray1);
   LinkedHashSet<String> set2 = convertArrayToSet(SArray2);
  
   // you can use the following to create a copy of set1 or simple create another "set1 "
// LinkedHashSet<String> set1Clone1 = (LinkedHashSet<String>)set1.clone();
//   
  
// write your codes below

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.util.*;

public class Assign8_Q3 {

public static void main(String[] args) {

String SArray1[] = { "Greg", "Joyce", "Janette", "Bob", "Kevin", "Michael" };

String SArray2[] = { "George", "Katie", "Kevin", "Maddie", "Ryan" };

LinkedHashSet<String> set1 = convertArrayToSet(SArray1);

LinkedHashSet<String> set2 = convertArrayToSet(SArray2);

// you can use the following to create a copy of set1 or simple create another

// "set1 "

// LinkedHashSet<String> set1Clone1 = (LinkedHashSet<String>)set1.clone();

//

// write your codes below

System.out.println("set1 = "+set1.toString());

System.out.println("set2 = "+set2.toString());

LinkedHashSet<String> set = (LinkedHashSet<String>)set1.clone();

System.out.println("Before AddAll set2"+set.toString());

set.addAll(set2);

System.out.println("After AddAll set2"+set.toString());

LinkedHashSet<String> unionSet = new LinkedHashSet<String>();

for(String s: set1)

unionSet.add(s);

for(String s: set2)

unionSet.add(s);

LinkedHashSet<String> differenceSet = new LinkedHashSet<String>();

for(String s: set1){

if(!set2.contains(s))

differenceSet.add(s);

}

LinkedHashSet<String> intersectionSet = new LinkedHashSet<String>();

for(String s: set1){

if(set2.contains(s))

intersectionSet.add(s);

}

System.out.println("The union of the two sets is "+unionSet);

System.out.println("The difference of the two sets is "+differenceSet);

System.out.println("The intersection of the two sets is "+intersectionSet);

}

public static LinkedHashSet<String> convertArrayToSet(String[] SArray){

LinkedHashSet<String> set = new LinkedHashSet<String>();

for(String s: SArray){

set.add(s);

}

return set;

}

}

Add a comment
Know the answer?
Add Answer to:
The question exercises the union, intersection, and the intersection operations of two set of strings (names)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT