I really need help with the code for this in Java.
Set operations using iterators: implement a program that creates the sets below (as ArrayLists of
Integers), and then finds the result of the operations below. You only need a single .java file with
the main routine; you do not have to put each set operation in its own method. Do NOT use the
toArray( ) method; you will only need the add( ), addAll( ), contains( ), iterator( ), isEmpty( ),
and remove( ) methods defined for the ArrayList. Recall that setscannot have duplicate entries; it
is up to you to enforce this! (Note: in Java a Set is an interface, but it can also be a class, as all
interfaces are also classes!) Sets:
U = {1,2,3,4,5,6,7,8,9,10} (universe set)
E = {2,4,6,8,10}
O = {1,3,5,7,9}
A = {3,4,5}
B = {5,6,7}
C = { } (empty set)
Operations: find
A union B
A intersect B
A complement
O minus B
E minus A
A symmetric difference B (the symmetric difference of 2 sets is the elements in either, but not in
their intersection)
Subsets: write a method that takes two sets, determines if the first is a subset of the second, and
returns true or false. Then answer these questions (by writing code): is C a subset of U; is E and
subset of U; and, is A a subset of B?
Solution
//code has been tested on eclipse
import java.util.ArrayList;
public class HelloWorld{
public static boolean isSubset(ArrayList A, ArrayList B) { //method to //check subset
return B.containsAll(A);
}
public static void main(String []args){ //main method
ArrayList U=new ArrayList(); //Arraylist for storing elements of union
U.add(1); U.add(2);U.add(3); U.add(4); U.add(5); U.add(6); U.add(7); U.add(8); U.add(9);
U.add(10);//entering //elements of union set
ArrayList E=new ArrayList(); //Arraylist for storing elements of set E
E.add(2); E.add(4); E.add(6); E.add(8); E.add(10); //entering elements of set E
ArrayList O=new ArrayList(); //Arraylist for storing elements of set O
O.add(1);O.add(3);O.add(5);O.add(7);O.add(9); //entering elements of set O
ArrayList A=new ArrayList(); //Arraylist for storing elements of set A
A.add(3); A.add(4); A.add(5); //entering elements of set A
ArrayList B=new ArrayList(); //Arraylist for storing elements of set B
B.add(5);B.add(6);B.add(7); //entering elements of set B
ArrayList C=new ArrayList(); //Arraylist for storing elements of set C
ArrayList UnionA_B=new ArrayList(); //Arraylist for storing elements of Union of A and B
UnionA_B.addAll(A); //Union operation using Addall
for(Integer x:B) //loop to find union of A and B
{
if(!(A.contains(x)))
UnionA_B.add(x); //add elements of union of A and B
}
System.out.println(\"Union of set A and B \"); //printing elements of union A and B
for(Integer x:UnionA_B)
System.out.println(x+\" \");
ArrayList IntsctA_B=new ArrayList(); //Arraylist for storing elements of //Intersection of A and
B
for(Integer x:A){ //loop to find Intersection of A and B
if(B.contains(x))
IntsctA_B.add(x); //add elements of Intersection of A and B
}
System.out.println(\"Intersection.
Separation of Lanthanides/ Lanthanides and Actinides
I really need help with the code for this in Java.Set operations u.pdf
1. I really need help with the code for this in Java.
Set operations using iterators: implement a program that creates the sets below (as ArrayLists of
Integers), and then finds the result of the operations below. You only need a single .java file with
the main routine; you do not have to put each set operation in its own method. Do NOT use the
toArray( ) method; you will only need the add( ), addAll( ), contains( ), iterator( ), isEmpty( ),
and remove( ) methods defined for the ArrayList. Recall that setscannot have duplicate entries; it
is up to you to enforce this! (Note: in Java a Set is an interface, but it can also be a class, as all
interfaces are also classes!) Sets:
U = {1,2,3,4,5,6,7,8,9,10} (universe set)
E = {2,4,6,8,10}
O = {1,3,5,7,9}
A = {3,4,5}
B = {5,6,7}
C = { } (empty set)
Operations: find
A union B
A intersect B
A complement
O minus B
E minus A
A symmetric difference B (the symmetric difference of 2 sets is the elements in either, but not in
their intersection)
Subsets: write a method that takes two sets, determines if the first is a subset of the second, and
returns true or false. Then answer these questions (by writing code): is C a subset of U; is E and
subset of U; and, is A a subset of B?
Solution
//code has been tested on eclipse
import java.util.ArrayList;
public class HelloWorld{
public static boolean isSubset(ArrayList A, ArrayList B) { //method to //check subset
return B.containsAll(A);
}
2. public static void main(String []args){ //main method
ArrayList U=new ArrayList(); //Arraylist for storing elements of union
U.add(1); U.add(2);U.add(3); U.add(4); U.add(5); U.add(6); U.add(7); U.add(8); U.add(9);
U.add(10);//entering //elements of union set
ArrayList E=new ArrayList(); //Arraylist for storing elements of set E
E.add(2); E.add(4); E.add(6); E.add(8); E.add(10); //entering elements of set E
ArrayList O=new ArrayList(); //Arraylist for storing elements of set O
O.add(1);O.add(3);O.add(5);O.add(7);O.add(9); //entering elements of set O
ArrayList A=new ArrayList(); //Arraylist for storing elements of set A
A.add(3); A.add(4); A.add(5); //entering elements of set A
ArrayList B=new ArrayList(); //Arraylist for storing elements of set B
B.add(5);B.add(6);B.add(7); //entering elements of set B
ArrayList C=new ArrayList(); //Arraylist for storing elements of set C
ArrayList UnionA_B=new ArrayList(); //Arraylist for storing elements of Union of A and B
UnionA_B.addAll(A); //Union operation using Addall
for(Integer x:B) //loop to find union of A and B
{
if(!(A.contains(x)))
UnionA_B.add(x); //add elements of union of A and B
}
System.out.println("Union of set A and B "); //printing elements of union A and B
for(Integer x:UnionA_B)
System.out.println(x+" ");
ArrayList IntsctA_B=new ArrayList(); //Arraylist for storing elements of //Intersection of A and
B
for(Integer x:A){ //loop to find Intersection of A and B
if(B.contains(x))
IntsctA_B.add(x); //add elements of Intersection of A and B
}
System.out.println("Intersection of set A and B "); //printing elements of Intersection A and B
for(Integer x:IntsctA_B)
System.out.println(x+" ");
3. ArrayList Acomplmnt=new ArrayList(); //Arraylist for storing elements of Complement //of A
for(Integer x:U){ //loop to find Complement of A
if(!(A.contains(x)))
Acomplmnt.add(x); //add elements of Complement of A
}
System.out.println("Complement of set A "); //printing elements of Complement A
for(Integer x:Acomplmnt)
System.out.println(x+" ");
ArrayList OminusB=new ArrayList(); //Arraylist for storing elements of O minus B
OminusB.addAll(O);
for(Integer x:O){ //loop to find O minus B
if(B.contains(x))
OminusB.remove(x); //remove elements of b from O,i.e OminusB
}
System.out.println("O minus B "); //printing elements of O minus B
for(Integer x: OminusB)
System.out.println(x+" ");
ArrayList EminusA=new ArrayList(); //Similarly for operation E minus A
EminusA.addAll(E);
for(Integer x:E){
if(A.contains(x))
EminusA.remove(x);
}
System.out.println("E minus A ");
for(Integer x: EminusA)
System.out.println(x+" ");
ArrayList AsymdfrB=new ArrayList(); //ArrayList for storing symmetric //difference of A and
B
UnionA_B.removeAll(IntsctA_B);
4. System.out.println("A symmetric difference B");
for(Integer x: UnionA_B) //printing elements of symmetric difference of A and B
System.out.println(x+" ");
System.out.println("Is C a subset of U : " + isSubset(C,U)); //is C a subset of U
System.out.println("Is E a subset of U : " + isSubset(E,U)); //is E a subset of U
System.out.println("Is A a subset of B : " + isSubset(A,B)); //is A a subset of B
}
}
***********OUTPUT*********
Union of set A and B
3
4
5
6
7
Intersection of set A and B
5
Complement of set A
1
2
6
7
8
9
10
O minus B
1
3
9
E minus A
5. 2
6
8
10
A symmetric difference B
3
4
6
7
Is C a subset of U : true
Is E a subset of U : true
Is A a subset of B : false
***********OUTPUT*********
Note:I have used arraylist and functions like add,addAll,contains,remove and had not make
different methods for each operations,please let me know in case of any doubt,Thanks.