Reputation: 7
This is my assignment:
Assume U = {1 , . . . ,10 }. Write a Java program which lets you enter a pair of sets A & B from the keyboard, then computes and prints their union and intersection. Remember that the sets of A and B must be a subset of the U(universe) set.
Right now my main concern is how to compare each index from the two arrays I have. If the indexes match, then I need it to be true, otherwise if they don't match, I need it to be false. I just don't know how to compare the two so if someone could give me some guidance, that'd be great. (I realize I don't have anywhere in my code comparing the two, I just am unsure of where to begin with it).
Here's my code:
import java.util.*;
public class sets {
public static void main (String[] args) {
Scanner console = new Scanner(System.in);
//Declare the variables
int [] universe = new int[10];
//Ask the user for the numbers in set A
System.out.print("How many numbers do you want in the first set? ");
int num1 = console.nextInt();
int [] A = new int [num1];
int i = 0;
while (i < num1) {
System.out.print("Please enter an integer 1-10: ");
int numA = console.nextInt();
A[i++] = numA;
}
//Ask the user for the numbers in set B
System.out.print("How many numbers do you want in the second set? ");
int num2 = console.nextInt();
int [] B = new int [num2];
int j = 0;
while (j < num2) {
System.out.print("Please enter an integer 1-10: ");
int numB = console.nextInt();
B[j++] = numB;
}
System.out.println(Arrays.toString(A));
System.out.println(Arrays.toString(B));
}
}
Upvotes: 0
Views: 101
Reputation: 39386
Disclaimer: As this is apparently a homework, I will not post any code per se.
The first thing you need to do is define the 3 notions you use in this exercise:
set
is a group of elements such as each element is present only onceset
of element present in group
A or in group
Bset
of element present both in group
A and in group
BThen you need to define what basic functions you will need.
set
.Now, looking at what you have so far, the issue is that you are creating 2 groups A and B that are not sets. There is nothing preventing to have a group like so [1, 1, 1]
Therefore, you first work should be to make sure each group remains a set, by checking the content of the set before adding a value. That uses the first function defined earlier.
Once A and B are both sets
, you need to find an algorithm for each of the functions that are asked of you.
The union algorithm is quite simple:
The intersection is not very much complicated:
All this should fit in around 20 lines, 30 tops.
Upvotes: 0
Reputation: 39437
Declare two methods
private static int[] union(int[] s1, int[] s2)
and
private static int[] intersection(int[] s1, int[] s2)
See if you can generate their return values
from the int[]
values (i.e. the sets) passed in.
Alternatively, you can avoid all the trouble, and just use
HashSet<Integer>
instead of int[]
for representing your
sets. But I guess you want to do everything yourself which is
better (as this is just an exercise).
Upvotes: 1