Advertisement
EmhyrVanEmrias

java1lab1

Oct 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javaapplication1;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileReader;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. /**
  14.  *
  15.  * @author Powerzak
  16.  */
  17. public class Zbior implements IZbior {
  18.     private int tab[];
  19.     public void wczytaj(String filename)
  20.     {
  21.         List<Integer> liczby = new ArrayList<Integer>();
  22.    
  23.         try{
  24.              BufferedReader br = new BufferedReader(new FileReader(filename));
  25.             String linia = null;
  26.             while ((linia = br.readLine()) != null) {
  27.                 int liczba = Integer.parseInt(linia);
  28.                 if(cjtl(liczby,liczba))
  29.                 {
  30.                 liczby.add(liczba);
  31.                 }
  32.         }
  33.         zapiszdotablicy(liczby);
  34.     }
  35.         catch(Exception e)
  36.         {
  37.             System.out.println("blad odczytu z pliku");
  38.         }
  39.    
  40. }
  41. public boolean cjtl(List<Integer> liczby,int liczba )
  42. { boolean a=true;
  43.     for(int i=0;i<liczby.size();i++)
  44.     {
  45.         if(liczby.get(i)==liczba)
  46.         {
  47.             a=false;
  48.         }
  49.     }
  50.     return a;
  51. }
  52.         public void zapiszdotablicy(List<Integer> liczby)
  53.         {
  54. tab=new int[liczby.size()];
  55. for(int i=0;i<tab.length;i++)
  56. {
  57.     tab[i]=liczby.get(i);
  58. }
  59.  
  60. }
  61.         public void wypisz()
  62.         {
  63.             for(int i=0;i<tab.length;i++)
  64.             {
  65.                 System.out.println(tab[i]);
  66.             }
  67.         }
  68.         public void dodaj(int nowy)
  69.         {
  70.             List<Integer> liczby = new ArrayList<Integer>();
  71.             for(int i=0;i<tab.length;i++)
  72.             {
  73.             liczby.add(tab[i]);
  74.             }
  75.           if(cjtl(liczby,nowy))
  76.           {
  77.               liczby.add(nowy);
  78.            zapiszdotablicy(liczby);
  79.            
  80.           }
  81.        
  82.         }
  83.  
  84.     @Override
  85.     public void usun(int element) {
  86.        List<Integer> liczby = new ArrayList<Integer>();
  87.             for(int i=0;i<tab.length;i++)
  88.             {
  89.             liczby.add(tab[i]);
  90.             }
  91.              for(int i=0;i<liczby.size();i++)
  92.              {
  93.           if(liczby.get(i)==element)
  94.           {
  95.               liczby.remove(i);
  96.            zapiszdotablicy(liczby);
  97.            
  98.           }
  99.              }
  100.     }
  101.  
  102.     @Override
  103.     public boolean czyZawiera(int element) {
  104.      boolean a=false;
  105.     for(int i=0;i<tab.length;i++)
  106.     {
  107.         if(tab[i]==element)
  108.         {
  109.             a=true;
  110.         }
  111.     }
  112.     return a;
  113.     }
  114.  
  115.     @Override
  116.     public boolean czyRowne(IZbior drugi) {
  117.        boolean a =true;
  118.          List<Integer> liczby = new ArrayList<Integer>();
  119.             for(int i=0;i<tab.length;i++)
  120.             {
  121.             liczby.add(tab[i]);
  122.             }
  123.             for(int i=0;i<drugi.getTab().length;i++)
  124.             {
  125.                 if(cjtl(liczby,drugi.getTab()[i]))
  126.                 {
  127.                     a=false;
  128.                    
  129.                 }
  130.             }
  131.        return a;
  132.     }
  133.  
  134.     @Override
  135.     public IZbior suma(IZbior drugi) {
  136.         IZbior a=new Zbior();
  137.         List<Integer> liczby = new ArrayList<Integer>();
  138.         {
  139.             if(tab.length>drugi.getTab().length)
  140.             {
  141.                 try{
  142.             for(int i=0;i<tab.length;i++)
  143.             {
  144.                 if((cjtl(liczby,tab[i])) && cjtl(liczby,drugi.getTab()[i]) &&  tab[i]!=drugi.getTab()[i] )
  145.                 {
  146.             liczby.add(tab[i]);
  147.             liczby.add(drugi.getTab()[i]);
  148.             }
  149.                 else if(!cjtl(liczby,tab[i]) && cjtl(liczby,drugi.getTab()[i]))
  150.                 {
  151.                            liczby.add(drugi.getTab()[i]);
  152.                         }
  153.                   else if(cjtl(liczby,tab[i]) && !cjtl(liczby,drugi.getTab()[i]))
  154.                 {
  155.                            liczby.add(tab[i]);
  156.                         }
  157.                 }
  158.                 }
  159.                 catch(Exception e)
  160.                 {
  161.                     for(int i=drugi.getTab().length;i<tab.length;i++)
  162.                     {
  163.                         if((cjtl(liczby,tab[i]))  )
  164.                 {
  165.             liczby.add(tab[i]);
  166.            
  167.             }
  168.                     }
  169.                 }
  170.                IZbior d=new Zbior(liczby.size());
  171. for(int i=0;i<tab.length;i++)
  172. {
  173.     d.getTab()[i]=liczby.get(i);
  174. }
  175.  
  176.             }
  177.         }
  178.         return d;
  179.     }
  180.  
  181.     public Zbior() {
  182.     }
  183.  
  184.     @Override
  185.     public IZbior roznica(IZbior drugi) {
  186.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  187.     }
  188.  
  189.     @Override
  190.     public IZbior przeciecie(IZbior drugi) {
  191.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  192.     }
  193.  
  194.     @Override
  195.     public IZbior roznicaSymetryczna(IZbior drugi) {
  196.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  197.     }
  198.  
  199.     public int[] getTab() {
  200.         return tab;
  201.     }
  202.  
  203.     public Zbior(int n)
  204.     {
  205.         tab=new int[n];
  206.     }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement