public class elements { public static void main(String args[]) { Scanner sc = new Scanner(System.in); // Creating two sets Set set1 = new HashSet(); Set set2 = new HashSet(); System.out.println("Enter the element of set A [max-5 elements]: "); for (int i = 0; i < 5; i++) { int num = sc.nextInt(); set1.add(num); } System.out.println("Enter the element of set B [max-5 elements]: "); for (int i = 0; i < 5; i++) { int num1 = sc.nextInt(); set2.add(num1); } // creating a menu of opeartions System.out.println("a. Identify elements of set"); System.out.println("b. Check Union"); System.out.println("c. Check Difference"); System.out.println("d. Check Intersection"); System.out.println("e. Check Subset"); System.out.println("f. If set A and B are empty can't perform opeartion b to e"); System.out.print("Enter you choice: "); // taking choice input int ch = sc.next().charAt(0); while (ch != 'f') { switch (ch) { // finding the set A and B elements case 'a': if (set1.size() == 0 && set2.size() == 0) { System.out.println("Set A and B are empty."); } System.out.println("\nSet A = " + set1); System.out.println("\nSet B = " + set2); break; // finding the Union of set case 'b': Set union_data = new HashSet(set1); union_data.addAll(set2); System.out.println("Union of set A and B is: "); System.out.println(union_data); break; case 'c': // Finding Difference of set1 and set2 Set difference_data = new HashSet(set1); difference_data.removeAll(set2); System.out.println("Difference of set A and B is: "); System.out.println(difference_data); break; case 'd': // Finding Intersection of set1 and set2 Set intersection_data = new HashSet(set1); intersection_data.retainAll(set2); System.out.println("Intersection of set A and B is: "); System.out.println(intersection_data); break; // finding subset case 'e': System.out.print("Enter a number: "); int number = sc.nextInt(); if (set1.contains(number) && set2.contains(number)) { System.out.println(number + " is subset of A and B"); } else { System.out.println(number + " is not subset of A and B"); } break; default: System.out.println("Enter a valid input."); break; } System.out.println("\na. Identify elements of set"); System.out.println("b. Check Union"); System.out.println("c. Check Difference"); System.out.println("d. Check Intersection"); System.out.println("e. Check Subset"); System.out.println("f. If set A and B are empty can't perform opeartion b to e"); System.out.print("Enter you choice: "); ch = sc.next().charAt(0); } if (ch == 'f') { System.out.println("set A and B is empty and you can't perform operation b to e."); } } }
INSTRUCTION: make this a flowchart
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class elements {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Creating two sets
Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();
System.out.println("Enter the element of set A [max-5 elements]: ");
for (int i = 0; i < 5; i++) {
int num = sc.nextInt();
set1.add(num);
}
System.out.println("Enter the element of set B [max-5 elements]: ");
for (int i = 0; i < 5; i++) {
int num1 = sc.nextInt();
set2.add(num1);
}
// creating a menu of opeartions
System.out.println("a. Identify elements of set");
System.out.println("b. Check Union");
System.out.println("c. Check Difference");
System.out.println("d. Check Intersection");
System.out.println("e. Check Subset");
System.out.println("f. If set A and B are empty can't perform opeartion b to e");
System.out.print("Enter you choice: ");
// taking choice input
int ch = sc.next().charAt(0);
while (ch != 'f') {
switch (ch) {
// finding the set A and B elements
case 'a':
if (set1.size() == 0 && set2.size() == 0) {
System.out.println("Set A and B are empty.");
}
System.out.println("\nSet A = " + set1);
System.out.println("\nSet B = " + set2);
break;
// finding the Union of set
case 'b':
Set<Integer> union_data = new HashSet<Integer>(set1);
union_data.addAll(set2);
System.out.println("Union of set A and B is: ");
System.out.println(union_data);
break;
case 'c': // Finding Difference of set1 and set2
Set<Integer> difference_data = new HashSet<Integer>(set1);
difference_data.removeAll(set2);
System.out.println("Difference of set A and B is: ");
System.out.println(difference_data);
break;
case 'd': // Finding Intersection of set1 and set2
Set<Integer> intersection_data = new HashSet<Integer>(set1);
intersection_data.retainAll(set2);
System.out.println("Intersection of set A and B is: ");
System.out.println(intersection_data);
break;
// finding subset
case 'e':
System.out.print("Enter a number: ");
int number = sc.nextInt();
if (set1.contains(number) && set2.contains(number)) {
System.out.println(number + " is subset of A and B");
} else {
System.out.println(number + " is not subset of A and B");
}
break;
default:
System.out.println("Enter a valid input.");
break;
}
System.out.println("\na. Identify elements of set");
System.out.println("b. Check Union");
System.out.println("c. Check Difference");
System.out.println("d. Check Intersection");
System.out.println("e. Check Subset");
System.out.println("f. If set A and B are empty can't perform opeartion b to e");
System.out.print("Enter you choice: ");
ch = sc.next().charAt(0);
}
if (ch == 'f') {
System.out.println("set A and B is empty and you can't perform operation b to e.");
}
}
}
Step by step
Solved in 2 steps with 1 images