Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. package ajudatp2;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Conjunto {
  7.    
  8.         private ArrayList<Integer> conjunto;
  9.         private int tamanho;
  10.         public Conjunto() {
  11.             this.conjunto = new ArrayList<Integer>();
  12.             tamanho = 0;
  13.         }
  14.  
  15.         public void adicionarAoConjunto(int n) {
  16.            
  17.            
  18.             if(this.tamanho == 0) {
  19.                 if(conjunto.contains(n)) {
  20.                     System.out.println("Numero Já existe");
  21.                     return;
  22.                 }
  23.                 conjunto.add(n);
  24.                 tamanho++;
  25.             }else {
  26.                 if(conjunto.contains(n)) {
  27.                     System.out.println("Numero Já existe");
  28.                     return;
  29.                 }
  30.                 this.ordenar(conjunto.size()-1,n);
  31.                 tamanho++;
  32.             }
  33.         }
  34.        
  35.         private void ordenar(int index,int n){
  36.             int valor = conjunto.get(index);
  37.             if(index == 0) {
  38.                 if(n>valor) {
  39.                     conjunto.add(n);
  40.                 }else {
  41.                 int numremovido = conjunto.remove(0);
  42.                 conjunto.add(n);
  43.                 conjunto.add(numremovido);
  44.                 }
  45.             }else {
  46.                 if(n>valor) {
  47.                 conjunto.add(n);
  48.                 }else {
  49.                 int removido = conjunto.remove(index);
  50.                 ordenar(index-1,n);
  51.                 conjunto.add(removido);
  52.                 }
  53.             }}
  54.  
  55.         public int removerDoConjunto(int n) {
  56.             int valor = Integer.MIN_VALUE;
  57.             for (int i = 0; i < conjunto.size(); i++) {
  58.                 int atual = conjunto.get(i);
  59.                 if (n == atual ) {
  60.                     valor = atual;
  61.                     tamanho--;
  62.                     return valor;
  63.                  }else if(valor > n) {
  64.                     System.out.println("O numero não existe");
  65.                     break;
  66.                 }
  67.             }
  68.             return valor;
  69.         }
  70.  
  71.         public void imprimeConjunto() {
  72.             System.out.println("O conjunto é:");
  73.             for (int i = 0; i < conjunto.size(); i++) {
  74.                 System.out.print(conjunto.get(i)+",");
  75.             }
  76.         }
  77.         public int retornaTamanho() {
  78.             return this.tamanho;
  79.         }
  80.        
  81.         public void Unir(Conjunto uniao,Conjunto conj) {
  82.             int valor1 = Integer.MIN_VALUE;
  83.             int valor2  = Integer.MIN_VALUE;
  84.             if(conj.retornaTamanho()>0) {
  85.                 valor1 = conj.removerDoConjunto(conj.conjunto.get((conj.retornaTamanho()-1)));
  86.             }
  87.             if(this.retornaTamanho()>0) {
  88.                 valor2 = this.removerDoConjunto(this.conjunto.get(this.retornaTamanho()-1));
  89.             }
  90.        
  91.             if(valor1 != Integer.MIN_VALUE) {
  92.                 uniao.adicionarAoConjunto(valor1);
  93.             }
  94.             if(valor2 != Integer.MIN_VALUE) {
  95.                 uniao.adicionarAoConjunto(valor2);
  96.             }
  97.             if(valor1 == Integer.MIN_VALUE && valor2 == Integer.MIN_VALUE) {
  98.                 return;
  99.             }
  100.             this.Unir(uniao, conj);
  101.        
  102.         }
  103.         public void interseccao(Conjunto intersec,Conjunto conj) {
  104.             if(this.retornaTamanho() == 0) {
  105.                 int valor = this.removerDoConjunto(this.conjunto.get(0));
  106.                 if(conj.conjunto.contains(valor)) {
  107.                     intersec.adicionarAoConjunto(valor);
  108.                 }
  109.                 return;
  110.             }
  111.             int valor = this.removerDoConjunto(this.conjunto.get(this.retornaTamanho()-1));
  112.             if(conj.conjunto.contains(valor)) {
  113.                 intersec.adicionarAoConjunto(valor);
  114.             }
  115.             this.interseccao(intersec,conj);
  116.         }
  117.        
  118.         public void dif(Conjunto dif,Conjunto conj) {
  119.             if(this.retornaTamanho() == 0) {
  120.                 int valor = this.removerDoConjunto(this.conjunto.get(0));
  121.                 if(!conj.conjunto.contains(valor)) {
  122.                     dif.adicionarAoConjunto(valor);
  123.                 }
  124.                 return;
  125.             }
  126.             int valor = this.removerDoConjunto(this.conjunto.get(this.retornaTamanho()-1));
  127.             if(!conj.conjunto.contains(valor)) {
  128.                 dif.adicionarAoConjunto(valor);
  129.             }
  130.             this.dif(dif,conj);
  131.         }
  132.         public static void main(String[] args) {
  133.             Conjunto conj = new Conjunto();
  134.             Conjunto conj2 = new Conjunto();
  135.             Conjunto Uniao = new Conjunto();
  136.             Conjunto Intersec = new Conjunto();
  137.             conj2.adicionarAoConjunto(0);
  138.             conj2.adicionarAoConjunto(2);
  139.             conj2.adicionarAoConjunto(9);
  140.             conj2.adicionarAoConjunto(13);
  141.             conj2.adicionarAoConjunto(15);
  142.             conj.adicionarAoConjunto(0);
  143.             conj.adicionarAoConjunto(2);
  144.             conj.adicionarAoConjunto(5);
  145.             conj.adicionarAoConjunto(7);
  146.             conj.imprimeConjunto();
  147.             System.out.println("====================");
  148.             conj.adicionarAoConjunto(3);
  149.             conj.imprimeConjunto();
  150.             System.out.println("");
  151.             conj.retornaTamanho();
  152.             conj.dif(Intersec, conj2);
  153.             Intersec.imprimeConjunto();
  154.         }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement