Advertisement
Guest User

Untitled

a guest
Nov 17th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Set
  4. {
  5.  
  6. private ArrayList<String>elements;
  7.  
  8. /**
  9.  * creates an empty set
  10.  */
  11.  
  12. public Set()
  13. {
  14.     elements = null;
  15. }
  16.    
  17. /**
  18.  * creates a set using the elements of the ArrayList s.
  19.  * @param s the ArrayList whose elements are used to create this set.
  20.  * @throws IllegalArgumentException if s contains duplicity.
  21.  */    
  22.    
  23. public Set(ArrayList<String> s)
  24. {
  25.     int i;
  26.     elements = new ArrayList<String>();
  27.    
  28.     for(i=0;i<s.size();i++)
  29.     {
  30.         if(elements.contains(s.get(i)))
  31.         {throw new IllegalArgumentException("Set(ArrayList<String>)duplicity not allowed in sets");}
  32.        
  33.         elements.add(s.get(i));
  34.            
  35.     }
  36. }
  37.    
  38. /**
  39.  * creates a set using the elements of the array s.
  40.  * @param s the array whose elements are used to create this set.
  41.  * @throws illegalArgumentException if s contains duplicity.
  42.  */    
  43.  
  44. public Set(String[] s)
  45.    {
  46.        int i;
  47.        elements = new ArrayList<String>();
  48.        for(i=0; i<s.length; i++)
  49.        {
  50.           if (elements.contains(s[i]))
  51.           {throw new IllegalArgumentException("Set(String[]):duplicity not allowed in sets");}
  52.           elements.add(s[i]);
  53.        }
  54.    }
  55.  
  56.    /**
  57.     * determines whether a set contains the specified element
  58.     * @param elt an element
  59.     * @return true if elt is an element of this set; otherwise, false
  60.     */
  61.  
  62.    public boolean isElement(String elt)
  63.    {
  64.        return elements.contains(elt);
  65.    }
  66.  
  67.    /**
  68.     * determines the size of this set.
  69.     * @return the size of this set.
  70.     */
  71.    
  72.    public int cardinality()
  73.    {
  74.        return elements.size();
  75.    }
  76.  
  77.    /**
  78.     * computes the intersection of this set and the
  79.     * specified set.
  80.     * @param s a set
  81.     * @return a set representing the intersection of this set
  82.     *         and s.
  83.     */
  84.    
  85.    public Set intersect(Set s)
  86.    {
  87.        int i;
  88.        ArrayList<String> result = new ArrayList<String>();
  89.        for (i=0;i<s.cardinality();i++)
  90.        {
  91.            if (this.isElement(s.elements.get(i)))
  92.            {result.add(s.elements.get(i));}
  93.        }  
  94.        return new Set(result);          
  95.    }
  96.    
  97.     /**
  98.     * computes the union of this set and the specified set.
  99.     * @param s a sets
  100.     * @return a set representing the union of this set
  101.     *         and s.
  102.     */
  103.  
  104.   public Set union(Set s)
  105.    {
  106.        int i;
  107.        ArrayList<String> result = new ArrayList<String>();
  108.        result.addAll(this.elements);
  109.        result.addAll(s.elements);
  110.        for(i=0;i<s.cardinality();i++)
  111.        {
  112.            if (this.isElement(s.elements.get(i)))
  113.            {result.remove(s.elements.get(i));}
  114.        }
  115.        return new Set(result);
  116.    }
  117.  
  118.    /**
  119.     * computes the difference between this set and the
  120.     * specified set.
  121.     * @param s a set
  122.     * @return a set representing the difference between
  123.     *         this set and s.
  124.     */
  125.  
  126.    public Set diff(Set s)
  127.    {
  128.        int i;
  129.        ArrayList<String> result = new ArrayList<String>();
  130.        result.addAll(this.elements);
  131.        for(i=0;i<s.cardinality();i++)
  132.        {
  133.            if (this.isElement(s.elements.get(i)))
  134.            {result.remove(s.elements.get(i));}
  135.        }
  136.        return new Set(result);
  137.    }
  138.    
  139.    /**
  140.     * computes the symmetric difference between this set
  141.     * and the specified set.
  142.     * @param s a set
  143.     * @return a set representing the symmetrical difference
  144.     *         between this set and s.
  145.     */
  146.    
  147.    public Set symDiff(Set s)
  148.    {
  149.        int i;
  150.        ArrayList<String> result = new ArrayList<String>();
  151.        result.addAll(this.elements);
  152.        result.addAll(s.elements);
  153.        for(i=0;i<s.cardinality();i++)
  154.        {
  155.            if (this.isElement(s.elements.get(i)) && s.isElement(this.elements.get(i)))
  156.            {result.remove(this.elements.get(i));
  157.             result.remove(s.elements.get(i));}
  158.        }
  159.        return new Set(result);
  160.    }
  161.    
  162.    /**
  163.     * computes the Cartesian product for this set
  164.     * and the specified set.
  165.     * @param s a set
  166.     * @return a set representing the Cartesian product
  167.     *         of this set and s.
  168.     */
  169.    
  170.    public Set xProduct(Set s)
  171.    {
  172.        //implement this method
  173.    }
  174.  
  175.  
  176.    /**
  177.     * determines whether a set is empty
  178.     * @return true if this set is empty; otherwise, false
  179.     */
  180.    
  181.    public boolean isEmpty()
  182.    {
  183.        return elements.isEmpty();
  184.    }
  185.  
  186.    /**
  187.     * determines whether this set is equal to the specified
  188.     * set.
  189.     * @param s a set
  190.     * @return true if this set is equal to s; otherwise, false
  191.     */
  192.    
  193.    public boolean equals(Set s)
  194.    {
  195.        return elements.equals(s.elements);
  196.    }
  197.  
  198.    /**
  199.     * determines whether this set is a subset of the specified set.
  200.     * @param s a set
  201.     * @return true if this set is a subset of s; otherwise, false
  202.     */
  203.    
  204.    public boolean subset(Set s)
  205.    {
  206.         return elements.containsAll(s.elements);
  207.    }
  208.  
  209.    /**
  210.     * determines whether this set is a proper subset of the specified set.
  211.     * @param s a set
  212.     * @return true if this set is a proper subset of s; otherwise, false
  213.     */
  214.    
  215.    public boolean properSubset(Set s)
  216.    {
  217.        if(elements.equals(s.elements) && elements.containsAll(s.elements))
  218.        {return false;}
  219.        else{
  220.            return true;
  221.        }
  222.        
  223.    }
  224.    
  225.    /**
  226.     * returns a string {x1,x2,...,xn} representing this set,
  227.     * where x1,x2,...,xn are elements of this set.
  228.     * @return a string representation of this set formatted
  229.     *         as specified.
  230.     */
  231.    
  232.     @Override
  233.    public String toString()
  234.    {
  235.        return "{"+this.elements+"}";          
  236.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement