Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ajudatp2;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Conjunto {
- private ArrayList<Integer> conjunto;
- private int tamanho;
- public Conjunto() {
- this.conjunto = new ArrayList<Integer>();
- tamanho = 0;
- }
- public void adicionarAoConjunto(int n) {
- if(this.tamanho == 0) {
- if(conjunto.contains(n)) {
- System.out.println("Numero Já existe");
- return;
- }
- conjunto.add(n);
- tamanho++;
- }else {
- if(conjunto.contains(n)) {
- System.out.println("Numero Já existe");
- return;
- }
- this.ordenar(conjunto.size()-1,n);
- tamanho++;
- }
- }
- private void ordenar(int index,int n){
- int valor = conjunto.get(index);
- if(index == 0) {
- if(n>valor) {
- conjunto.add(n);
- }else {
- int numremovido = conjunto.remove(0);
- conjunto.add(n);
- conjunto.add(numremovido);
- }
- }else {
- if(n>valor) {
- conjunto.add(n);
- }else {
- int removido = conjunto.remove(index);
- ordenar(index-1,n);
- conjunto.add(removido);
- }
- }}
- public int removerDoConjunto(int n) {
- int valor = Integer.MIN_VALUE;
- for (int i = 0; i < conjunto.size(); i++) {
- int atual = conjunto.get(i);
- if (n == atual ) {
- valor = atual;
- tamanho--;
- return valor;
- }else if(valor > n) {
- System.out.println("O numero não existe");
- break;
- }
- }
- return valor;
- }
- public void imprimeConjunto() {
- System.out.println("O conjunto é:");
- for (int i = 0; i < conjunto.size(); i++) {
- System.out.print(conjunto.get(i)+",");
- }
- }
- public int retornaTamanho() {
- return this.tamanho;
- }
- public void Unir(Conjunto uniao,Conjunto conj) {
- int valor1 = Integer.MIN_VALUE;
- int valor2 = Integer.MIN_VALUE;
- if(conj.retornaTamanho()>0) {
- valor1 = conj.removerDoConjunto(conj.conjunto.get((conj.retornaTamanho()-1)));
- }
- if(this.retornaTamanho()>0) {
- valor2 = this.removerDoConjunto(this.conjunto.get(this.retornaTamanho()-1));
- }
- if(valor1 != Integer.MIN_VALUE) {
- uniao.adicionarAoConjunto(valor1);
- }
- if(valor2 != Integer.MIN_VALUE) {
- uniao.adicionarAoConjunto(valor2);
- }
- if(valor1 == Integer.MIN_VALUE && valor2 == Integer.MIN_VALUE) {
- return;
- }
- this.Unir(uniao, conj);
- }
- public void interseccao(Conjunto intersec,Conjunto conj) {
- if(this.retornaTamanho() == 0) {
- int valor = this.removerDoConjunto(this.conjunto.get(0));
- if(conj.conjunto.contains(valor)) {
- intersec.adicionarAoConjunto(valor);
- }
- return;
- }
- int valor = this.removerDoConjunto(this.conjunto.get(this.retornaTamanho()-1));
- if(conj.conjunto.contains(valor)) {
- intersec.adicionarAoConjunto(valor);
- }
- this.interseccao(intersec,conj);
- }
- public void dif(Conjunto dif,Conjunto conj) {
- if(this.retornaTamanho() == 0) {
- int valor = this.removerDoConjunto(this.conjunto.get(0));
- if(!conj.conjunto.contains(valor)) {
- dif.adicionarAoConjunto(valor);
- }
- return;
- }
- int valor = this.removerDoConjunto(this.conjunto.get(this.retornaTamanho()-1));
- if(!conj.conjunto.contains(valor)) {
- dif.adicionarAoConjunto(valor);
- }
- this.dif(dif,conj);
- }
- public static void main(String[] args) {
- Conjunto conj = new Conjunto();
- Conjunto conj2 = new Conjunto();
- Conjunto Uniao = new Conjunto();
- Conjunto Intersec = new Conjunto();
- conj2.adicionarAoConjunto(0);
- conj2.adicionarAoConjunto(2);
- conj2.adicionarAoConjunto(9);
- conj2.adicionarAoConjunto(13);
- conj2.adicionarAoConjunto(15);
- conj.adicionarAoConjunto(0);
- conj.adicionarAoConjunto(2);
- conj.adicionarAoConjunto(5);
- conj.adicionarAoConjunto(7);
- conj.imprimeConjunto();
- System.out.println("====================");
- conj.adicionarAoConjunto(3);
- conj.imprimeConjunto();
- System.out.println("");
- conj.retornaTamanho();
- conj.dif(Intersec, conj2);
- Intersec.imprimeConjunto();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement