Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. public interface Conjunto {
  2. // Descripcion general: Concepto matemático de conjunto
  3.  
  4. public void añadir(int n);
  5. // Pre : ---
  6. // Post: Se ha añadido n al conjunto
  7.  
  8. public boolean pertenece(int n);
  9. // Pre : ---
  10. // Post: Dice si n pertenece al conjunto
  11.  
  12. public Conjunto union(Conjunto c);
  13. // Pre : ---
  14. // Post: Devuelve la union de este conjunto con c
  15.  
  16. public int nelem();
  17. // Pre : ---
  18. // Post: Dice cuantos elementos tiene el conjunto
  19. }
  20.  
  21. public class ConjuntoTabla implements Conjunto {
  22. //Descripció general: Implementación en forma de tabla de enteros
  23.  
  24. private int tablaInt[]; //Tabla de enteros ordenada de forma creciente
  25.  
  26. public void añadir(int n)
  27. //Pre: --
  28. //Post: Se ha añadido n al conjunto ordenadamente
  29. {
  30. if (!this.pertanece(n)) { //No puede contener elementos duplicados
  31. int i = this.nelem();
  32. while (i > 0 && n < this.tablaInt[i-1]) {
  33. this.taulaInt[i] = this.tablaInt[i-1];
  34. i--;
  35. }
  36. this.tablaInt[i] = n;
  37. }
  38. }
  39.  
  40.  
  41. public Conjunto union(Conjunto c) {
  42. Conjunto union = new ConjuntoTabla();
  43. for (int i=0; i < this.nelem(); i++) {
  44. [B] union.tablaInt[i] = this.tablaInt[i];[/B] //Hace una copia del array actual para evitar modificarlo
  45. }
  46. for (int i=0; i < c.nelem(); i++) {
  47. [B]union.añadir(c.tablaInt[i]);[/B] //Ya comprueba que no haya duplicados
  48. }
  49. return union;
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement