Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By JosAH

Thread: Discrete Math Question

  1. #1
    HardToHandle is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default 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. #2
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default 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. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Discrete Math Question

    Have a look at the BitSet class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    HardToHandle is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default 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. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    HardToHandle is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default 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. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default 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
    HardToHandle likes this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Discrete Math Question

    I like your new sig Jos!

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

    Default Re: Discrete Math Question

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    HardToHandle is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Discrete Math Question

    Quote Originally Posted by JosAH View Post
    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 :)

Similar Threads

  1. math and GUI
    By urbanleg in forum AWT / Swing
    Replies: 3
    Last Post: 08-06-2011, 05:05 PM
  2. Discrete Mathematics Question
    By asafavi in forum New To Java
    Replies: 3
    Last Post: 12-09-2010, 12:05 PM
  3. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 07:23 AM
  4. Math.random Question
    By noble in forum New To Java
    Replies: 21
    Last Post: 09-16-2010, 03:17 AM
  5. Math class question
    By shendel in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 01:06 PM

Tags for this Thread

Posting Permissions

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