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
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;
}
}
The question exercises the union, intersection, and the intersection operations of two set of strings (names)....
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...
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 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 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...
/* 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 +...