Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Help With Java Homework: Set Operations

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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();}
     
    	}
     
     
    	}
    Last edited by copeg; March 31st, 2011 at 08:59 AM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: Help With Java Homework: Set Operations

    For future reference, please use the code tags. I've edited your post to reflect this.
    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)

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);

    }


    }

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Java Homework: Set Operations

    Thats the part im having trouble with.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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).

  8. #8
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

  11. #11
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Java Homework: Set Operations

    alright. thx

Similar Threads

  1. Operations with lists
    By datreta in forum Collections and Generics
    Replies: 8
    Last Post: October 29th, 2010, 08:54 AM
  2. unsafe operations note??
    By bookface in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 22nd, 2010, 09:58 AM
  3. Bulk operations on sets/ map problem
    By kyuss in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 14th, 2010, 12:48 PM
  4. Help for java homework
    By Dark_Shadow in forum Object Oriented Programming
    Replies: 6
    Last Post: December 7th, 2009, 03:08 AM
  5. MORE java homework help.
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: October 13th, 2009, 07:15 PM