
/***** Exercice 1 *****/

public class Set
{
    private boolean[] tab ;
    private int capacity ;
    private int cardinal ;
    
    public Set(int nsup)
    {
        capacity = Math.max(10,nsup) ;
        tab = new boolean[capacity] ;
        for(int i=0; i<capacity; i++) tab[i]=false; // non indispensable
        cardinal = 0 ;
    }
    
    public Set(int[] t)
    {
        this(t[t.length-1]+1);
        for(int i=0; i<t.length; i++) tab[t[i]]=true;
        cardinal = t.length ;
    }

    public int capacity() {
        return capacity ;
    }

    public int cardinal() {
        return cardinal ;
    }

    public boolean isElement(int n) {
        return (0<=n) && (n<capacity) && tab[n];
    }
    
    public void add(int n) {
        if (0<=n && n<capacity && !tab[n]) {
            tab[n]=true;
            cardinal++;
        }
    }

    public void addExtended(int n) {
	  if (n<0 || isElement(n)) return ;
	  if (n>=capacity) {
           	boolean[] newtab = new boolean[n+1] ;
            for(int i=0; i<capacity; i++) newtab[i] = tab[i] ;
            for(int i=capacity; i<n; i++) newtab[i] = false ;
            tab = newtab ;
            capacity = n ;
        }
        tab[n] = true ;
        cardinal++ ;
    }

    public void complement() {
        for(int i=0; i<capacity; i++) tab[i] = !tab[i];
        cardinal = capacity - cardinal ;
    }
    
    public Set inter(Set s) {
        int dim = Math.min(capacity, s.capacity()) ;
        Set e = new Set(dim);
        for(int i=0; i<dim; i++)
            if (tab[i] && s.isElement(i))  e.add(i) ;
        return e ;
    }

    public String toString() {
	  String s = "" ;
	  for(int i=0; i<capacity; i++)
		if (tab[i]) s += i + " " ;
	  return s ;
    }
    
} // end class Set



/***** Exercice 2 *****/

public class Test
{
    public static void main (String[] args) { 
        int[] t1 = {10, 100, 1000} ;
	  Set s1 = new Set(t1) ;
	  
        int[] t2 = {1, 10, 100, 110} ;
        Set s2 = new Set(t2) ;

	  Set s3 = s1.inter(s2) ;
	  System.out.println(s3.toString()) ;
    }

} // end class Test


