In: Computer Science
Using Java, design a program to let user enter two lists of numbers within the range [0, 9] from keyboard, you could either use flag to denote end of the list or ask user to enter the list size first and then enter the list numbers. Each of these two lists represents a set (keep in mind that duplicate elements are not allowed in the same set but are allowed between sets), so we have two sets A and B. Make sure each type of calculation is in a separate method.
Suppose the universal set is U = {0,1,2,···,9}. Write a program to implement the following functions:
(1) Print out A∪B.
(2) Print out A∩B.
(3) Print out B'
(4) Print out (A∩B)×A.
java code:
package setdriver;
import java.util.Scanner;
public class SetDriver {
public static void main(String[] args) {
int[] A=new int[10];
int[] B=new int[10];
int[] AUB=new int[10];
int[] AnB=new int[10];
int[] BC=new int[10];
int[] AM=new int[10];
int n=0,m=0;
Scanner sc=new Scanner(System.in);
A=getElements(sc);
n=getLength(A);
//print A set
System.out.print("\nSet A: ");
printElements(A,n);
System.out.println("\n================================================");
//Set B
B=getElements(sc);
m=getLength(B);
//print B set
System.out.print("\nSet B: ");
printElements(B,m);
System.out.println("\n================================================");
//call union function
System.out.print("Union of set A and B: ");
union(A,n,B,m);
//call intersection function
System.out.print("\nIntersection of set A and B: ");
intersetion(A,n,B,m);
//call complement function
System.out.print("\nSet B's complement: ");
complement(B,m);
//call production function
System.out.print("\nProduction of (intersection of set A and B) and A: ");
production(A,n,B,m);
}
//get elements from user
public static int[] getElements(Scanner sc)
{
int n=0;
int[] A=new int[10];
System.out.println("Please enter unique element and enter -99 to end the entering:");
while(n<10)
{
A[n]=sc.nextInt();
n++;
if(A[n-1]==-99){
return A;
}
for(int i=0;i<n-1;i++){
if(A[i]==A[n-1]){
System.out.println("Element already exists, please reenter");
n--;
}
}
}
return A;
}
//returnt the set length
private static int getLength(int[] A) {
for(int i=0;i<A.length;i++)
{
if(A[i]==-99)
return i;
}
return 10;
}
//print the set
public static void printElements(int[] A,int n)
{
System.out.print("{");
for(int i=0;i<n;i++)
System.out.print(A[i]+" ");
System.out.print("}");
}
//union function
public static void union(int[] A,int n, int[] B,int m)
{
int i=0,j=0,k=0;
int[] U=new int[50];
for(i=0;i<n;i++) //add A set
{
U[k]=A[i];
k++;
}
//add B set elements
for(j=0;j<m;j++)
{
U[k]=B[j];
k++;
for(int x=0;x<k-1;x++)
{
if(U[x]==B[j]) //ignore the same elements
k--;
}
}
printElements(U,k); //call print function
}
//instersection of A and B
public static void intersetion(int A[],int n, int B[],int m)
{
int[] N=new int[n];
int k=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(A[i]==B[j]){
N[k]=A[i];
k++;
}
}
}
printElements(N,k); //call print function
}
//complementof B set
private static void complement(int[] B, int m) {
int[] C=new int[11];
int k=0;
for(int i=0;i<10;i++)
{
C[k]=i;
k++;
for(int j=0;j<m;j++)
{
if(C[k-1]==B[j])
k--;
}
}
printElements(C,k); //call print function
}
//production
private static void production(int[] A, int n, int[] B, int m) {
int[] N=new int[n];
int k=0;
//computethe intersection
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(A[i]==B[j]){
N[k]=A[i];
k++;
}
}
}
//compute and print the production
System.out.print("{");
for(int i=0;i<k;i++){
for(int j=0;j<n;j++)
{
System.out.print("("+N[i]+","+A[j]+")");
}
if(i<k-1)
System.out.print(",");
}
System.out.print("}");
}
}
output:
plzz rate me my answer...