# Set of Integers

• 01-27-2010, 10:46 AM
rsjava24
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.:)
• 01-27-2010, 11:03 AM
pbrockway2
Why use a Queue to represent a set when a Set is available?

Cartesian products involve creating a new data type. If you are only printing the product and not manipulating it just use nested for loops.
• 01-27-2010, 01:38 PM
rummy
Here is the answer for you
Here is the coding.... It may be useful to you.....
• 01-27-2010, 01:54 PM
JosAH
Quote:

Originally Posted by rummy
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
• 01-28-2010, 10:44 AM
rsjava24
Oh, thank you for the code. Highly appreciated. But I have another one. How to implement an array using Queue? Thanks
• 01-28-2010, 10:51 AM
rummy
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....
• 01-28-2010, 11:07 AM
rsjava24
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.
• 01-28-2010, 11:29 AM
JosAH
Quote:

Originally Posted by rsjava24
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