1. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## Discrete Math Question

Hello all,

I need to write a program for my computing math course ,and I am kind of stumped by the assignment. Basically, I am given the universal set {0-9}, and need to ask the user to input set A and set B,and then find out what the sym.diff is, where they intersect and such, but without using the set classes in java, but rather using bit strings. I know how to write the part for where they intersect, but I am having trouble saving the user input for both sets and then using the bit strings. I know this is probably a stupid question, but I have wondered about this for a while, and I still have trouble doing it.Any help will be greatly appreciated! :)

2. Member
Join Date
Oct 2011
Posts
92
Rep Power
0

## Re: Discrete Math Question

Sorry man. your question doesn't make any sense whatsoever. you're having trouble saving the user input? As in write to a file or simply storing it in a variable within your program? And using WHAT bit strings exactly? You are going to need to be a bit more specific.

3. ## Re: Discrete Math Question

Have a look at the BitSet class.

kind regards,

Jos

4. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## Re: Discrete Math Question

Unfortunately, I am not allowed to use the bitSet class, otherwise it would have been easy. Basically, I need to get two sets. The Universal set is {0-9}, as I mentioned. So lets say user wants set A {0,1,2} and set B {3,4,5}. The problem I have is how to prompt the user to enter the data, as the sets can be empty, or can contain 5,6,7 elements. That is what I am having trouble figuring out. I hope this is more specific. Should I just use a do while loop for this?

5. ## Re: Discrete Math Question

Lets do it the other way around: what are you allowed to use? Does it really have to be a crippled solution?

kind regards,

Jos

6. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## Re: Discrete Math Question

I am allowed to use bit strings to convert the user input into 0s (if element is not in set) and 1s (if element is in set) for the sets. That is basically what the program is about, and then I am supposed to print out some operations on the sets.I have no trouble performing those, I am stumped on gathering input for both sets and then converting them to 0s and 1s and storing the sets. So if the set is {0,1,2}, there are 1s in those spaces and 0s anywhere else.

7. ## Re: Discrete Math Question

The following code scans a String for 0s and 1s:

Java Code:
```for (int i= 0; i < yourString.length(); i++) {
if (yourString.charAt(i) == '0')
// a 0 was found at position i
else if (yourString.charAt(i) == '1')
// a 1 was found at position i
}```
kind regards,

Jos

8. ## Re: Discrete Math Question

I like your new sig Jos!

9. ## Re: Discrete Math Question

Originally Posted by sunde887
I like your new sig Jos!
:-D

kind regards,

Jos

10. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## Re: Discrete Math Question

Originally Posted by JosAH
The following code scans a String for 0s and 1s:

Java Code:
```for (int i= 0; i < yourString.length(); i++) {
if (yourString.charAt(i) == '0')
// a 0 was found at position i
else if (yourString.charAt(i) == '1')
// a 1 was found at position i
}```
kind regards,

Jos
Thanks a lot, that helps, but I did it without strings, I just used this block of code :

Java Code:
``` while ((userChoice != -1) && in.hasNextInt())
{
userChoice = in.nextInt();

if ((userChoice > -1) && (userChoice < 10))

{
setA |= (1 << userChoice);

System.out.print ("Enter a number between 0 and 9 (or -1 to end): ");
}

}```
I figured it out, but I appreciate the help :)