# Untitled

unknown
plain_text
19 days ago
4.7 kB
1
Indexable
Never
```package ccs107;

import java.util.Scanner;

public class labact1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Exercise 1: Creating and Printing Sets
int[] setA = {1, 2, 3};
int[] setB = {2, 3, 4};

System.out.println("Exercise 1: Creating and Printing Sets");
System.out.print("Set A: ");
displaySet(setA);

System.out.print("Set B: ");
displaySet(setB);

// Exercise 2: Union of Two Sets
System.out.println("\nExercise 2: Union of Two Sets");
int[] unionResult = union(setA, setB);
System.out.print("Union of Set A and Set B: ");
displaySet(unionResult);

// Exercise 3: Intersection of Two Sets
System.out.println("\nExercise 3: Intersection of Two Sets");
int[] intersectionResult = intersection(setA, setB);
System.out.print("Intersection of Set A and Set B: ");
displaySet(intersectionResult);

// Exercise 4: Difference Between Two Sets
System.out.println("\nExercise 4: Difference Between Two Sets");
int[] differenceResult = difference(setA, setB);
System.out.print("Difference of Set A and Set B (A - B): ");
displaySet(differenceResult);

// Exercise 5: Subset Check
System.out.println("\nExercise 5: Subset Check");
boolean subsetResult = isSubset(setA, setB);
System.out.println("Is Set A a subset of Set B? " + subsetResult);

// Exercise 6: Power Set
System.out.println("\nExercise 6: Power Set");
System.out.println("Power Set of Set A:");
generatePowerSet(setA);

// Exercise 7: Basic Boolean Expressions
System.out.println("\nExercise 7: Basic Boolean Expressions");
System.out.println("P AND Q: " + (false));
System.out.println("P OR Q: " + (true));
System.out.println("NOT P: " + (false));
}

public static void displaySet(int[] set) {
System.out.print("[");
for (int i = 0; i < set.length; i++) {
System.out.print(set[i]);
if (i < set.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}

public static int[] union(int[] setA, int[] setB) {
int[] resultSet = new int[4]; // We know the union has 4 unique elements
int count = 0;

for (int i : setA) {
resultSet[count++] = i;
}
for (int i : setB) {
boolean isUnique = true;
for (int j : setA) {
if (i == j) {
isUnique = false;
break;
}
}
if (isUnique) {
resultSet[count++] = i;
}
}
return resultSet;
}

public static int[] intersection(int[] setA, int[] setB) {
int[] resultSet = new int[2]; // The intersection has 2 elements
int count = 0;

for (int i : setA) {
for (int j : setB) {
if (i == j) {
resultSet[count++] = i;
}
}
}
return resultSet;
}

public static int[] difference(int[] setA, int[] setB) {
int[] resultSet = new int[1]; // The difference has 1 element
int count = 0;

for (int i : setA) {
boolean isUnique = true;
for (int j : setB) {
if (i == j) {
isUnique = false;
break;
}
}
if (isUnique) {
resultSet[count++] = i;
}
}
return resultSet;
}

public static boolean isSubset(int[] setA, int[] setB) {
for (int i : setA) {
boolean found = false;
for (int j : setB) {
if (i == j) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}

public static void generatePowerSet(int[] set) {
int powerSetSize = 1 << set.length; // 2^n subsets
for (int i = 0; i < powerSetSize; i++) {
System.out.print("{");
for (int j = 0; j < set.length; j++) {
if ((i & (1 << j)) > 0) {
System.out.print(set[j] + " ");
}
}
System.out.println("}");
}
}
}
```