Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package zbior;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.HashSet;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Hubert
- */
- public class Zbior implements InterfejsZbior{
- int rozmiar;
- int [] zbior=new int[100];
- public static void main(String[] args){
- Zbior z=new Zbior();
- Zbior z2=new Zbior();
- InterfejsZbior zbior_przeciecie = new Zbior();
- InterfejsZbior zbior_roznicaSymetryczna = new Zbior();
- InterfejsZbior zbior_roznica = new Zbior();
- InterfejsZbior zbior_suma = new Zbior();
- //z.wczytaj("plik.txt");
- z.dodaj(1);z.dodaj(2);z.dodaj(3);z.dodaj(4);z.dodaj(5);
- z2.dodaj(5);z2.dodaj(6);z2.dodaj(7);z2.dodaj(8);
- //z2.dodaj(5);
- z.wypisz();
- //z.usun(5);
- z.wypisz();
- zbior_suma=z.suma(z2);
- zbior_roznica=z.roznica(z2);
- zbior_przeciecie=z.przeciecie(z2);
- zbior_roznicaSymetryczna=z.roznicaSymetryczna(z2);
- System.out.println("Czy zawiera 3?"+" "+z.czyZawiera(3));
- System.out.println("Czy zbior1 i 2 sa rowne?"+" "+z.czyRowne(z2));
- System.out.println("Suma");
- for(int i=0;i<z.GetRozmiar()+z2.GetRozmiar();i++)
- if(zbior_suma.GetTab()[i]!=0)
- System.out.print(zbior_suma.GetTab()[i]+" ");
- System.out.println();
- System.out.println("Roznica");
- for(int i=0;i<z.GetRozmiar()+z2.GetRozmiar();i++)
- if(zbior_roznica.GetTab()[i]!=0)
- {System.out.print(zbior_roznica.GetTab()[i]+" ");}
- System.out.println();
- System.out.println("Pzeciecie");
- for(int i=0;i<z.GetRozmiar()+z2.GetRozmiar();i++)
- if(zbior_przeciecie.GetTab()[i]!=0)
- System.out.print(zbior_przeciecie.GetTab()[i]+" ");
- System.out.println();
- System.out.println("Roznica Symetryczna");
- for(int i=0;i<z.GetRozmiar()+z2.GetRozmiar();i++)
- if(zbior_roznicaSymetryczna.GetTab()[i]!=0)
- System.out.print(zbior_roznicaSymetryczna.GetTab()[i]+" ");
- }
- @Override
- public int[] GetTab(){
- return zbior;
- }
- @Override
- public int GetRozmiar(){
- return rozmiar;}
- @Override
- public void wczytaj(String filename) {
- int wartosc;
- try {
- Scanner sc=new Scanner(new File(filename));
- while(sc.hasNext()){
- wartosc=sc.nextInt();
- if(czyZawiera(wartosc)==false)
- {
- zbior[rozmiar]=wartosc;
- rozmiar++;
- }
- }
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Zbior.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @Override
- public void wypisz() {
- for(int i=0;i<rozmiar;i++)
- System.out.println("Zbior :"+zbior[i]);
- }
- @Override
- public void dodaj(int nowy) {
- if(czyZawiera(nowy)==false){
- zbior[rozmiar]=nowy;
- rozmiar++;
- System.out.println("Dodaje element "+nowy);
- }
- }
- @Override
- public void usun(int element) {
- System.out.println("Usuwam element "+element);
- for(int i=0;i<rozmiar;i++)
- if(zbior[i]==element)
- {
- zbior[rozmiar]=zbior[rozmiar-1];
- rozmiar--;
- }
- }
- @Override
- public boolean czyZawiera(int element) {
- for(int i=0;i<rozmiar;i++)
- if(zbior[i]==element)
- return true;
- return false;
- }
- @Override
- public boolean czyRowne(InterfejsZbior drugi) {
- if(rozmiar==drugi.GetRozmiar())
- {
- for(int i=0;i<rozmiar;i++)
- if(drugi.czyZawiera(zbior[i])==false)
- return false;
- return true;
- }
- else
- return false;
- }
- @Override
- public InterfejsZbior suma(InterfejsZbior drugi) {
- InterfejsZbior New =new Zbior();
- for(int i=0;i<rozmiar;i++)
- { New.dodaj(zbior[i]);
- }
- for(int i=0;i<rozmiar;i++)
- New.dodaj(drugi.GetTab()[i]);
- return New;
- }
- @Override
- public InterfejsZbior roznica(InterfejsZbior drugi) {
- InterfejsZbior New =new Zbior();
- for(int i=0;i<rozmiar;i++)
- if(!drugi.czyZawiera(zbior[i]))
- New.dodaj(zbior[i]);
- return New;
- }
- @Override
- public InterfejsZbior przeciecie(InterfejsZbior drugi) {
- InterfejsZbior New =new Zbior();
- for(int i=0;i<rozmiar;i++)
- if(drugi.czyZawiera(zbior[i]))
- New.dodaj(zbior[i]);
- return New;
- }
- @Override
- public InterfejsZbior roznicaSymetryczna(InterfejsZbior drugi) {
- InterfejsZbior New =new Zbior();
- for(int i=0;i<rozmiar;i++)
- New.dodaj(zbior[i]);
- for(int i=0;i<rozmiar;i++)
- if(New.czyZawiera(drugi.GetTab()[i]))
- New.usun(i+1);
- else
- New.dodaj(drugi.GetTab()[i]);
- return New;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement