Create a program which is capable of assembling and displaying various sets I wi
ID: 3789854 • Letter: C
Question
Create a program which is capable of assembling and displaying various sets I will provide you with. The program must correctly display the result of operators on sets (such as Union and Intersection), and in addition it must do the following two things: First, it must NOT repeat any values that might appear more than once. So if you Union two sets together that each have the number ‘5’ somewhere in them, then ‘5’ should only appear ONCE in the resulting set. Second, if the resulting thread is empty, your program must indicate the empty thread in some recognizable manner, such as displaying “Empty Thread” or “E” or something similar. Below are the threads you will use. Also assume that the A group comprises ‘reality’ so to speak, the numbers 1-10 being all elements possible, such that the INVERSE of A, is the empty thread. Thus if you were asked to compute the INVERSE of B, it would be equivalent to saying “Set A minus Set B”. For this exercise, we will represent inverse with the ! symbol. (100pts total: 60 for program, 5 per problem)
A = {1,2,3,4,5,6,7,8,9,10}
B = {2,4,6,8,10}
C = {1,3,5,7,9}
D = {1,2,3,5,7}
Problem 1.) A n D
Problem 2.) ( B U C ) n A
Problem 3.) (!C U C) n A
Problem 4.) A – D
Problem 5.) N(!A U ( C U D))
Problem 6.) B n C
Problem 7.) N(B n C)
Problem 8.) A U B U C U D
A reminder, N() indicates the SIZE of the resulting set. So if you have a set such as A = {1,3,7,10} then N(A) = 4. For problems asking for N(), your result should be a single number indicating the resulting set’s size. Finally, a basic hint to help with this assignment: You may find arrays to be useful
Explanation / Answer
Here you go
package com.HackerRank;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class MySet{
public static void main(String args[]){
Set<Integer> A = new HashSet<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));
Set<Integer> B = new HashSet<Integer>(Arrays.asList(2,4,6,8,10));
Set<Integer> C = new HashSet<Integer>(Arrays.asList(1,3,5,7,9));
Set<Integer> D = new HashSet<Integer>(Arrays.asList(1,2,3,5,7));
//Problem1
Set<Integer> p1 = Intersection(A,D);
System.out.println("Result of P1");
printSets(p1);
//Problem2
Set<Integer> temp = Union(B,C);
Set<Integer> p2 = Intersection(temp, A);
System.out.println("Result of P2");
printSets(p2);
//Problem3
temp = Union(C,C);
temp = Minus(A,temp);
Set<Integer> p3 = Intersection(temp,A);
System.out.println("Result of P3");
printSets(p3);
//Problem4
Set<Integer> p4 = Minus(A, D);
System.out.println("Result of P4");
printSets(p4);
//Problem5
temp = Union(C,D);
Set<Integer> temp2 = Minus(A,A);
temp = Union(temp,temp2);
int p5 = Norm(temp);
System.out.println("Result of P5");
System.out.println(p5);
//Problem6
Set<Integer> p6 = Intersection(B, C);
System.out.println("Result of P6");
printSets(p6);
//Problem7
int p7 = Norm(p6);
System.out.println("Result of P7");
System.out.println(p7);
//Problem8
temp = Union(A,B);
temp = Union(temp,C);
Set<Integer> p8 = Union(temp,D);
System.out.println("Result of P8");
printSets(p8);
}
public static Set<Integer> Intersection(Set<Integer> A, Set<Integer> B){
Set<Integer> C = new HashSet<Integer>();
for(Integer x:A){
for(Integer y:B){
if(x==y){
C.add(x);
}
}
}
return C;
}
public static Set<Integer> Union(Set<Integer> A, Set<Integer> B){
Set<Integer> C = new HashSet<Integer>();
for(Integer x:A)
C.add(x);
for(Integer y:B)
C.add(y);
return C;
}
public static Set<Integer> Minus(Set<Integer> A, Set<Integer> B){
Set<Integer> C = new HashSet<Integer>();
for(Integer a:A){
C.add(a);
}
Set<Integer> temp = Intersection(A,B);
for(Integer e:temp){
C.remove(e);
}
return C;
}
public static int Norm(Set<Integer> A){
return A.size();
}
public static void printSets(Set<Integer> A){
if(Norm(A)==0){
System.out.println("{}");
return;
}
for(Integer a:A){
System.out.println(a);
}
return;
}
}
Here's the sample output from my eclipse:
Result of P1
1
2
3
5
7
Result of P2
1
2
3
4
5
6
7
8
9
10
Result of P3
2
4
6
8
10
Result of P4
4
6
8
9
10
Result of P5
6
Result of P6
{}
Result of P7
0
Result of P8
1
2
3
4
5
6
7
8
9
10
Ideone of it, for your reference: http://ideone.com/S22oBF
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.