Question

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. Suppose the universal set is U 0, 1,2,,9). Write a program implementing the following functions: (a) Print out AUB. (b) Print out AnB. (c) Print out B d) Print out (AnB) x A

need java code. (d) is cartesian product rule.

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

import java.util.HashSet;

import java.util.Iterator;

import java.util.Scanner;

import java.util.Set;

public class TestSet {

public static void main(String[] args) {

Set<Integer> U = new HashSet<Integer>();

Set<Integer> A = new HashSet<Integer>();

Set<Integer> B = new HashSet<Integer>();

Set<Integer> union = new HashSet<Integer>();

Set<Integer> BDash = new HashSet<Integer>();

Set<Integer> intersection = new HashSet<Integer>();

Scanner sc= new Scanner(System.in);

for(int i=0;i<10;i++){

U.add(i);

}

BDash.addAll(B);

System.out.println("Enter set A element(Enter -1 to exit){Range: 0 to 9}");

for(;;){

int element = sc.nextInt();

if(element == -1){

break;

}

A.add(element);

}

System.out.println("Enter set B element(Enter -1 to exit){Range: 0 to 9}");

for(;;){

int element = sc.nextInt();

if(element == -1){

break;

}

B.add(element);

}

System.out.print("Value of (A U B) :");

union.addAll(A);

union.addAll(B);

System.out.println(union);

System.out.println("Value of (A intersection B) :");

intersection.addAll(A);

intersection.retainAll(B);

System.out.println(intersection);

System.out.println("Value of B': ");

BDash.addAll(U);

BDash.removeAll(B);

System.out.println(BDash);

System.out.println("Value of (A intersection B)XA :");

System.out.println("{");

Object[] colorObj1 = intersection.toArray();

Object[] colorObj2 = A.toArray();

for(int i=0;i<colorObj1.length;i++){

for(int j=0;j<colorObj2.length;j++){

System.out.println("("+colorObj1[i]+","+colorObj2[j]+")");

}

}

System.out.println("}");

}

}

Add a comment
Know the answer?
Add Answer to:
need java code. (d) is cartesian product rule. Design a program to let user enter two...
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

  • write a java program that prompts a user to enter two different numbers, divide the first...

    write a java program that prompts a user to enter two different numbers, divide the first number by second and prints the result

  • C++ Write a program that prompts the user to enter two positive integers: num1 and num2....

    C++ Write a program that prompts the user to enter two positive integers: num1 and num2. - Validate that num1 is less than num2 and that both numbers are positive. If any of these conditions are not met, allow the user to re-enter num1 and num2 until the input is determined valid. - For all integers from num1 through num2, print the word keyboard if the current integer is divisible by 2 and print the word mouse if the current...

  • Write a Java program that performs these operations Prompt the user to enter a low height...

    Write a Java program that performs these operations Prompt the user to enter a low height in feet (an integer) Prompt the user to enter a high height in feet (an integer) Prompt the user to enter a low weight in pounds (an integer) Prompt the user to enter a high weight in pounds (an integer) Print a table of Body Mass Index (BMI) for the heights and weights entered, ranging from the low height to the high height (inclusive),...

  • Exercise #3: write a Java program that prompts the user to enter a sentence. The program...

    Exercise #3: write a Java program that prompts the user to enter a sentence. The program has to find the print: a. the position of vowels in the sentence. b. the number of vowels in the sentence (A, a, U, u, E, e, O, o, I, i) c. the number of characters as numbers or special characters (other than English letters a.z, A..Z). Hint: remember to ignore the spaces. Sample input-output: Enter the sentnse: UAEU is the university of the...

  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • I need to write a program in java using two array lists. I need to program...

    I need to write a program in java using two array lists. I need to program to prompt the user to enter a day of the week and return an output of a predetermined temperature for the day they selected. I also need the program the output the average of daily temperatures across the week if the user inputs "week". So basically if i set the temperatures to something arbitrary i.e.: Monday = 50 Tuesday = 55 Wednesday = 52...

  • in JAVA program.Use top-down design to design and implement a program to ask the user to...

    in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of...

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