lunes, 29 de agosto de 2011

Ejercicio "Conjunto"

Crear la clase Conjunto<T>, que implementará la interfaz Set<T>. No se pondrá código a todas las funciones de Set<T>, tan sólo aquellas que nos interesan de momento.
Para almacenar todos los objetos que forman parte del conjunto, se empleará un ArrayList. Además, se creará la clase privada ParaRecorrerMiConjunto, alojada dentro de la clase Conjunto<T>, que implementa Iterator<T> y nos permite recorrer todos los elementos que forman parte del conjunto.

public class Conjunto<T> implements Set<T> {
    private ArrayList<T> datos;
    private class ParaRecorrerMiConjunto implements Iterator<T> {
        private int actual = -1;
        public boolean hasNext() {
            return actual < datos.size() - 1;
        }
        public T next() {
            if (!hasNext()) {
                throw new java.util.NoSuchElementException();
            }
            actual++;
            return datos.get(actual);
        }
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    public Conjunto() {
        datos = new ArrayList<T>();
    }
    public void clear() {
        datos.clear();
    }
    @SuppressWarnings("element-type-mismatch")
    public boolean contains(Object o) {
        return datos.contains(o);
    }
    public int size() {
        return datos.size();
    }
    public boolean isEmpty() {
        return datos.isEmpty();
    }
    public boolean add(T e) {
        if (!datos.contains(e)) {
            return datos.add(e);
        }
        return false;
    }
    @SuppressWarnings("element-type-mismatch")
    public boolean remove(Object o) {
        return datos.remove(o);
    }
    @Override
    public String toString() {
        return datos.toString();
    }
    @Override
    public boolean equals(Object o) throws ClassCastException {
        Conjunto<T> aux = (Conjunto<T>) o;
        return this.datos.containsAll(aux.datos) & aux.datos.containsAll(this.datos);
    }
    public Iterator<T> iterator() {
        return new ParaRecorrerMiConjunto();
    }
    public Object[] toArray() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public Object[] toArray(Object[] a) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public boolean containsAll(Collection c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public boolean addAll(Collection c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public boolean retainAll(Collection c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public boolean removeAll(Collection c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

MODIFICACIÓN
Para que funcione realmente como un conjunto de cosas, modificaremos la clase ParaRecorrerMiConjunto, de manera que cada vez que se instancie un iterador para recorrer los elementos del conjunto, lo haga en orden aleatorio.

public class Conjunto3<T> implements Set<T> {
    private ArrayList<T> datos;
    private class ParaRecorrerMiConjunto implements Iterator<T>{
        private int actual=-1;
        private ArrayList<T> aux;
        ParaRecorrerMiConjunto(){
            aux = new ArrayList<T>(datos);
            Collections.shuffle(aux);
        }
        public boolean hasNext() {
            return !aux.isEmpty();
        }
        public T next() {
            if(!hasNext())
                throw new java.util.NoSuchElementException();
            actual++;
            return aux.get(actual);
        }
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    public Conjunto3() {
        datos = new ArrayList<T>();
    }
    public void clear() {
        datos.clear();
    }
    @SuppressWarnings("element-type-mismatch")
    public boolean contains(Object o) {
        return datos.contains(o);
    }
    public int size() {
        return datos.size();
    }
    public boolean isEmpty() {
        return datos.isEmpty();
    }
    public boolean add(T e) {
        if (!datos.contains(e)) {
            return datos.add(e);
        }
        return false;
    }
    @SuppressWarnings("element-type-mismatch")
    public boolean remove(Object o) {
        return datos.remove(o);
    }
    @Override
    public String toString() {
        return datos.toString();
    }
    @Override
    public boolean equals(Object o) throws ClassCastException {
        Conjunto3<T> aux = (Conjunto3<T>) o;
        return this.datos.containsAll(aux.datos) & aux.datos.containsAll(this.datos);
    }
    public Iterator<T> iterator() {
        return new ParaRecorrerMiConjunto();
    }
    public Object[] toArray() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public Object[] toArray(Object[] a) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public boolean containsAll(Collection c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public boolean addAll(Collection c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public boolean retainAll(Collection c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public boolean removeAll(Collection c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

No hay comentarios:

Publicar un comentario