Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface Conjunto {
- // Descripcion general: Concepto matemático de conjunto
- public void añadir(int n);
- // Pre : ---
- // Post: Se ha añadido n al conjunto
- public boolean pertenece(int n);
- // Pre : ---
- // Post: Dice si n pertenece al conjunto
- public Conjunto union(Conjunto c);
- // Pre : ---
- // Post: Devuelve la union de este conjunto con c
- public int nelem();
- // Pre : ---
- // Post: Dice cuantos elementos tiene el conjunto
- }
- public class ConjuntoTabla implements Conjunto {
- //Descripció general: Implementación en forma de tabla de enteros
- private int tablaInt[]; //Tabla de enteros ordenada de forma creciente
- public void añadir(int n)
- //Pre: --
- //Post: Se ha añadido n al conjunto ordenadamente
- {
- if (!this.pertanece(n)) { //No puede contener elementos duplicados
- int i = this.nelem();
- while (i > 0 && n < this.tablaInt[i-1]) {
- this.taulaInt[i] = this.tablaInt[i-1];
- i--;
- }
- this.tablaInt[i] = n;
- }
- }
- public Conjunto union(Conjunto c) {
- Conjunto union = new ConjuntoTabla();
- for (int i=0; i < this.nelem(); i++) {
- [B] union.tablaInt[i] = this.tablaInt[i];[/B] //Hace una copia del array actual para evitar modificarlo
- }
- for (int i=0; i < c.nelem(); i++) {
- [B]union.añadir(c.tablaInt[i]);[/B] //Ya comprueba que no haya duplicados
- }
- return union;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement