All pastes #395586 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-03-15 03:56 UTC
rendered paste body
package nonNegIntSet2;

import java.util.ArrayList;

/**
 * @author rupchurch<br>
 * 
 * Created on Mar. 7, 2007<br>
 * Provides for creating and manipulating sets of non-negative integers (0...).
 *
 */

public class NonNegIntSet {
    
//Attributes
    private final int DEFAULT_SIZE=100;
    private ArrayList<Integer> contents = new ArrayList(DEFAULT_SIZE);

    
//Constructors

//
/**
 * Creates the empty set<br>
 * Postcondition: Creates an empty set, cardinality()==0 
 */
NonNegIntSet(){

}

/**
 * Creates a set from the values of the array that are greater than or equal to 0<br>
 * Postcondition: For all x in values, such that 0<=x, isElement(x).
 * @param values - ints which become part of the set
 */
NonNegIntSet(int[] values){
	
	for(int i: values)
    	this.insert(i);
}

//Set Methods

/**
 * 
 * Precondition: none<br>
 * Postcondition: true is the set is empty, false otherwise
 *@return true is the set is empty, false otherwise
 */
public boolean isEmpty(){
	return this.contents.size() == 0;
	} 

/**
 * 
 * Precondition: none<br>
 * Postcondition: return the number of elements in the set
 *@return the number of elements in the set
 */
public int cardinality(){
    return this.contents.size();}

/**
 * 
 * Precondition: none<br>
 * Postcondition: if 0<=element then return this.contents[element] else return false
 *@param element the int for inclusion in the set
 *@return true if element is in the set, false otherwise
 */
public boolean isElement(int element){
	return this.contents.contains(element);
    	
} 

/**
 * 
 * Precondition: none<br>
 * Postcondition:  if every element of s is an element of this return true else return false
 *@param s -  the set to test for containment
 *@return if s is a subset of this then return true else return false
 */
public boolean contains(NonNegIntSet s){
	
    if (this.contents.size() >= s.contents.size()){
    	for(int i: s.contents){
    		if(!this.contents.contains(i))
    			return false;
    	}
    	return true;
    }
    else return false;	
} 

/**
 * 
 * Precondition: none<br>
 * Postcondition: returns true if the two sets contain precisely the same elements, otherwise false
 *@param s -  the NonNegIntSet to compare to this
 *@return true is the sets have the same elements, false if they do not
 */
public boolean equals(NonNegIntSet s){
	
	boolean result=true;
    if(this.contents.size() != s.contents.size())
    	result=false;
    else {
    	for(int i: this.contents){
    		if(this.contents.contains(i) == s.contents.contains(i))
    			result = true;
    		else
    			return false;
    	}
    }
    
    return result;} 

/**
 * 
 * Precondition: none<br>
 * Postcondition: the specified elemetn is added to this Set if it is not already present.  
 * If this Set already contained ths epecified element, the set is left unchanged and flase has been returned.  
 * If the element has been inserted in the Set, true has been returned.
 *@param element - the int to be added to the set
 *@return true is the sets have the same elements, false if they do not
 */
public boolean  insert(int element){
	boolean result = false;
	
	if(element>= 0){
		this.contents.ensureCapacity(element);
		result =! isElement(element);
		if(!isElement(element)){
			this.contents.add(element);
			result = true;
			return result;
		}
	}
	
    return false;

	} 
//
/**
 * 
 * Precondition: none<br>
 * Postcondition: element has been removed from the Set, if it was present and true was returned, return false otherwise
 *@param element the int to be removed from the set
 *@return true if the element is removed from the set, false otherwise
 */
public boolean remove(int element){
	
		boolean result=false;
	    if(isElement(element)){
	    	this.contents.remove(new Integer(element));
	    	result=true;
	    }
	    return result;}

/**
 * 
 * Precondition: none<br>
 * Postcondition: this.equals(this.copy()) && this!=this.copy()
 *@return NonNegIntSet which has the same elements as this
 */
public NonNegIntSet copy(){
	
	NonNegIntSet a = new NonNegIntSet();
	
	for(int i: this.contents)
			a.insert(i);
	
		return a;}


//Operations

/**
 * 
 * Precondition: none<br>
 * Postcondition: for all x in the result, this.isElement(x) || s.isElement(x)
 *@param s - the set of elements to combine with the element of this
 *@return the set consisting of those elements in this or in s
 */
public NonNegIntSet union(NonNegIntSet s){
	
	for(int i: s.contents)
		if(!this.contents.contains(i)){
			this.insert(i);
		}
		return this;} 

/**
 * 
 * Precondition: none<br>
 * Postcondition: for all x in the result, this.isElement(x) && s.isElement(x)
 *@param s the set of elements to find in common with this
 *@return the set of all elements the two sets have in common
 */            
  public NonNegIntSet intersection(NonNegIntSet s){
	  
	  NonNegIntSet a = new NonNegIntSet();
      
	  for(int i: this.contents) {	
    	  if (this.isElement(i) && s.isElement(i)) 
    		  a.insert(i);}
      return a;
  } 


  /**
   * 
   * Precondition: none<br>
   * Postcondition: for all x in the result, this.isElement(x) && !s.isElement(x)
   *@param s - the set whose element are not to be in the result
   *@return the set of elements in this that are not in s
   */
  public NonNegIntSet difference(NonNegIntSet s){
	  
	  NonNegIntSet a = new NonNegIntSet();
	  
	  for(int i: this.contents) {	
    	  if (this.isElement(i) && !s.isElement(i)) 
    		  a.insert(i);
	  }
	  return a;
     } 


  //Utilities
 
  
  /**
   * The representation is {int, int, int} beginning with the smallest value 
   *  in the set to the largest from left to right.
   *@return a String representation of the  Set
   */
public String toString(){
	
	String result="{";
    int elementcount=0;
    for(int i: this.contents)
	if(isElement(i))
	    if(elementcount>0) 
	    	result+=", "+i;
	    else {
		elementcount++;
		result+=i;
	    }
    return result+"}";} 
   
   //Needed for junit testing. visibility must be public due to inheritance
   //DO NOT MODIFY
    public boolean equals(Object o){
        if (o.getClass() == this.getClass())
            return this.equals((NonNegIntSet)o);
        else
            return false;
    }

    

}