Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with programming assignment? //Starter Code package set; import java.util.A

ID: 3854248 • Letter: H

Question

Help with programming assignment?

//Starter Code


package set;

import java.util.ArrayList;

/**
* A generic set implementation
*/

public class Set<T>
{
/**
* A list of elements contained in the set
*/
public ArrayList<T> elements;

/**
* Creates a set using the elements of the ArrayList list.
* @param list the ArrayList whose elements are used to create this set.
* @throws IllegalArgumentException if list contains a duplicity.
*/
public Set(ArrayList<T> list)
{
elements = new ArrayList<T>();
int i, size = list.size();
T elt;
for(i = 0; i < size; i++)
{
elt = list.get(i);
if (elements.contains(elt))
throw new IllegalArgumentException("Set<T>: Duplicity not "
+ "allowed in sets");
elements.add(elt);
}
}

/**
* 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(T elt)
{
return elements.contains(elt);
}

/**
* Determines whether a set is empty
* @return true if this set is empty; otherwise, false
*/
public boolean isEmpty()
{
return elements.isEmpty();
}
  
/**
* 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<T> intersect(Set<T> s)
{
T elt;
ArrayList<T> result = new ArrayList();
   int i, size = elements.size();
for (i = 0; i < size; i++)
{
elt = elements.get(i);
if (s.elements.contains(elt))
result.add(elt);
}
return new Set(result);   
}

/**
* Computes the union of this set and the specified set.
* @param s a set
* @return a set representing the union of this set and s.
*/
public Set<T> union(Set<T> s)
{
// implement this method
Set<T> union = Set.union(elt,s);
}

/**
* Computes the difference of this set and the specified set.
* @param s a set
* @return a set representing the difference of this set and s.
*/
public Set<T> diff(Set<T> s)
{
// implement this method
ArrayList<T> diff = elements();
   int i, size = elements.size();
for (i = 0; i < size; i++)
{
= elements.get(i);
if (s.elements.contains(elt))
result.add(elt);
}
return new Set(result)

}
}
/**
* Determines whether this set is equal to the specified set.
* @param obj an object
* @return false if the specified object is not equal to this set;
* otherwise, true
*/
public boolean equals(Object obj)
{
// implement this method
}

/**
* Determines whether this set is a subset of the specified set.
* @param param s a set
* @return false if this set is not a subset of the specified set;
* otherwise, true
*/
public boolean subset(Set<T> s)
{
// implement this method

}


/**
* Determines whether this set is a proper subset of the specified set.
* @param param s a set
* @return false if this set is not a proper subset of the specified set;
* otherwise, true
*/
public boolean properSubset(Set<T> 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()
{
String setAsString = "{";
int size = elements.size();
if (size > 0)
   {
setAsString += elements.get(0);
int i;
for (i = 1; i < size; i++)
setAsString += ", " + elements.get(i);   
}
return setAsString+"}";
}
}

Csc 1351-03 Spring 2017, Lab 8: Project Set create a project named set that illustrates the use of generic classes and generic methods mplementing a Generic Set Class DEFINITION 1. Generics is a way of defining classes, interfaces and methods by using type pa rameters to represent types. A more technical term for generics is parametric polymorphism A generic method, interface or class is fully specified at run time. In Java, a generic type can only be specialized using an object type. Every primitive type in Java has an associated object type (a wrapper class DEFINITION 2. A set is a finite or infinite collection of objects in which order has no significance and multiplicity is generally also ignored. Members of a set are often referred to as elements and the notation r E A is used to denote that r s an element of a set A. A set is usually denoted as a list of elements. For example, 12. 3. 4, 5. G is a set that contains five elements. In today's lab, you will implement a generic class, the set T class. The class will be generic but its methods will not be generic. Some methods will use the formal type parameter of the class. In this lab, we introduce a basic software engineering concept called composition one object is composed of another. A set is composed of a list We will use the Java API ArrayList class Basic Set Operations DEFINITION 3. The cardinality of a set is the number of elements that the set contains. For example, the cardinality of A f2.3, 4, 5, 6h, denoted A is 5 DEFINITION 4. The intersection of two sets A and B is the set of elements common to A and B. This is written An B.and is pronounced "intersection" or "cap DEFINITION 5. The union of two sets A and B is the set obtained by combining the members of each without allowing multiplicity. This is written A u B, and is pronounced "union" or "cup DEFINITION 6. The difference of sets A and B denoted A-B, is the set of elements belonging to set A but not B

Explanation / Answer

package set;

import java.util.ArrayList;

/**
* A generic set implementation
*/

public class Set<T> {
   /**
   * A list of elements contained in the set
   */
   public ArrayList<T> elements;

   /**
   * Creates a set using the elements of the ArrayList list.
   *
   * @param list
   * the ArrayList whose elements are used to create this set.
   * @throws IllegalArgumentException
   * if list contains a duplicity.
   */
   public Set(ArrayList<T> list) {
       elements = new ArrayList<T>();
       int i, size = list.size();
       T elt;
       for (i = 0; i < size; i++) {
           elt = list.get(i);
           if (elements.contains(elt))
               throw new IllegalArgumentException("Set<T>: Duplicity not " + "allowed in sets");
           elements.add(elt);
       }
   }

   /**
   * 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(T elt) {
       return elements.contains(elt);
   }

   /**
   * Determines whether a set is empty
   *
   * @return true if this set is empty; otherwise, false
   */
   public boolean isEmpty() {
       return elements.isEmpty();
   }

   /**
   * 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<T> intersect(Set<T> s) {
       T elt;
       ArrayList<T> result = new ArrayList();
       int i, size = elements.size();
       for (i = 0; i < size; i++) {
           elt = elements.get(i);
           if (s.elements.contains(elt))
               result.add(elt);
       }
       return new Set(result);
   }

   /**
   * Computes the union of this set and the specified set.
   *
   * @param s
   * a set
   * @return a set representing the union of this set and s.
   */
   public Set<T> union(Set<T> s) {
       // implement this method
       //0Set<T> union = Set.union(elt, s);
       T elt;
       ArrayList<T> result = new ArrayList();
       int i, size = elements.size();
       for (i = 0; i < size; i++) {
           elt = elements.get(i);
           result.add(elt);
       }
       for (i = 0; i < s.elements.size(); i++) {
           elt = s.elements.get(i);
           if(!result.contains(elt)){
               result.add(elt);
           }
       }
       return new Set(result);
   }

   /**
* Computes the difference of this set and the specified set.
* @param s a set
* @return a set representing the difference of this set and s.
*/
public Set<T> diff(Set<T> s)
{
     
// implement this method
//ArrayList<T> diff = s.elements;
ArrayList<T> result = new ArrayList<T>();
int i, size = elements.size();
for (i = 0; i < size; i++){
   T elt = elements.get(i);
if (!s.elements.contains(elt))
result.add(elt);
}
return new Set(result);

}

   /**
   * Determines whether this set is equal to the specified set.
   *
   * @param obj
   * an object
   * @return false if the specified object is not equal to this set;
   * otherwise, true
   */
   public boolean equals(Object obj) {
      
       // implement this method
       T elt;
       Set<T> compSet = (Set<T>) obj;
       if(compSet.elements.size() != elements.size()){
           return false;
       }
       int i, size = elements.size();
       for (i = 0; i < size; i++) {
           elt = elements.get(i);
           if (!compSet.elements.contains(elt))
               return false;
       }
       return true;
   }

   /**
   * Determines whether this set is a subset of the specified set.
   *
   * @param param
   * s a set
   * @return false if this set is not a subset of the specified set;
   * otherwise, true
   */
   public boolean subset(Set<T> s) {
      
       T elt;
       int i, size = elements.size();
       for (i = 0; i < size; i++) {
           elt = elements.get(i);
           if (!s.elements.contains(elt))
               return false;
       }
       return true;
   }

   /**
   * Determines whether this set is a proper subset of the specified set.
   *
   * @param param
   * s a set
   * @return false if this set is not a proper subset of the specified set;
   * otherwise, true
   */
   public boolean properSubset(Set<T> s) {
       // implement this method
       if(subset(s) && !equals(s)){
           return true;
       }
       return false;
   }

   /**
   * 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() {
       String setAsString = "{";
       int size = elements.size();
       if (size > 0) {
           setAsString += elements.get(0);
           int i;
           for (i = 1; i < size; i++)
               setAsString += ", " + elements.get(i);
       }
       return setAsString + "}";
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote