Results 1 to 8 of 8

Thread: Set of Integers

  1. #1
    rsjava24 is offline Member
    Join Date
    Jan 2010
    Posts
    23
    Rep Power
    0

    Smile Set of Integers

    Hello there! H0w can i make a java program using Queue that would print a set of Integers..for example; Union.,.
    Qa = 123
    Qb = 456

    output = 123456

    Intersection

    Qa = 123
    Bb = 135

    output = 13

    Cartesian

    Qa = {1,2}
    Qb = {3,4}

    output = 1, 3, 1 ,4 ,2 ,3, 2, 4

    I can't visualized this. Can somebody help me or just give me a sample program? Thank you.:)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Why use a Queue to represent a set when a Set is available?

    union==addAll() intersection==retainAll()

    Cartesian products involve creating a new data type. If you are only printing the product and not manipulating it just use nested for loops.

  3. #3
    rummy is offline Member
    Join Date
    Jan 2010
    Posts
    21
    Rep Power
    0

    Default Here is the answer for you

    Here is the coding.... It may be useful to you.....
    Attached Files Attached Files

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rummy View Post
    Here is the coding.... It may be useful to you.....
    That code won't work; try the union of { 1, 2, 3 } and { 2, 3, 4 } for example.

    kind regards,

    Jos

  5. #5
    rsjava24 is offline Member
    Join Date
    Jan 2010
    Posts
    23
    Rep Power
    0

    Default

    Oh, thank you for the code. Highly appreciated. But I have another one. How to implement an array using Queue? Thanks

  6. #6
    rummy is offline Member
    Join Date
    Jan 2010
    Posts
    21
    Rep Power
    0

    Default Sorry..... I think this one is correct...

    Hey .... Sorry i made a mistake.... Please check it out .... Here to remove the duplicates i use TreeSet....
    Attached Files Attached Files

  7. #7
    rsjava24 is offline Member
    Join Date
    Jan 2010
    Posts
    23
    Rep Power
    0

    Default

    Thank you again. But in my program I have 3 choices. 1.Union 2.Intersection. 3. Cartesian.
    When I compiled the program the compiler would ask me the 3 choices. If I entered 1 it will switch to Union then I will input depend how many numbers I'd like to input. For ex: Qa is 123, Qb is 456. output is = 123456. But there is an exception. If entered the same element in the Queue u can display one. For ex: QA- 123, QB 234. Output = 1234..it cannot be 122334. Thank you so much. Appreciated it.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rsjava24 View Post
    Thank you again. But in my program I have 3 choices. 1.Union 2.Intersection. 3. Cartesian.
    When I compiled the program the compiler would ask me the 3 choices. If I entered 1 it will switch to Union then I will input depend how many numbers I'd like to input. For ex: Qa is 123, Qb is 456. output is = 123456. But there is an exception. If entered the same element in the Queue u can display one. For ex: QA- 123, QB 234. Output = 1234..it cannot be 122334. Thank you so much. Appreciated it.
    Read the previous replies instead of copying and pasting code without thinking: rummy's original code doesn't work as I wrote before. It allows duplicates as you have noticed.

    kind regards,

    Jos

Similar Threads

  1. Sorting 3 Integers Using If Else
    By MSteinman in forum New To Java
    Replies: 12
    Last Post: 02-19-2010, 01:52 PM
  2. Integers and Lists
    By TGH in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 10:49 AM
  3. How to write integers into file?
    By dj kourampies in forum New To Java
    Replies: 1
    Last Post: 08-20-2009, 05:52 AM
  4. how to get the Integers out of a String
    By JordashTalon in forum New To Java
    Replies: 10
    Last Post: 01-30-2009, 07:28 PM
  5. Random Integers
    By www.kwalski.com in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 06:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •