ArrayList (Unions, Intersection, Difference, Etc)

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4290 Views - Last Post: 18 November 2012 - 04:54 PM Rate Topic: -----

#1 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

ArrayList (Unions, Intersection, Difference, Etc)

Posted 14 November 2012 - 11:10 PM

I am working on a class assignment and I am having a difficult time understanding how to manipulate multiple arrays. More specifically, Union, Intersection, Difference, etc. of multiple sets.

This is what I have thus far.

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++)
      {
         
      }
   }

   /**
    * 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)
   {
      
   }
   
   /**
    * 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)
   {
          
   }
   
   /**
    * 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)
   {
       
   }


   /**
    * 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)
   {
      
   }

   /**
    * 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)
   {
       

   }

   /**
    * 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)
   {
       
   }
   
   /**
    * 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.
    */
   
    @Override
   public String toString()
   {
       return "Set="++"";            
   }  



Basically I need to implement all the methods. The public Set intersect(Set s) was given as a guide. I'm simply looking for guidance as I am new to Java. I know I need to use for loops and if statements and to check the boolean for each Array.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList (Unions, Intersection, Difference, Etc)

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2794
  • View blog
  • Posts: 11,830
  • Joined: 20-September 08

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 15 November 2012 - 04:32 AM

http://docs.oracle.c...til.Collection)
http://docs.oracle.c...til.Collection)

Check out the above
Was This Post Helpful? 1
  • +
  • -

#3 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 15 November 2012 - 12:51 PM

Quote



Unfortunately I cannot use those methods. I have a set method summary to use for my project. I cannot go outside of it.
Was This Post Helpful? 0
  • +
  • -

#4 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 15 November 2012 - 09:34 PM

Ok, this is what I have so far and it's not working.

public Set union(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.remove(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 (this.isElement(s.elements.get(i)) && s.isElement(s.elements.get(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)
   {
       int i;
       ArrayList<String> result = new ArrayList<String>();
       for(i=0;i<s.cardinality();i++)
       {
           if (this.isElement(s.elements.get(i)) && !s.isElement(s.elements.get(i)))
           {result.add(s.elements.get(i));}
       }
       return new Set(result);
   }
   
   /**
    * 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)
   {
       return elements.equals(s.elements);
   }

   /**
    * 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)
   {
        return elements.containsAll(s.elements);
   }

   /**
    * 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)
   {
       if(elements.equals(s.elements) && elements.containsAll(s.elements))
       {return false;}
       else{
           return true;
       }
       
   }
   
   /**
    * 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.
    */
   
    @Override
   public String toString()
   {
       //implement this method            
   }     

Was This Post Helpful? 0
  • +
  • -

#5 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 17 November 2012 - 03:06 PM

Ok, so I wrote most of the program and it seems to be outputting what I need.

I'm having some trouble with the Cartesian product method however.

I know I need to use loops, but I'm not sure where to begin.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 17 November 2012 - 03:47 PM

Do you have code you want help with?
Was This Post Helpful? 0
  • +
  • -

#7 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 17 November 2012 - 08:42 PM

It's this part:

 public Set xProduct(Set s)

   {

       //implement this method

   }



I have to implement a method to calculate the Cartesian product of this array and a specified array.

I assume I have to create a new arraylist.

Like

int i;
ArrayList<String> result = new ArrayList<String>();
       for (i=0;i<s.cardinality();i++)



This is where I'm stuck. I know there are methods available to simply calculate the Cartesian Product, but I'm not allowed to use them. I have to figure out how to iterate through each array and put them in a specific format.
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 05:29 AM

That's not code you need help with but an empty method you (apparently) want someone to write for you unless you're confused about what the Cartesian Product is and have questions about that.

Wikipedia gives a good example using the standard deck of cards: the X-product of a set of 13 card ranks and another set of 4 card suits results in the standard deck of 52 playing cards (13 x 4 members in the result).

Beyond the definition, what do you need help with? You mention iterating through the arrays and putting them in a specific format. What do you mean?

This post has been edited by GregBrannon: 18 November 2012 - 05:30 AM

Was This Post Helpful? 0
  • +
  • -

#9 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 08:36 AM

Very nice assumption of you. I think I simply asked for some guidance.

I know what the cartesian product is, and I also know what the output should look like.

public Set xProduct(Set s)
   {
       int i;
       int j;
       String p1;
        for(i=0;i<s.cardinality();i++)
        {
            for(j=0;j<s.cardinality();j++)
            {
                
            }
            
        }
       
   }



This is what I've written so far for it. I know I need to use two for loops.

P.S. If I wanted someone to write the code for me, I would have never mentioned that this was a class assignment.
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 09:11 AM

I'm just trying to understand what you need help with. Based on your latest, it seems you have it all figured out, except <blank>, and I don't know what the <blank> is.

Yes, two for loops that pair each element of one array with each element of the other then storing the resulting pairing in a new array is an approach that should work. What's preventing you from writing that?
Was This Post Helpful? 0
  • +
  • -

#11 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 09:24 AM

It's telling me duplicity not allowed; which is a boolean I wrote further up the code.

I'm thinking I need to somehow "switch" that off just for this statement.

What's keeping me from writing it is I don't know how to write that for nested loops. This is the first nested for loop I code for this class.
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 09:59 AM

Quote

It's telling me duplicity not allowed; which is a boolean I wrote further up the code.

Post the whole error message and point to the code it's complaining about.

An example nested for loop that iterates 2 arrays and pairs their contents into a new array (untested but should be close):
// newArray is an array of arrays of the form
// [ [a, b], [c, d], [e, f], etc . . . ] and
// is declared and initialized in advance of
// of these loops where a, b, c, d, . . . are
// of type "TypeArray"
for ( int i = 0 ; i < array1.length ; i++ )
{
    for ( int j = 0 ; j < array2.length ; j++ )
    {
        TypeArray[][] newElement = { array1[i], array2[j] };
        newArray[( i * ( array2.length + 1 ) ) + j ] = newElement;
    }
}


Hope that helps.

Edit: This suggested approach is improved and tested in Post #17.

This post has been edited by GregBrannon: 18 November 2012 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#13 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 01:18 PM

Nah, I understand the reasoning behind nested for loops, but the rest is still confusing me a bit.

The output has to be in the form
(x1,y1),(x1,y2),(x1,y3)

Here is an example.

S4 = {2, 4, 6, 8} ⊗ {2, 3, 5, 7}
S4 = {(2, 2), (2, 3), (2, 5), (2, 7), (4, 2), (4, 3), (4, 5), (4, 7), (6, 2), (6, 3), (6, 5), (6, 7), (8, 2), (8, 3), (8, 5), (8, 7)}

So, I'm using the outer loop to handle iterating through the first Set, and the inner loop to iterate through the second set.

I'm still at:

   public Set xProduct(Set s)
   {
       int i;
       int j;
       ArrayList<String> result = new ArrayList<String>();
        for(i=0;i<this.cardinality();i++)
        {
            for(j=0;j<s.cardinality();j++)
            {
               
            }
            
        }
       return new Set(result);
   }

Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 02:18 PM

Using an ArrayList<> simplifies things some. Here's a simple example:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestClass
{
    public static void main(String [] args)
    {
        String[] rank = { "Ace", "Two", "Three", "Four", "Five" };
        String[] suit = { "Spades", "Diamonds", "Clubs", "Hearts" };
        List<String[]> deck = new ArrayList<String[]>();

        for ( int i = 0 ; i < rank.length ; i++ )
        {
            for ( int j = 0 ; j < suit.length ; j++ )
            {
                String[] newElement = { rank[i], suit[j] };
                deck.add( newElement );
            }
        }

        System.out.println( Arrays.deepToString( ( deck.toArray() ) ) );
    }
}

You may have to write your own toString() method to get the exact look you've posted.
Was This Post Helpful? 0
  • +
  • -

#15 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 02:50 PM

How I did it. But it's wrong.

public Set xProduct(Set s)
   {
       int i;
       int j;
       ArrayList<String> result = new ArrayList<String>();
        for(i=0;i<this.cardinality();i++)
        {
            for(j=0;j<s.cardinality();j++)
            {
                 ArrayList<String> xproduct = new ArrayList<String>();
                 xproduct.add(this.elements.get(i));
                 xproduct.add(s.elements.get(j));
                 result.addAll(xproduct);
            }
            
        }
       return new Set(result);
   }


Error I'm getting

Exception in thread "main" java.lang.IllegalArgumentException: Set(ArrayList<String>)duplicity not allowed in sets
{[2, 4, 6, 8]} ⊖ {[2, 3, 5, 7]} ∪ {[2, 4, 6, 8]} ∩ {[2, 3, 5, 7]} ∪ {[2, 3, 5, 7]} ⊖ {[2, 4, 6, 8]} = {[4, 6, 8, 2, 3, 5, 7]}
	at setapplication.Set.<init>(Set.java:34)
	at setapplication.Set.xProduct(Set.java:189)
	at setapplication.Set.main(Set.java:279)
Java Result: 1



It's frustrating. I know what I want it to do. I know the process of how to do it, but coding it write is eluding me.

Code it right, I mean.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2