complete this method in Java Create a new set that contains

complete this method in Java

* Create a new set that contains all the elements from this set and the other set.

* @param set2 * the second set in the union

* @precondition * set2 is not null, and * getCapacity( ) + set2.getCapacity( ) <= Integer.MAX_VALUE.

* @return * the union of this set and set2

public IntSet union(IntSet set2)

{

// If set2 is null, then a NullPointerException is thrown.

}

Solution

Here No only a method I have also provided the class to make you understand it clearly.

// Imporintng the required package util for sets

import java.util.*;

//IntSet class for the given type

public class IntSet {

// The IntSet has the property of a set varaible for the operations

   Set<Integer> ints=new HashSet<Integer>();
  

// Union method
   public IntSet union(IntSet set2)
   {

// As mentioned checking the condition

// Not null and lessthan MAX value
       if(set2!=null && getCapacity()+set2.getCapacity()<=Integer.MAX_VALUE)
       {

// Taking a new set varibale to return
           Set<Integer> ints2=new HashSet<Integer>();

// Adding this set & set2\'s to the created set using predefined method addAll
           ints2.addAll(this.ints);
           ints2.addAll(set2.ints);

// Creating new varaible of type IntSet to return
           IntSet set=new IntSet();

// Initializing the resulting set
           set.ints=ints2;

// Returnig the set
           return set;
          
       }

// Else throws the Null pointer exeption
       else
       {
           throw new NullPointerException();
          
       }
   }

// A method taken to get the size
   public int getCapacity()
   {
       return ints.size();
   }
  
}

complete this method in Java * Create a new set that contains all the elements from this set and the other set. * @param set2 * the second set in the union * @p
complete this method in Java * Create a new set that contains all the elements from this set and the other set. * @param set2 * the second set in the union * @p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site