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 6 of 6

Thread: Intersection and Union of Three or more Sets

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Intersection and Union of Three or more Sets

    I need help i have to write a program to verify if a set is a subset of the other that A intersect B intersect C = A' union B' union C'
    I am new to this forum Thanks in advance


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Intersection and Union of Three or more Sets

    Welcome robertm08
    What is it you need help with?
    Sounds like the description is of several problems, choose one to work with first, and let us know what you get stuck on

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Intersection and Union of Three or more Sets

     
    package SetTheory;
     
    import java.util.HashSet;
    import java.util.Set;
    import java.util.TreeSet;
     
     
    public class test {
     
    	/**
    	 * @param args
    	 */
     
    	  public static  Set intersection(Set setA, Set setB, Set setC) {
    		    Set S = new TreeSet(setA);
    		   S.retainAll(setB);
    		   S.retainAll(setC);
    		    return S;
    		  }
     
    	  public static  Set union(Set setA, Set setB, Set setC) {
    		    Set M = new TreeSet(setA);
    		    M.addAll(setB);
    		    M.addAll(setC);
    		    return M;
    		  }
     
    	  public static  Set notB (Set setA, Set setB, Set setC) {
    		    setA.removeAll(setB);
    		    setC.removeAll(setB);
    		    setA.addAll(setC); 
    	         return setA;
    	 	  }
               public static  Set notA (Set setA, Set setB, Set setC) {
    		    setB.removeAll(setA);
    		    setC.removeAll(setA);
    		    Set R = new TreeSet(setB);
    		    R.addAll(setC);
     
    		     return R;
    		  }
     
    	public static void main(String[] args) {
    		TreeSet set1 = new TreeSet();
    	    TreeSet set2 = new TreeSet();
    	    TreeSet set3 = new TreeSet();
     
     
    		set1.add('a');
    		set1.add('d');
    		set1.add('e');
    		set1.add('g');
     
    		set2.add('b');
    		set2.add('d');
    		set2.add('f');
    		set2.add('g');
     
    		set3.add('c');
    		set3.add('f');
    		set3.add('e');
    		set3.add('g');
     
    		System.out.println("Union:" + union(set1, set2, set3));
    		System.out.println("Intersection:" + intersection(set1, set2, set3));
    		System.out.println("B' = " + notB(set1, set2, set3));
    		System.out.println("A' = " + notA(set1, set2, set3));
    		}
     
    }
    output
    Union:[a, b, c, d, e, f, g]
    Intersection:[g]
    B' = [a, c, e]
    A' = [b, d, f, g]

    the first difference output from B' is correct because B' = (A-B)+(C-B) = a,c,e
    i am confused because of the output of A' = (B-A)+(C-A) i need to make the output to be b,f,c
    Last edited by robertm08; August 19th, 2013 at 01:05 AM. Reason: code tags

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Intersection and Union of Three or more Sets

    Please use code tags when posting code, assistance can be found on the Announcements Page.

    Please do note that we do not provide solutions, we provide suggestions, but I do not understand what it is you need help with. What are you stuck on?

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Intersection and Union of Three or more Sets

    I am sorry Jps now i have fixed it sorry for my bad english

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Intersection and Union of Three or more Sets

    I'm sorry too... I still do not see your question or a description of the problem you are stuck on.

Similar Threads

  1. Intersection of two loops
    By sengreen in forum Loops & Control Statements
    Replies: 3
    Last Post: May 26th, 2013, 12:44 PM
  2. Union and Intersection methods (SetADT)
    By jason3460 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 7th, 2013, 11:22 AM
  3. Union Coding Hashmap
    By codingjava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2013, 06:21 AM
  4. INTERSECTION OF TWO LIST
    By muhammad waqar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 07:09 AM
  5. Not sure what kind of loop I need to use for an intersection program
    By sessypeanut in forum Loops & Control Statements
    Replies: 4
    Last Post: December 21st, 2012, 10:11 PM

Tags for this Thread