Question: Write a program that will be used to perform simple set manipulation. A set consists of a list of non-duplicated integers, so the following integers:
Write a program that will be used to perform simple set manipulation. A set consists of a list of non-duplicated integers, so the following integers: 5, 9, 16, 9, 3, 0, 5 would be stored in a set as follows: 0, 3, 5, 9, 16 Notice that sets are stored in sorted, ascending, order. For the purpose of this assignment, our sets will not contain any negative numbers - only integers 0 or greater. In the computer, a set is actually a partially-filled array. It, therefore, consists of An array to hold the numbers in the set An integer variable used to keep track of the used size of the array For this program, any array used to hold a set must be declared to have a maximum size of 20. In general, your program will need to get two sets from the user, and then perform some calculations using the sets. Your program should perform the following steps (in the order specified here): Begin by asking the user to enter the integers that belong in the first set. The user can enter 20 unique integers or less. The program should stop allowing the user to enter integers when 20 unique integers have been entered, or when the user enters a negative number. Here's an example where the user enters less than 20 unique numbers, and uses a negative value to signal the end of the input. Note that the input numbers will have spaces, tabs or enter keys between the numbers: Enter the integers for the first set: 5 9 16 9 3 0 5 -1 Ask the user to enter the integers that belong in the second set. The user The user can enter 20 unique integers or less. The program should stop allowing the user to enter integers when 20 unique integers have been entered, or when the user enters a negative number. Here's an example where the user enters exactly 20 unique numbers, and therefore the program won't even allow them to enter the negative number to signal the end of input (because it isn't needed): Enter the integers for the second set: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 18 18 18 18 19 20 Sort the integers in the two sets in ascending order using any sorting algorithm of your choice. You may write your own sorting method, or you may use the sorting method from the Arrays class in Java. Calculate and display the intersection of the two sets The intersection of set A and set B: 3, 5, 9, 16 Calculate and display the difference: first set - second set The difference: set A minus set B: 0 Calculate and display the difference: second set - first set The difference: set B minus set A: 1, 2, 4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20 Ask the user if they want to repeat the program again with two different sets. If the user answers "Y" or "y", return to step 1.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
