Question

write a java program that will perform the following. Given two finite sets, list all elements...

write a java program that will perform the following.

Given two finite sets, list all elements in the Cartesian product of these two sets.

Given a finite set, list all elements of its power set.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source Code in Java:

class Sets
{
static void cartesianProduct(int a[],int b[]) //method to print cartersian product of aXb
{
int i,j; //variables to loop through the arrays
for(i=0;i<a.length;i++)
{
for(j=0;j<b.length;j++)
System.out.print("("+a[i]+","+b[j]+")");
}
}
static void powerSet(int a[])
{
long size=(long)Math.pow(2,a.length); //calculating number of elements in the power set
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if((i&(1 << j))>0)
System.out.print(a[j]+" ");
}
if(i==0) //if it is the first element, we print null
System.out.print("null");
if(i<size-1)
System.out.print(", ");
}
}
public static void main(String args[])
{
//testing the two functions
int a[]={1,2,3};
int b[]={4,5};
System.out.print("Carterian Product: ");
cartesianProduct(a,b);
System.out.println();
System.out.print("Power Set: ");
powerSet(a);
System.out.println();
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
write a java program that will perform the following. Given two finite sets, list all elements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • u are to write a program in Java that takes two sets of up to 5...

    u are to write a program in Java that takes two sets of up to 5 symbols(letters, numbers or any characters) and prints the cartesian product of the two sets. The program should allow the user to enter the characters for each set and the output should, consist of the name of each set followed by the characters and on the following line print the cartesian product. Example: {1,3,*} X {3,?,D} = {(1,3),(1,?),(1,D),(3,3),(3,?),(3,D,(*,3),(*,?),(*,D)} or {2,3,5,7} X (5,6} = {(2,5),(2,6),(3,5),(3,6),(5,5),(5,6),(7,5),(7,6)}

  • Program has to be in C# language Write a computer program that takes two sets (let...

    Program has to be in C# language Write a computer program that takes two sets (let the user determine the cardinality of each set and enter them manually) and calculates the following operations: Union Intersection Difference (set1 - set2) Cartesian product (set2 X set1) Check whether set2 is a subset of set1 or set1 is a subset of set2 Find the powerset of set1 and print out its elements and cardinality

  • need java code. (d) is cartesian product rule. Design a program to let user enter two...

    need java code. (d) is cartesian product rule. 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 a set), so we have two sets A and B....

  • Write a java calculator program that perform the following operations: 1. function to Addition of two...

    Write a java calculator program that perform the following operations: 1. function to Addition of two numbers 2. function to Subtraction of two numbers 3. function to Multiplication of two numbers 4. function to Division of two numbers 5. function to Modulus (a % b) 6. function to Power (a b ) 7. function to Square root of () 8. function to Factorial of a number (n!) 9. function to Log(n) 10. function to Sin(x) 11. function to Absolute value...

  • Write Java code (do not use Java libraries) to perform the following on a linked list...

    Write Java code (do not use Java libraries) to perform the following on a linked list of integers: prepend a new value

  • Write a Java program to remove the duplicate elements of a given array and return the...

    Write a Java program to remove the duplicate elements of a given array and return the new length of the array. Sample array: [20, 20, 32, 76, 30, 40, 50, 50, 52] After removing the duplicate elements the program should return 6 as the new length of the array. Out put Original array length: 9 Array elements are: 20 20 32 76 30 40 50 50 52 The new length of the array is: 7

  • What is the cardinality of each of the following sets '? (i.e., finite, countably infinite, or...

    What is the cardinality of each of the following sets '? (i.e., finite, countably infinite, or uncountably infinite) a. The set of all possible Java programs b.The set of all finite strings over the alphabet 10,1,2) c.iO, N, Q. R) d. R-Q

  • 6. Given a finite set A, denote IA] as a nurnber of elements in A. Let f : X → Y be a function wi...

    6. Given a finite set A, denote IA] as a nurnber of elements in A. Let f : X → Y be a function with |XI, Yl< oo, i.e. X, Y are finite sets. Prove the following statements a) IXIS IYİ if f is injective. b) IY1S 1X1 if f is surjective. 6. Given a finite set A, denote IA] as a nurnber of elements in A. Let f : X → Y be a function with |XI, Yl

  • JAVA Program

    1. Write a java program to find the sum of the elements on the diagonal of a matrix. 2. Write a java program to add two matrices. 3. Write a java program to find a given value in a matrix. 4. Write a java program to multiply two matrices. 5. Write a java program to find the transpose of a matrix.

  • java program: Write a program which displays the following list on the screen and asks the...

    java program: Write a program which displays the following list on the screen and asks the user to enter either 1 or 2 and perform one of the operations based on the user’s input. If the user enters any other character other than 1 or 2 then display “wrong choice”. LIST OF OPERATIONS 1. Buzz Number                      2. Consecutive Odd numbers Note: A BUZZ number is a number which either ends with 7 or is divisible by 7. Sample input 27...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT