Help With Java Homework: Set Operations
I am not doing that well in my Java Programming class. My next assignment seems pretty simple but I have no idea of what to do and where to start. Any algorithm or sample program would be very helpful.
You will write a program called Sets.java that will:
* Read two sets of numbers from the keyboard.
* Print the intersection of the two sets, in any order, without duplicates.
* Print the union of the two sets, in any order, without duplicates.
Sample Run #1
Just for illustration, what you type looks like this:
Enter values for first set, ending with -1
1 3 9 7 5 -1
Enter values for second set, ending with -1
2 3 5 7 -1
Intersection: 3 7 5
Union: 1 3 9 7 5 2
Again any help would be very helpful. Thanks
Re: Help With Java Homework: Set Operations
This is what I have so far. I think I'm on the right track... But I don't know how to keep reading numbers from the keyboard until -1 is typed in. I tried using an if statement but I dont know if I'm doing it right.
Any feedback?
Thanks.
Code java:
package Sets;
import java.util.Scanner;
public class Sets {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter values for first set, ending with -1");
int set1 = in.nextInt();
{if (set1 != -1);
set1 = in.nextInt();}
System.out.println("Enter values for second set, ending with -1");
int set2 = in.nextInt();
{if (set2 != -1);
set2 = in.nextInt();}
}
}
Re: Help With Java Homework: Set Operations
For future reference, please use the code tags. I've edited your post to reflect this.
Quote:
But I don't know how to keep reading numbers from the keyboard until -1 is typed in. I tried using an if statement
Think while or do while: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: Help With Java Homework: Set Operations
Ok I think I'm getting there. I'm having trouble getting the Union of both sets without repeating numbers. And I have no idea how to get the intersect. I think I need to use Char.At or something and compare all the ints in both sets and only print the ones that appear in both. But im not sure how to go about it... This is what I have so far. Any help? Thanks.
package Sets;
import java.util.Scanner;
public class Sets {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = new String();
String r = new String();
System.out.println("Enter TWO numbers to begin.");
int set1 = in.nextInt();
int set2 = in.nextInt();
System.out.println("Enter values for first set, ending with -1");
while (set1 != -1){
set1 = in.nextInt();
if (set1 != -1)
s = s + set1 + " ";
}
System.out.println("Enter values for second set, ending with -1");
while (set2 != -1){
set2 = in.nextInt();
if (set2 != -1)
r = r + set2 + " ";
}
System.out.println("Union: " +s +r);
}
}
Re: Help With Java Homework: Set Operations
1. Code tags please.
2. Indentation please.
3. So, now you have all integers as a string concatenated to each other. You can simply extract individual integer and compare it with the other string, and then perform your task.
Re: Help With Java Homework: Set Operations
Thats the part im having trouble with.
Re: Help With Java Homework: Set Operations
If you are separating each integer into a string with any special character then you may simply use nextInt() of Scanner (util.java).
Re: Help With Java Homework: Set Operations
I thought I already did that to read numbers from the keyboard and stored those numbers into a string. I was kinda hoping if someone could add to my code because I literally think I do not have the java knowledge on coding this next piece and I can review and fiddle with it to see exactly what it does and how it works.
Re: Help With Java Homework: Set Operations
I can understand that how much problems are there for newbie. But you need to get rid of these troubles by browsing, learning, making mistakes and then you will be able to learn.
Re: Help With Java Homework: Set Operations
1. Initialize your string with null.
2. After getting first input and coming inside the loop, first change your integer to string and then get the other value and continue concatnating till the end.
3. As soon as you completed concatenation, you can use hasNextInt() that will extract the integer and places the pointer to that location.
4. That's how you can extract integer from strings and i hope you can compare these to eachother then.
Re: Help With Java Homework: Set Operations