Create a Java program that will ask the user to enter numbers for two different
ID: 3564417 • Letter: C
Question
Create a Java program that will ask the user to enter numbers for two different sets and find the union, the intersection, and the difference of both sets. The implementation of ArrayList is needed for this program.
Requirements:
Create a generic Set class that have (generic type T) that contains:
-A remove method (that removes a value) (ignore duplicates)
-An add method (that adds a new value) (ignore duplicates)
-A non static method that returns true if there is a value in the set and false if it is not there
-A static intersection method (returns the intersection of the two sets)
-A static union method (returns the union of the 2 sets)
-A static difference method (returns the difference of the 2 sets)
You will also need a main method that asks for the input of values/numbers of two different sets from the user and displays the intersection, the union, and the difference of the two sets.
Explanation / Answer
import java.util.ArrayList;
import java.util.Random;
public class Set<T>
{
ArrayList<T> al=new ArrayList<T>();
public void remove(T t){
al.remove(t);
}
public void add(T t){
al.add(t);
}
public boolean isPresent(T t){
return al.contains(t);
}
public static ArrayList intersection(Set al1,Set al2){
ArrayList alist=new ArrayList();
ArrayList a1=(ArrayList)al1.al.clone();
ArrayList a2=(ArrayList)al2.al.clone();
for (int i = 0; i < al1.al.size(); i++) {
if(a2.contains(al1.al.get(i))){
alist.add(al1.al.get(i));
a2.remove(al1.al.get(i));
a1.remove(al1.al.get(i));
}
}
return alist;
}
public static ArrayList union(Set al1,Set al2){
ArrayList alist=new ArrayList();
ArrayList a1=(ArrayList)al1.al.clone();
ArrayList a2=(ArrayList)al2.al.clone();
for (int i = 0; i < al1.al.size(); i++) {
if(a2.contains(al1.al.get(i))) {
alist.add(al1.al.get(i));
a1.remove(al1.al.get(i));
a2.remove(al1.al.get(i));
}
}
for (int i = 0; i < a1.size(); i++) alist.add(a1.get(i));
for (int i = 0; i < a2.size(); i++) alist.add(a2.get(i));
return alist;
}
public static ArrayList difference(Set al1,Set al2)
{
ArrayList alist=new ArrayList();
ArrayList a1=(ArrayList)al1.al.clone();
ArrayList a2=(ArrayList)al2.al.clone();
for (int i = 0; i < al1.al.size(); i++) {
if(a2.contains(al1.al.get(i))) {
a1.remove(al1.al.get(i));
a2.remove(al1.al.get(i));
}
}
for (int i = 0; i < a1.size(); i++)alist.add(a1.get(i));
for (int i = 0; i < a2.size(); i++)alist.add(a2.get(i));
return alist;
}
public static void main(String[] args)
{
Random r=new Random();
Set<Integer> s1=new Set<Integer>();
Set<Integer> s2=new Set<Integer>();
int k;
for (int i = 0; i < 10; i++){
k=r.nextInt(10);
s1.add(new Integer(k));
}
for (int i = 0; i < 15; i++){
k=r.nextInt(10);
s2.add(new Integer(k));
}
System.out.println("Set1: "+s1.al);
System.out.println("Set2: "+s2.al);
System.out.println("Union: "+Set.union(s1,s2)+" Size: "+Set.union(s1,s2).size());
System.out.println("Intersection: "+Set.intersection(s1,s2)+" Size: "+Set.intersection(s1,s2).size());
System.out.println("Difference: "+Set.difference(s1,s2)+" Size: "+Set.difference(s1,s2).size());
}
}