Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://www.caelum.com.br/apostila-java-orientacao-objetos/classes-abstratas/#aumentando-o-exemplo
- class Main {
- public static void main (String args[]) {
- //Dez primeiros numeros naturais
- Conjunto n1 = new Conjunto(10);
- n1.inserirValor(0);
- n1.inserirValor(1);
- n1.inserirValor(2);
- n1.inserirValor(3);
- n1.inserirValor(4);
- n1.inserirValor(5);
- n1.inserirValor(6);
- n1.inserirValor(7);
- n1.inserirValor(8);
- n1.inserirValor(9);
- //Cinco primeiros numeros pares
- Conjunto n2 = new Conjunto(5);
- n2.inserirValor(2);
- n2.inserirValor(4);
- n2.inserirValor(6);
- n2.inserirValor(8);
- n2.inserirValor(10);
- //Cinco primeiros numeros impares
- Conjunto n3 = new Conjunto(5);
- n3.inserirValor(1);
- n3.inserirValor(3);
- n3.inserirValor(5);
- n3.inserirValor(7);
- n3.inserirValor(9);
- //Dez primeiros numeros primos
- Conjunto n4 = new Conjunto(10);
- n4.inserirValor(2);
- n4.inserirValor(3);
- n4.inserirValor(5);
- n4.inserirValor(7);
- n4.inserirValor(11);
- n4.inserirValor(13);
- n4.inserirValor(17);
- n4.inserirValor(19);
- n4.inserirValor(23);
- n4.inserirValor(29);
- System.out.println("--------------- ITEM A ---------------");
- //Se o conjunto 4 eh subconjunto de si mesmo
- n4.verificarSubconjunto(n4);
- System.out.println("--------------- ITEM B ---------------");
- //A pertinencia dos conjuntos 2, 3 e 4 no conjunto 1
- System.out.println("--------------- ITEM B.1 ---------------");
- n1.verificarSubconjunto(n2);
- System.out.println("--------------- ITEM B.2 ---------------");
- n1.verificarSubconjunto(n3);
- System.out.println("--------------- ITEM B.3 ---------------");
- n1.verificarSubconjunto(n4);
- System.out.println("--------------- ITEM C ---------------");
- //Se a uniao de 2 e 3 eh igual ao conjunto 1;
- Conjunto union = n2.uniaoConjunto(n3);
- System.out.println("--------------- ITEM D ---------------");
- //Se a interseccao dos conjuntos 1 e 2 eh vazia
- Conjunto inter = n1.interConjunto(n2);
- System.out.println("--------------- ITEM E ---------------");
- //Qual a diferenca entre os conjuntos 1 e 2
- Conjunto difer = n1.diferencaConjunto(n2);
- System.out.println("--------------- FIM ---------------");
- n1.imprimirVetor();
- n2.imprimirVetor();
- n3.imprimirVetor();
- n4.imprimirVetor();
- System.out.println("Uniao:"); union.imprimirVetor();
- System.out.println("Interseccao:"); inter.imprimirVetor();
- System.out.println("Diferenca:"); difer.imprimirVetor();
- //System.out.println(n.getLength());
- }
- }
- class Conjunto {
- int[] vetor;
- int ponteiro;
- int quantidade;
- Conjunto (int tamanho) {
- this.vetor = new int[tamanho];
- this.quantidade = 0;
- this.ponteiro = -1;
- }
- void inserirValor(int valor) {
- if (this.ponteiro < (this.vetor.length-1)) {
- this.quantidade++;
- this.ponteiro++;
- this.vetor[this.ponteiro] = valor;
- System.out.println ("Valor adicionado.");
- } else {
- System.out.println ("Conjunto cheio.");
- }
- }
- boolean verificarValor(int valor) {
- int verificador = 0;
- while (verificador <= this.ponteiro) {
- if (this.vetor[verificador] == valor) {
- verificador = -1;
- break;
- }
- verificador++;
- }
- if (verificador == -1) {
- System.out.println ("O valor pertence ao conjunto.");
- return true;
- } else {
- System.out.println ("O valor nao pertence ao conjunto.");
- return false;
- }
- }
- int verificarSubconjunto(Conjunto c) {
- if (c.quantidade > this.quantidade) {
- System.out.println ("O subconjunto e maior que o conjunto comparado.");
- return 0;
- } else {
- int check = c.ponteiro;
- int flag = c.quantidade;
- int contador = 0;
- while (flag > 0) {
- if (this.verificarValor(c.vetor[check])) {
- contador++;
- }
- flag--;
- check--;
- }
- if (contador == c.quantidade) {
- System.out.println ("E subconjunto.");
- return 1;
- } else {
- System.out.println ("Nao e subconjunto.");
- return 0;
- }
- }
- }
- Conjunto uniaoConjunto(Conjunto c) {
- if (this.verificarSubconjunto(c) == 0) {
- Conjunto uniao_teste = new Conjunto(this.quantidade + c.quantidade);
- int t1 = this.ponteiro;
- int t2 = c.ponteiro;
- for (int x = this.quantidade; x > 0; x--) {
- if (!uniao_teste.verificarValor(this.vetor[t1])) {
- uniao_teste.inserirValor(this.vetor[t1]);
- t1--;
- }
- }
- for (int x = c.quantidade; x > 0; x--) {
- if(!uniao_teste.verificarValor(c.vetor[t2])) {
- uniao_teste.inserirValor(c.vetor[t2]);
- }
- t2--;
- }
- int contador = 0;
- for (int t3 = uniao_teste.ponteiro; t3 >= 0; t3--) {
- if (uniao_teste.vetor[t3] == 0) {
- contador++;
- }
- }
- Conjunto uniao = new Conjunto(uniao_teste.quantidade - (contador-1));
- t1 = this.ponteiro;
- t2 = c.ponteiro;
- for (int x = this.quantidade; x > 0; x--) {
- if (!uniao.verificarValor(this.vetor[t1])) {
- uniao.inserirValor(this.vetor[t1]);
- t1--;
- }
- }
- for (int x = c.quantidade; x > 0; x--) {
- if(!uniao.verificarValor(c.vetor[t2])) {
- uniao.inserirValor(c.vetor[t2]);
- }
- t2--;
- }
- quickSort(uniao.vetor,0,uniao.vetor.length-1);
- return uniao;
- } else {
- return this;
- }
- }
- Conjunto interConjunto(Conjunto c) {
- int contador = 0;
- for (int i = 0; i < this.quantidade; i++) {
- for (int j = 0; j < c.quantidade; j++) {
- if (this.vetor[i] == c.vetor[j]) {
- contador++;
- }
- }
- }
- if (contador == 0) {
- contador++;
- }
- Conjunto intersec = new Conjunto(contador);
- for (int i = 0; i < this.quantidade; i++) {
- for (int j = 0; j < c.quantidade; j++) {
- if (this.vetor[i] == c.vetor[j]) {
- intersec.inserirValor(this.vetor[i]);
- }
- }
- }
- quickSort(intersec.vetor,0,intersec.vetor.length-1);
- return intersec;
- }
- Conjunto diferencaConjunto(Conjunto c) {
- int tamanho;
- int contador = 0;
- for (int i = 0; i < this.quantidade; i++) {
- for (int j = 0; j < c.quantidade; j++) {
- if (this.vetor[i] == c.vetor[j]) {
- contador++;
- break;
- }
- }
- }
- if (this.quantidade >= c.quantidade) {
- tamanho = this.quantidade;
- } else {
- tamanho = c.quantidade;
- }
- if (contador == tamanho) {
- Conjunto diferenca = new Conjunto(1);
- diferenca.inserirValor(0);
- quickSort(diferenca.vetor,0,diferenca.vetor.length-1);
- return diferenca;
- } else {
- tamanho -= contador;
- Conjunto diferenca = new Conjunto(tamanho);
- for (int i = 0; i < this.quantidade; i++) {
- for (int j = 0; j < c.quantidade; j++) {
- int flag = -1;
- if (this.vetor[i] == c.vetor[j]) {
- flag += 1;
- if (flag > 0 && j == c.quantidade) {
- for (int k = 0; k < flag; k++) {
- diferenca.inserirValor(this.vetor[i]);
- }
- }
- }
- else {
- if (j == c.quantidade) {
- diferenca.inserirValor(this.vetor[i]);
- }
- }
- }
- }
- quickSort(diferenca.vetor,0,diferenca.vetor.length-1);
- return diferenca;
- }
- }
- int getLength() {
- return this.vetor.length;
- }
- void imprimirVetor() {
- String msg = "[";
- for (int x = 0; x <= getLength()-1; x++) {
- if (x == getLength()-1)
- msg += this.vetor[x] + "]";
- else
- msg += this.vetor[x] + ",";
- }
- System.out.println(msg);
- }
- void quickSort(int[] vetor, int inicio, int fim) {
- if (inicio < fim) {
- int posicaoPivo = separar(vetor, inicio, fim);
- quickSort(vetor, inicio, posicaoPivo - 1);
- quickSort(vetor, posicaoPivo + 1, fim);
- }
- }
- int separar(int[] vetor, int inicio, int fim) {
- int pivo = vetor[inicio];
- int i = inicio + 1, f = fim;
- while (i <= f) {
- if (vetor[i] <= pivo)
- i++;
- else if (pivo < vetor[f])
- f--;
- else {
- int troca = vetor[i];
- vetor[i] = vetor[f];
- vetor[f] = troca;
- i++;
- f--;
- }
- }
- vetor[inicio] = vetor[f];
- vetor[f] = pivo;
- return f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement