Question

In: Computer Science

Write a program in Java that will take as input two sets A and B, and...

Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.

Solutions

Expert Solution

SOURCE CODE

import java.util.*;
public class Set_example
{
   public static void main(String args[])
   {
       Set<Integer> a = new HashSet<Integer>();
       a.addAll(Arrays.asList(new Integer[] {21, 23, 22, 24, 28, 29, 20}));
       Set<Integer> b = new HashSet<Integer>();
       b.addAll(Arrays.asList(new Integer[] {21, 23, 27, 25, 24, 20, 27, 25}));

       // To find intersection
       Set<Integer> intersection = new HashSet<Integer>(a);
       intersection.retainAll(b);
       System.out.print("Intersection");
       System.out.println(intersection);

       // To find union
       Set<Integer> union = new HashSet<Integer>(a);
       union.addAll(b);
       System.out.print("Union");
       System.out.println(union);

       // To find the symmetric difference
       Set<Integer> difference = new HashSet<Integer>(a);
       difference.removeAll(b);
       System.out.print("SET Difference");
       System.out.println(difference);
   }
}

OUTPUT SCREENSHOT

please give a upvote if u feel helpful.


Related Solutions

Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT