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...
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 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?
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...
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 a JAVA program called Convertor that has two methods. The first one converts an input...
Write a JAVA program called Convertor that has two methods. The first one converts an input binary into its equivalent decimal number. The second method converts a decimal number into its equivalent binary.​ binary2Decimal, decimal2Binary are the names for those methods.​ binary2Decimal receives a binary number as a string and returns a decimal number as an integer. And vice versa for the decimal2Binary method.​
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
Write a complete java program to get input of a person’s age and their years of...
Write a complete java program to get input of a person’s age and their years of current USA citizenship. Tell them if they are eligible to run for US House of Representatives, US Senate, or President. At first, have the program just run once and give the answer for the given inputs. Give the answer in a nice format and be clear which offices the person can run for. Write good and complete pseudo code. Next, put that program in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT