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

Thread: Union and Intersection methods (SetADT)

  1. #1
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Question Union and Intersection methods (SetADT)

    Hey, so i have this SetADT, and i need to add two methods, Union and Intersection.

    • The Union method will return a union of the two sets as an array to the calling program (no duplicates allowed).
    • The Intersection method will return the intersection of the two sets as an array to the calling program (no duplicates allowed).

    Can anyone guide me in the right direction to adding these two methods?

    SetADT.java

    package DSA2;
     
    public class SetADT
    {
        //define fields
        private int pointer;
        private int[] setArray;
     
        //define methods
        public SetADT()
        {
            pointer = 0;
            setArray = new int[5];
        }
     
        public void add(int value)
        {
            if (!contains(value))
            {   //check available space
                //if not - increase the array size
                if (pointer == setArray.length)
                {
                    setArray = increaseSize();
                }
     
                setArray[pointer] = value;
                pointer++;
            }
        }
     
        @Override
        public String toString()
        {    
            StringBuilder sb = new StringBuilder();
     
            for(int index=0; index<pointer; index++)
            { 
                sb.append(setArray[index]);
                sb.append(", ");
            }
     
            return sb.toString();
        }//toString
     
        public boolean contains(int value)
        {
            for(int index=0; index < setArray.length; index++)
            {
                if(setArray[index]== value)
                {
                    return true;
                }
            }
     
            return false;
        }//contains
     
        private int[] increaseSize()
        {
            int size = setArray.length;
            int [] newArr = new int[size + 5];
     
            System.arraycopy(setArray, 0, newArr, 0, setArray.length);
            return newArr;
        }
     
        public int[] toArray()
        {
            int [] tmpArray = new int[pointer];
            System.arraycopy(setArray, 0, tmpArray,0, pointer);
     
            return tmpArray;
        }//toArray
     
    }

    Thanks in advance!

    Jason.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,356
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Union and Intersection methods (SetADT)

    What are your ideas for how to implement each of these methods?
    Do you have two arrays of int values to scan through and make some decisions about using their contents to make a third array.
    Are the arrays sorted?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Union and Intersection methods (SetADT)

    package DSA2;
     
    public class TestSet
    {
        public static void main(String[] args) 
        {
            SetADT set1 = new SetADT();
            SetADT set2 = new SetADT();
     
            set1.add(1);
            set1.add(2);
            set1.add(4);
            set1.add(5);
            set1.add(7);
     
            set2.add(1);
            set2.add(2);
            set2.add(3);
            set2.add(6);
            set2.add(7);
            set2.add(8);
     
     
            System.out.println(set1.toString());
     
            System.out.println();
     
            System.out.println(set2.toString());
     
            int [] setItems1 = set1.toArray();
     
            int [] setItems2 = set2.toArray();
     
            System.out.println(setItems1.length);
     
            System.out.println(setItems2.length);
     
        }
    }

    This is my test code, which adds several numbers to two different sets.

    I need to figure out how to implement the two methods, one producing the union, and the other the intersection of the two sets. I'm not sure how to do this, any help would be much appreciated.

    Thanks, Jason.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,356
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Union and Intersection methods (SetADT)

    What are your ideas for how to implement each of these two methods?
    Do you have two arrays of int values to scan through and make some decisions about using their contents to make a third array.
    Are the arrays sorted? If not, having them sorted would make it easier.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,342
    My Mood
    Hungover
    Thanks
    143
    Thanked 625 Times in 536 Posts

    Default Re: Union and Intersection methods (SetADT)

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/72662-union-intersection-methods-setadt.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Union and Intersection methods (SetADT)

    What are your ideas for how to implement each of these two methods?
    Maybe combine both arrays and then iterate through to find similar values and removes them ?

    Do you have two arrays of int values to scan through and make some decisions about using their contents to make a third array.
    The arrays both contain int values

    Are the arrays sorted? If not, having them sorted would make it easier.
    No they aren't sorted, I might consider doing this, if needs be

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,356
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Union and Intersection methods (SetADT)

    Sorting the arrays will make the logic much easier.
    Given the two arrays: {1,3,4} and {1,2,3}
    How do you get their intersection?
    There needs to be a separate index for each array. Look at the current element in each array at the location pointed to by the index for that array.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Union Coding Hashmap
    By codingjava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2013, 06:21 AM
  2. 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
  3. 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
  4. [SOLVED] UNION OF TWO INTEGER ARRAYS... HELP?
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 23rd, 2011, 03:28 PM
  5. help: union of two array,.. kinda problem
    By sanfor in forum Collections and Generics
    Replies: 2
    Last Post: November 29th, 2009, 01:33 PM