0

我搜索了整个论坛,找不到我要找的东西。我正在为学校做作业,我需要实施一些方法。我做了大部分,得到了我需要的输出。但是,我在实现笛卡尔积 (xproduct) 方法时遇到了麻烦。

到目前为止,这是我的代码:

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>();
       result.addAll(this.elements);
       result.addAll(s.elements);
       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>();
       result.addAll(this.elements);
       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 symmetric difference between this set
    * and the specified set.
    * @param s a set
    * @return a set representing the symmetrical difference
    *         between this set and s.
    */

   public Set symDiff(Set s)
   {
       int i;
       ArrayList<String> result = new ArrayList<String>();
       result.addAll(this.elements);
       result.addAll(s.elements);
       for(i=0;i<s.cardinality();i++)
       {
           if (this.isElement(s.elements.get(i)) && s.isElement(this.elements.get(i)))
           {result.remove(this.elements.get(i));
            result.remove(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)
   {
       int i;
       ArrayList<String> result = new ArrayList<String>();
       result.addAll(this.elements);
       result.addAll(s.elements);

   }


   /**
    * 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()
   {
       return "{"+this.elements+"}";          
   }     


    public static void main(String[] args)
    {
       String[]a1 = {"2","4","6","8"};
       String[]a2 = {"2","3","5","7"};
       String[]a3 = {"1","3","5"};
    Set s1 = new Set(a1);
    Set s2 = new Set(a2); 
    Set s3 = new Set(a3); 

        System.out.print("S1 ="); System.out.printf("%s",s1);
        System.out.println();
        System.out.print("S2 ="); System.out.printf("%s",s2);
        System.out.println();
        System.out.print("S3 ="); System.out.printf("%s",s3);
        System.out.println();System.out.println();

       System.out.println("(S1 \u222A S2:)");
       System.out.printf("%s \u222A %s = %s%n",s1,s2,s1.union(s2));
       System.out.println();

       System.out.println("(S1 \u2296 S2) \u222a (S1 \u2229 S2) \u222a (S2 \u2296 S1)");
       System.out.printf("%s \u2296 %s \u222a %s \u2229 %s \u222a %s \u2296 %s = %s%n",s1,s2,s1,s2,s2,s1,s1.diff(s2).union(s1.intersect(s2).union(s2.diff(s1))));

       //Cartesian Product of s1 and s2
       //Cartesian product of s2 and s1


    }
}

任何指导将不胜感激。

4

1 回答 1

1

我不明白你为什么有困难,但 Set1 x Set2 的笛卡尔积可以这样计算:

Product = {}
Set1. iterate with i
    Set2.iterate with j
        Product.append (pair(i,j))

如果您仍然有困难,请告诉我用 Java 编写代码。现在我只发布了伪代码。

于 2012-11-17T22:35:38.437 回答