Hey guys, I need some help with this code I have. I'm running into several issues at the moment.
1 - I don't know what bitwise operators to use for finding the difference of A and B
2 - When the Union and Symmetric Difference calculate, the outputs aren't displaying 0 (I think it has something to do with the printSet at the bottom but I'm not sure
import static java.lang.Math.*;
import java.util.Scanner;
public class Assignment_1
{
public static void main(String args[])
{
// Universal Set = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
int Set_A = 0;
int Set_B = 0;
int Intersection_Set = 0;
int Union_Set = 0;
int Compliment_Set = 0;
int Difference_Set = 0;
int Symmetric_Difference_Set = 0;
int Element_Limit = 0;
int[] Set = {0,0,0,0,0,0,0,0,0,0};
Scanner in = new Scanner (System.in);
//Reads user input as the max number of elements in set A
System.out.print("Enter number of elements for Set A: ");
Element_Limit = in.nextInt();
System.out.print("Enter Set A (Separate elements with spaces): ");
//Populates the Set array
for (int i = 0; i < Element_Limit; i++)
{
Set[i] = in.nextInt();
}
//Populates set A
for (int i = 0; i < Element_Limit; i++)
{
Set_A |= (int)pow(2.0, (Set[i] - 1));
}
//Reads user input as the max number of elements in set A
System.out.print("Enter number of elements for Set B: ");
Element_Limit = in.nextInt();
System.out.print("Enter Set B (Seperate elements with spaces): ");
for (int i = 0; i < Element_Limit; i++)
{
Set[i] = in.nextInt();
}
for (int i = 0; i < Element_Limit; i++) // Populates Set
{
Set_B |= (int)pow(2.0, (Set[i] - 1));
}
Compliment_Set = ~Set_A; // Computes the compliment set A
Union_Set = Set_A | Set_B; // Computes the union of set A and B
Intersection_Set = Set_A & Set_B; // Computes the intersection of set A and B
Difference_Set = Set_A - Set_B; // Computes the difference of set A and B
Symmetric_Difference_Set = Set_A ^ Set_B; //Computes the symmetric difference of set A and B
System.out.print("Compliment of A: ");
printSet(Compliment_Set);
System.out.println();
System.out.print("Union of A and B: ");
printSet(Union_Set);
System.out.println();
System.out.print("Intersection of A and B: ");
printSet(Intersection_Set);
System.out.println();
System.out.print("Difference of A and B: ");
printSet(Difference_Set);
System.out.println();
System.out.print("Symmetric Difference of A and B: ");
printSet(Symmetric_Difference_Set);
System.out.println();
}
static void printSet(int set)
{
int mask = 1;
System.out.print("{");
for(int i = 1; i <= 10; i++)
{
if((mask & set) == 1)
System.out.print(i + " ");
set = set >> 1;
}
System.out.print("}");
}
}