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
  • Using Java programming, The Set interface in the API has the methods addAll(Collection<? exte...

    Using Java programming, The Set interface in the API has the methods addAll(Collection<? extends E> c) and retainAll(Collections<? > c), which acts like union and intersection respectively. However, these modify the set it is called on. This means after set1.addAll(set2) or set1.retainsAll(set2) is executed, set1 will be the union or intersection of the two sets, respectively. Choose ONE of the implementations below Ordered lists Binary search trees Hash tables Add a method in that implementation that returns the union of...

  • Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet...

    Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet object holds integers in the range 0-100 Represented by an array of booleans, such that array element a[i] is set to true if integer i is in the set, and false otherwise Create these constructors and methods for the class IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()...

  • I can't get my code to work on xcode and give me an output. #include <conio.h> #include <cstdlib> #incl...

    I can't get my code to work on xcode and give me an output. #include <conio.h> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; // So "std::cout" may be abbreviated to "cout" //Declare global arrays int dummy1[10]; int dummy2[10]; int dummy3[10]; int universalSet[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //Function to return statement "Empty thread" when the resultant set is empty string isEmpty(int arr[]) { string...

  • Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding...

    Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding functionality Set A contains Set B if every element in Set B is also in Set A. We will compare three ways to determine whether Set A contains Set B. Approach 1: Write code in the main method in a test class Add code to the main of ArraySetTester to create SetA and SetB, fill them with data and write a contains operation that...

  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

  • The attached document is a C++ source code, INTERFACE__Class{aSet}.cpp, which contains an interface for class aSet and an implementation section which is largely left empty, so that students could fill in the missing code, where it is written // C++ code

    /* FILE NAME: Class{aSet}.cpp FUNCTION: A template class for a set in C++. It implements all the set operations, except set compliment:  For any two sets, S1 and S2 and an element, e  A. Operations which result in a new set:  (1) S1 + S2 is the union of S1 and S2 (2) S1 - S2 is the set difference of S1 and S2, S1 - S2 (3) S1 * S2 is the set intersection of S1 and S2, S1 * S2 (4) S1 + e (or e +...

  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

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