1

I'm new to using sets and for one of the exercise to learn how to use sets, I have been given a template file "SetInt" and a completed test file "TestSetInt". I know that I should use retainAll() and addAll() but the test class has asked me to write methods. Anyone have any idea how to write these two methods? Any help would be appreciated.

SetInt is the file which I have to add methods to and goes as follows:

public class SetInt 
{
    int[] setArray = new int[52];

public void add(int a)
{
    if (a < setArray.length-2)
    {
        setArray[a] = a;
    }
}
/* add a to the set in the correct position
 *@param a the value to be added
 **/    

public SetInt intersection(SetInt anySet)
{       
    return anySet;
}
/* returns the set of integers common to both
 * the set passed in as an argument and the set the
 * method is called on
 * @param anySet - the set to be intersected
 * @return a set containing the result of the intersection
*/

public SetInt union(SetInt anySet)
{   
    return anySet;
}
/* returns the set of integers which are in either the 
 * set passed in as an argument or the set the method
 * is called on
 * @param anySet - one of the sets in the union
 * @return a set containing the result of the union
*/

}

Here is TestSetInt, a file provided for me which I have to run to see if my methods work correctly:

public class TestSetInt
{
    public TestSetInt()
    {
        SetInt test1 = new SetInt();
        SetInt test2 = new SetInt();

//      adding numbers to the 2 sets
        for(int i = 2; i<49;i=i+3)
        {
            test1.add(i);
        }
        System.out.println();

        for(int i = 2; i<49;i=i+4)
        {
            test2.add(i);
        }
        System.out.println();

//      testing intersection
        SetInt interx = new SetInt();
        interx=test2.intersection(test1);

//      testing union       
        SetInt unionx = test1.union(test2);

    }

    public static void main (String args[])
    {
        TestSetInt practice = new TestSetInt(); 
    }
}
2
  • Are you allowed to use other data structures?
    – rw.liang1
    Apr 26, 2014 at 18:30
  • I can use Set, SortedSet and NavigableSet and implementations of HashSet, TreeSet and ConcurrentSkipListSet
    – MOZAKATAK
    Apr 26, 2014 at 18:35

1 Answer 1

0

this works :)

SetInt Class

public class SetInt {
    int[] setArray = new int[52];

    public void add(int a) {
        if (a < setArray.length - 2) {
            setArray[a] = a;
        }
    }

    /*
     * add a to the set in the correct position
     * 
     * @param a the value to be added
     */

    public SetInt intersection(SetInt anySet) {
        SetInt temp = new SetInt();
        for (int i = 0; i < setArray.length; i++) {
            for (int j = 0; j < anySet.setArray.length; j++) {
                if (setArray[i] == anySet.setArray[j]) {
                    temp.setArray[i] = setArray[i];
                }
            }
        }
        return temp;
    }

    /*
     * returns the set of integers common to both the set passed in as an
     * argument and the set the method is called on
     * 
     * @param anySet - the set to be intersected
     * 
     * @return a set containing the result of the intersection
     */

    public SetInt union(SetInt anySet) {
        SetInt temp = new SetInt();
        for (int i = 0; i < setArray.length; i++) {
            for (int j = 0; j < anySet.setArray.length; j++) {
                temp.setArray[i] = setArray[i];
                if (anySet.setArray[j] != 0) {
                    temp.setArray[j] = anySet.setArray[j];
                }
            }
        }
        return temp;
    }
    /*
     * returns the set of integers which are in either the set passed in as an
     * argument or the set the method is called on
     * 
     * @param anySet - one of the sets in the union
     * 
     * @return a set containing the result of the union
     */

}

TestSetInt

public class TestSetInt {
    public TestSetInt() {
        SetInt test1 = new SetInt();
        SetInt test2 = new SetInt();

        // adding numbers to the 2 sets
        for (int i = 2; i < 49; i = i + 3) {
            test1.add(i);
        }
        printSetInt(test1);

        for (int i = 2; i < 49; i = i + 4) {
            test2.add(i);
        }
        printSetInt(test2);

        // testing intersection
        SetInt interx = new SetInt();
        interx = test2.intersection(test1);
        printSetInt(interx);

        // testing union
        SetInt unionx = test1.union(test2);
        printSetInt(unionx);

    }

    public void printSetInt(SetInt set) {
        for (int i : set.setArray) {
            System.out.print(i + ",");
        }
        System.out.println();
    }

    public static void main(String args[]) {
        TestSetInt practice = new TestSetInt();
    }

}
2
  • Thank you so much! I would give you an upvote but my rep is too late
    – MOZAKATAK
    Apr 26, 2014 at 19:42
  • I am glad that i could help :) . You can mark my answer as correct with button under upvote :) i would be realy thankful .... Apr 26, 2014 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.