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.

Do you want to calculate different sets (y or n)? n

The following is an example of what your might see on the screen see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's inputted values are shown below in italics. Note that your program must display the exact formatting shown here - including the spaces and blank lines.

Enter the integers for the first set: 5 9 16 9 3 0 5 -1 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 The intersection of set A and set B: 3, 5, 9, 16 The difference: set A minus set B: 0 The difference: set B minus set A: 1, 2, 4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20 Do you want to calculate different sets (y or n)? n

Here is another example program run:

Enter the integers for the first set: 5 9 16 9 3 0 5 -1 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 The intersection of set A and set B: 3, 5, 9, 16 The difference: set A minus set B: 0 The difference: set B minus set A: 1, 2, 4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20 Do you want to calculate different sets (y or n)? Y Enter the integers for the first set: 1 2 3 4 5 6 -1 Enter the integers for the second set: 2 4 6 8 -1 The intersection of set A and set B: 2, 4, 6 The difference: set A minus set B: 1, 3, 5 The difference: set B minus set A: 8 Do you want to calculate different sets (y or n)? N

Definitions:

The intersection of two sets is defined as the list of integers that both sets have in common. For example if we have the following two sets:

1, 5, 7, 8, 12, 20

2, 4, 6, 8, 10, 12, 19, 20, 21

The intersection of set A and set B would be as follows:

8, 12, 20

The difference of two sets is defined as the list of integers in the first set which do not appear in the second set. For example the difference, A B, would be all of the numbers in set A which do NOT also appear in set B. The resulting set would be as follows:

1, 5, 7

The difference, B A, would be all of the numbers in set B which do NOT also appear in set A. The resulting set would be as follows:

2, 4, 6, 10, 19, 21

Technical Notes & Requirements:

Remember that the keyboard object should be declared as a class variable (global) and initialized only one time - at the beginning of the main program.

Absolutely NO global variables (class variables) are allowed except for the keyboard object.

You are only allowed to use the number 20 one time in your program! You are not allowed to use the numbers 20, 19, or any other number that is logically related to 20 anywhere in your program. Your program should be designed so that the maximum size of the sets can be changed by simply changing only one globally defined constant. You should declare a constant called MAXSIZE and set it to be size 20.

DO NOT create arrays which are the exact sizes of what the user said. Your arrays MUST be created to be size 20. The point of this assignment is to manage partially-filled arrays --- not arrays which are always completely filled.

You must write and use the following methods in this program:

public static int getData(int [] set) This method will retrieve unique integers from the keyboard. The integers will be placed into the array set. The numbers are expected to be separated by a whitespace character. The user should NOT enter commas between the numbers. The method stops allowing the user to enter numbers once MAXSIZE unique integers have been entered, or when the user enters a negative value - whichever happens first. The return value from the method is the number of unique integers that were placed into set.

public static int intersection(int [] setA, int sizeA , int [] setB, int sizeB, int [] resultSet) This method will calculate the intersection of sets A and B. setA and setB are partially filled arrays assumed to be holding properly created sets, in ascending order. sizeA and sizeB are the number of elements in setA and setB, respectively When the method finishes, resultSet will contain the intersection of sets A and B. The return value from the method is the number of elements that were placed into resultSet.

public static int difference(int [] setA, int sizeA , int [] setB, int sizeB, int [] resultSet) This method calculates the difference (A B) of the two sets. setA and setB are partially filled arrays assumed to be holding properly created sets, in ascending order. sizeA and sizeB are the number of elements in setA and setB, respectively When the method finishes, resultSet will contain the difference of sets A and B. The return value from the method is the number of elements that were placed into resultSet.

When displaying the results, your program should display the contents of a single set all on one line, with each number separated by commas - as is shown in the above examples.

You are free to add additional methods if you need. Just make sure they have a useful purpose and are properly commented.

The intersection, difference and sort methods should NOT print anything to the screen.

When you write your loop to read in the numbers, the loop shouldn't simply count from 1 to the "set size". The user can theoretically enter an infinite number of integers if duplicates are entered. For example, if the user enters: 5 9 4 5 5 3 5 -1 the user actually entered 7 numbers, however the set size is only 4. So, for example, even though the arrays are declared to hold only 20 numbers, the user can actually enter 30 numbers if desired. The key point is that 20 of them must be unique. If the user enters 30 numbers where some of them are duplicated, only the 20 unique ones would be stored in the array and therefore, still within the limits of the array declaration.

Watch out for correct indenting! Do not use the SPACEBAR to move lines into their proper positions. Instead, use the TAB key.

Use line breaks to separate methods and to separate sections of code. Don't write long lines that scroll off the right side of the screen or that wrap around to the next line. Break them where appropriate to make them neater.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!