Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

• Behaviors (methods) – A getter method, int size() – A boolean elementOf(int x)

ID: 3633434 • Letter: #

Question

• Behaviors (methods)
– A getter method, int size()
– A boolean elementOf(int x) method that tells whether x is in the associated int set
– A void printElements() method that prints the associated set of integers on one line surrounded by {} and separated by commas and spaces, like so: { 1, 2, 3, 4, 5 }
– Override the merge method so that if x and y are IntegerSets then x.merge(y) returns a reference to a new IntegerSet z that contains the integers in x and/or y. The set z should not contain any duplicates. merge should work with empty sets.
. Write a simple test class that first creates three IntegerSet objects containing three sets of integers, including some duplicates across the three sets. Then merge the first two sets to produce a new fourth IntegerSet, and finally merge that IntegerSet with the third IntegerSet to produce a final IntegerSet that combines all three of the original IntegerSets. At the end of the test program print out the original three IntegerSets and the merged IntegerSet.

Explanation / Answer

Hi, I'ma ssuming this is the same IntegerSet class as in the other merge exercise. So I made some slight modifications to fit these requirements in with the already written code.

I hope this helps, please remember to rate :)

public class IntegerSet implements Mergeable {
    // attributes declaration
    private int[] intSet;
    private int size;
  
    // 0-arg constructor
    public IntegerSet()
    {
        intSet = new int[0];
        size = 0;
    }
  
    // 2-arg constructor
    public IntegerSet(int[] set, int s)
    {
        size = s;
        intSet = new int[size];
        int[] tempSet = new int[size];
      
        // fill out a test array and resolve duplicates
        // before putting the final version into intSet
        int insertCount = 0;
        for(int i = 0; i < set.length; i++)
        {
            if( elementOf(set[i]) == false )
            {
                tempSet[insertCount] = set[i];
                insertCount++;
            }
        }
       
        // now we know the real size excluding duplicates
        // so we create your intSet and giev it thevalues
        // held in tempSet, we also update the size to reflect
        // the removed duplicates
        size = insertCount;
        intSet = new int[size];
        for(int i = 0; i < size; i++)
        {
            intSet[i] = tempSet[i];
        }
    }
   
    // getter method for size
    public int getSize()
    {
        return size;
    }
   
    // method to check if int is element of this IntegerSet
    public boolean elementOf(int n)
    {
        boolean duplicate = false;
        for(int i = 0; i < size; i++)
        {
            if(intSet[i] == n)
                duplicate = true;
        }
        return duplicate;
    }
   
    // print Elements method to display them
    public void printElements()
    {
        for(int i = 0 ; i < size; i++)
        {
            System.out.print(intSet[i] + " ");
        }
    }
  
    // merge method implemented from interface Mergeable
    @Override
    public Object merge(Object x)
    {
        if(! (x instanceof IntegerSet))
            return null;
        IntegerSet toMerge = (IntegerSet)x;
       
        // use a temp array to merge both
        // until you can remove the duplicates from 2nd
        // array that might show up in first one
        int[] tempMerged = new int[size + toMerge.getSize()];
       
       
       
        // each array from IntegerSet is duplicate-free from its constructor
        // so we just copy the first array as is with no risk of duplicates
        for(int i = 0; i< size; i++)
            tempMerged[i] = intSet[i];
       
        // now that we need to insert the 2nd array, there is a possibility
        // of duplicates between ints in this one and in the previous
        // we follow the same process as in the constructor duplicate-removal
        // but not starting at index = 0, we start right after the last element
        // of the first array, at index = 'size'
        int mergeCount = 0;
        int k = size;
        for(int i = 0; i < toMerge.getSize(); i++ )
        {
            if( elementOf(toMerge.intSet[i]) == false )
            {
                tempMerged[k] = toMerge.intSet[i];
                k++; // moves to next location to store element
                mergeCount++; // counts number of elements inserted - non-duplicates
            }
        }
       
        // create the final duplicate-free merged array with accurate size
        // and add the tempMerged items to it
        int[] merged = new int[size + mergeCount];
        for(int i = 0; i < merged.length; i++)
        {
            merged[i] = tempMerged[i];
        }
       
        IntegerSet mergedSet = new IntegerSet(merged, merged.length);
        return mergedSet;      
    }

}

public class TestClass {

    public static void main(String[] args) {
       
        System.out.println(" Creating IntegerSet 1...");
        IntegerSet set1 = new IntegerSet(new int[]{12, 8 , 6} , 3);
       
        System.out.println(" Creating IntegerSet 2...");
        IntegerSet set2 = new IntegerSet(new int[]{39, 7 , 12, 23} , 4);
       
        System.out.println(" Creating IntegerSet 3...");
        IntegerSet set3 = new IntegerSet(new int[]{23, 4} , 2);
       
        System.out.println(" Merging set 1 and set 2 into set 4 ...");
        IntegerSet set4 = (IntegerSet) set1.merge(set2);
       
       
        System.out.println(" Merging set 3 and set 4 into set 5 ...");
        IntegerSet set5 = (IntegerSet) set4.merge(set3);
       
       
        System.out.println(" Elements of Set1:");
        set1.printElements();
        System.out.println(" Elements of Set2:");
        set2.printElements();
        System.out.println(" Elements of Set3:");
        set3.printElements();
       
        System.out.println(" Elements of Set5:");
        set5.printElements();
       
    }
}