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 javaapplication1;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.util.ArrayList;
- import java.util.List;
- /**
- *
- * @author Powerzak
- */
- public class Zbior implements IZbior {
- private int tab[];
- public void wczytaj(String filename)
- {
- List<Integer> liczby = new ArrayList<Integer>();
- try{
- BufferedReader br = new BufferedReader(new FileReader(filename));
- String linia = null;
- while ((linia = br.readLine()) != null) {
- int liczba = Integer.parseInt(linia);
- if(cjtl(liczby,liczba))
- {
- liczby.add(liczba);
- }
- }
- zapiszdotablicy(liczby);
- }
- catch(Exception e)
- {
- System.out.println("blad odczytu z pliku");
- }
- }
- public boolean cjtl(List<Integer> liczby,int liczba )
- { boolean a=true;
- for(int i=0;i<liczby.size();i++)
- {
- if(liczby.get(i)==liczba)
- {
- a=false;
- }
- }
- return a;
- }
- public void zapiszdotablicy(List<Integer> liczby)
- {
- tab=new int[liczby.size()];
- for(int i=0;i<tab.length;i++)
- {
- tab[i]=liczby.get(i);
- }
- }
- public void wypisz()
- {
- for(int i=0;i<tab.length;i++)
- {
- System.out.println(tab[i]);
- }
- }
- public void dodaj(int nowy)
- {
- List<Integer> liczby = new ArrayList<Integer>();
- for(int i=0;i<tab.length;i++)
- {
- liczby.add(tab[i]);
- }
- if(cjtl(liczby,nowy))
- {
- liczby.add(nowy);
- zapiszdotablicy(liczby);
- }
- }
- @Override
- public void usun(int element) {
- List<Integer> liczby = new ArrayList<Integer>();
- for(int i=0;i<tab.length;i++)
- {
- liczby.add(tab[i]);
- }
- for(int i=0;i<liczby.size();i++)
- {
- if(liczby.get(i)==element)
- {
- liczby.remove(i);
- zapiszdotablicy(liczby);
- }
- }
- }
- @Override
- public boolean czyZawiera(int element) {
- boolean a=false;
- for(int i=0;i<tab.length;i++)
- {
- if(tab[i]==element)
- {
- a=true;
- }
- }
- return a;
- }
- @Override
- public boolean czyRowne(IZbior drugi) {
- boolean a =true;
- List<Integer> liczby = new ArrayList<Integer>();
- for(int i=0;i<tab.length;i++)
- {
- liczby.add(tab[i]);
- }
- for(int i=0;i<drugi.getTab().length;i++)
- {
- if(cjtl(liczby,drugi.getTab()[i]))
- {
- a=false;
- }
- }
- return a;
- }
- @Override
- public IZbior suma(IZbior drugi) {
- IZbior a=new Zbior();
- List<Integer> liczby = new ArrayList<Integer>();
- {
- if(tab.length>drugi.getTab().length)
- {
- try{
- for(int i=0;i<tab.length;i++)
- {
- if((cjtl(liczby,tab[i])) && cjtl(liczby,drugi.getTab()[i]) && tab[i]!=drugi.getTab()[i] )
- {
- liczby.add(tab[i]);
- liczby.add(drugi.getTab()[i]);
- }
- else if(!cjtl(liczby,tab[i]) && cjtl(liczby,drugi.getTab()[i]))
- {
- liczby.add(drugi.getTab()[i]);
- }
- else if(cjtl(liczby,tab[i]) && !cjtl(liczby,drugi.getTab()[i]))
- {
- liczby.add(tab[i]);
- }
- }
- }
- catch(Exception e)
- {
- for(int i=drugi.getTab().length;i<tab.length;i++)
- {
- if((cjtl(liczby,tab[i])) )
- {
- liczby.add(tab[i]);
- }
- }
- }
- IZbior d=new Zbior(liczby.size());
- for(int i=0;i<tab.length;i++)
- {
- d.getTab()[i]=liczby.get(i);
- }
- }
- }
- return d;
- }
- public Zbior() {
- }
- @Override
- public IZbior roznica(IZbior drugi) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public IZbior przeciecie(IZbior drugi) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public IZbior roznicaSymetryczna(IZbior drugi) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- public int[] getTab() {
- return tab;
- }
- public Zbior(int n)
- {
- tab=new int[n];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement