Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Implementing basic operations on sets using Array Lists

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Implementing basic operations on sets using Array Lists

    Hi I'm new to computer science and working on a program to implement a set class using array lists I was looking for some help because I am completely loss right now

    import java.util.*;
     
    public class Set 
    {
        private ArrayList<String> elements;
     
       /**
        * creates an empty set
        */
       public Set()
       {
           elements = null;
       }
     
       /**
        * creates a set using the elements of the ArrayList s.
        * @param s the ArrayList whose elements are used to
        *        create this set.
        * @throws IllegalArgumentException if s contains duplicity.
        */
       public Set(ArrayList<String> s)
       {
           int i;
           elements = new ArrayList<String>();
           for(i=0; i<s.size(); i++)
           {
              if (elements.contains(s.get(i)))
                  throw new IllegalArgumentException("Set(ArrayList<String>):duplicity not allowed in sets");
              elements.add(s.get(i));
           }
       }
     
        /**
        * creates a set using the elements of the array s.
        * @param s the array whose elements are used to
        *        create this set.
        * @throws IllegalArgumentException if s contains duplicity.
        */
       public Set(String[] s)
       {
           int i;
           elements = new ArrayList<String>();
           for(i=0; i<s.length; i++)
           {
              if (elements.contains(s[i]))
                  throw new IllegalArgumentException("Set(String[]):duplicity not allowed in sets");
              elements.add(s[i]);
           }
       }
     
       /**
        * determines whether a set contains the specified element
        * @param elt an element
        * @return true if elt is an element of this set; otherwise, false
        */
       public boolean isElement(String elt)
       {
           return elements.contains(elt);
       }
     
       /**
        * determines the size of this set.
        * @return the size of this set.
        */
       public int cardinality()
       {
           return elements.size();
       }
     
       /**
        * computes the intersection of this set and the
        * specified set.
        * @param s a set
        * @return a set representing the intersection of this set
        *         and s.
        */
       public Set intersect(Set s)
       {
           int i;
           ArrayList<String> result = new ArrayList<String>();
           for (i=0;i<s.cardinality();i++)
               if (this.isElement(s.elements.get(i)))
                   result.add(s.elements.get(i));
           return new Set(result);           
       }
     
       /**
        * computes the union of this set and the specified set.
        * @param s a sets
        * @return a set representing the union of this set
        *         and s.
        */
       public Set union(Set s)
       {
           int i;
           ArrayList<String> result = new ArrayList<String>();
           for (i=0; i<s.cardinality(); i++) 
         {
           if (elements.contains(i))
             result.add(s.elements.get(i));  		  
         }
        return new Set(result);   
       }
     
       /**
        * computes the difference between this set and the
        * specified set.
        * @param s a set
        * @return a set representing the difference between
        *         this set and s.
        */
       public Set diff(Set s)
       {
            int i;
           ArrayList<String> result = new ArrayList<String>();
           for (i=0; i<s.cardinality(); i++) 
         {
           if (elements.contains(i))
             result.remove(s.elements.get(i));  		  
         }
        return new Set(result); 
       }
     
       /**
        * computes the symmetric difference between this set
        * and the specified set.
        * @param s a set
        * @return a set representing the symmetrix difference
        *         between this set and s.
        */
       public Set symDiff(Set s)
       {
           //implement this method     
       }
     
       /**
        * computes the Cartesian product for this set
        * and the specified set.
        * @param s a set
        * @return a set representing the Cartesian product
        *         of this set and s.
        */
       public Set xProduct(Set s)
       {
           //implement this method
       }
     
     
       /**
        * determines whether a set is empty
        * @return true if this set is empty; otherwise, false
        */
       public boolean isEmpty()
       {
           return elements.isEmpty();
       }
     
       /**
        * determines whether this set is equal to the specified
        * set.
        * @param s a set
        * @return true if this set is equal to s; otherwise, false
        */
       public boolean equals(Set s)
       {
           //implement this method
       }
     
       /**
        * determines whether this set is a subset of the specified set.
        * @param s a set
        * @return true if this set is a subset of s; otherwise, false
        */
       public boolean subset(Set s)
       {
           //implement this method
     
       }
     
       /**
        * determines whether this set is a proper subset of the specified set.
        * @param s a set
        * @return true if this set is a proper subset of s; otherwise, false
        */
       public boolean properSubset(Set s)
       {
           //implement this method
       }
     
       /**
        * returns a string {x1,x2,...,xn} representing this set,
        * where x1,x2,...,xn are elements of this set.
        * @return a string representation of this set formatted
        *         as specified.
        */
       public String toString()
       {
           //implement this method            
       } 
    }

    this is what I have so far currently I'm stuck on sym diff


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Implementing basic operations on sets using Array Lists

    Quote Originally Posted by Bucs1992 View Post
    this is what I have so far currently I'm stuck on sym diff
    Hello Bucs!
    Where are you stuck? Can you explain what you are trying to do and is not working? Do you have the logic for the method?
    The symmetric difference can be expressed as the union without the intersection and as I can see you have implemented union and intersection.

Similar Threads

  1. Implementing order of operations for a Java calculator.
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: May 15th, 2011, 07:11 PM
  2. [SOLVED] implementing a dictionary of words using an array of linked lists
    By McTown in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 03:19 PM
  3. Operations with lists
    By datreta in forum Collections and Generics
    Replies: 8
    Last Post: October 29th, 2010, 09:54 AM
  4. Bulk operations on sets/ map problem
    By kyuss in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 14th, 2010, 01:48 PM
  5. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM