Advertisement
Guest User

Untitled

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