Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 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 zbior;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.util.HashSet;
  11. import java.util.Scanner;
  12. import java.util.Set;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17. *
  18. * @author Hubert
  19. */
  20. public class Zbior implements InterfejsZbior{
  21. int rozmiar;
  22. int [] zbior=new int[100];
  23.  
  24.  
  25. public static void main(String[] args){
  26.  
  27. Zbior z=new Zbior();
  28. Zbior z2=new Zbior();
  29. InterfejsZbior zbior_przeciecie = new Zbior();
  30. InterfejsZbior zbior_roznicaSymetryczna = new Zbior();
  31. InterfejsZbior zbior_roznica = new Zbior();
  32. InterfejsZbior zbior_suma = new Zbior();
  33. //z.wczytaj("plik.txt");
  34. z.dodaj(1);z.dodaj(2);z.dodaj(3);z.dodaj(4);z.dodaj(5);
  35. z2.dodaj(5);z2.dodaj(6);z2.dodaj(7);z2.dodaj(8);
  36. //z2.dodaj(5);
  37. z.wypisz();
  38. //z.usun(5);
  39. z.wypisz();
  40. zbior_suma=z.suma(z2);
  41. zbior_roznica=z.roznica(z2);
  42. zbior_przeciecie=z.przeciecie(z2);
  43. zbior_roznicaSymetryczna=z.roznicaSymetryczna(z2);
  44. System.out.println("Czy zawiera 3?"+" "+z.czyZawiera(3));
  45. System.out.println("Czy zbior1 i 2 sa rowne?"+" "+z.czyRowne(z2));
  46. System.out.println("Suma");
  47. for(int i=0;i<z.GetRozmiar()+z2.GetRozmiar();i++)
  48. if(zbior_suma.GetTab()[i]!=0)
  49. System.out.print(zbior_suma.GetTab()[i]+" ");
  50. System.out.println();
  51. System.out.println("Roznica");
  52. for(int i=0;i<z.GetRozmiar()+z2.GetRozmiar();i++)
  53. if(zbior_roznica.GetTab()[i]!=0)
  54. {System.out.print(zbior_roznica.GetTab()[i]+" ");}
  55. System.out.println();
  56. System.out.println("Pzeciecie");
  57.  
  58. for(int i=0;i<z.GetRozmiar()+z2.GetRozmiar();i++)
  59. if(zbior_przeciecie.GetTab()[i]!=0)
  60. System.out.print(zbior_przeciecie.GetTab()[i]+" ");
  61. System.out.println();
  62. System.out.println("Roznica Symetryczna");
  63. for(int i=0;i<z.GetRozmiar()+z2.GetRozmiar();i++)
  64. if(zbior_roznicaSymetryczna.GetTab()[i]!=0)
  65. System.out.print(zbior_roznicaSymetryczna.GetTab()[i]+" ");
  66. }
  67. @Override
  68. public int[] GetTab(){
  69. return zbior;
  70. }
  71. @Override
  72. public int GetRozmiar(){
  73. return rozmiar;}
  74. @Override
  75. public void wczytaj(String filename) {
  76. int wartosc;
  77. try {
  78. Scanner sc=new Scanner(new File(filename));
  79. while(sc.hasNext()){
  80. wartosc=sc.nextInt();
  81. if(czyZawiera(wartosc)==false)
  82. {
  83. zbior[rozmiar]=wartosc;
  84. rozmiar++;
  85. }
  86. }
  87.  
  88. } catch (FileNotFoundException ex) {
  89. Logger.getLogger(Zbior.class.getName()).log(Level.SEVERE, null, ex);
  90. }
  91.  
  92.  
  93. }
  94.  
  95. @Override
  96. public void wypisz() {
  97. for(int i=0;i<rozmiar;i++)
  98. System.out.println("Zbior :"+zbior[i]);
  99. }
  100.  
  101. @Override
  102. public void dodaj(int nowy) {
  103.  
  104. if(czyZawiera(nowy)==false){
  105. zbior[rozmiar]=nowy;
  106. rozmiar++;
  107. System.out.println("Dodaje element "+nowy);
  108. }
  109.  
  110.  
  111. }
  112.  
  113. @Override
  114. public void usun(int element) {
  115. System.out.println("Usuwam element "+element);
  116. for(int i=0;i<rozmiar;i++)
  117. if(zbior[i]==element)
  118. {
  119. zbior[rozmiar]=zbior[rozmiar-1];
  120. rozmiar--;
  121. }
  122. }
  123.  
  124. @Override
  125. public boolean czyZawiera(int element) {
  126. for(int i=0;i<rozmiar;i++)
  127. if(zbior[i]==element)
  128. return true;
  129. return false;
  130.  
  131. }
  132.  
  133. @Override
  134. public boolean czyRowne(InterfejsZbior drugi) {
  135. if(rozmiar==drugi.GetRozmiar())
  136. {
  137. for(int i=0;i<rozmiar;i++)
  138. if(drugi.czyZawiera(zbior[i])==false)
  139. return false;
  140. return true;
  141.  
  142. }
  143. else
  144. return false;
  145. }
  146.  
  147. @Override
  148. public InterfejsZbior suma(InterfejsZbior drugi) {
  149. InterfejsZbior New =new Zbior();
  150. for(int i=0;i<rozmiar;i++)
  151. { New.dodaj(zbior[i]);
  152.  
  153. }
  154. for(int i=0;i<rozmiar;i++)
  155. New.dodaj(drugi.GetTab()[i]);
  156. return New;
  157. }
  158.  
  159. @Override
  160. public InterfejsZbior roznica(InterfejsZbior drugi) {
  161. InterfejsZbior New =new Zbior();
  162. for(int i=0;i<rozmiar;i++)
  163. if(!drugi.czyZawiera(zbior[i]))
  164. New.dodaj(zbior[i]);
  165.  
  166. return New;
  167.  
  168. }
  169.  
  170. @Override
  171. public InterfejsZbior przeciecie(InterfejsZbior drugi) {
  172. InterfejsZbior New =new Zbior();
  173. for(int i=0;i<rozmiar;i++)
  174. if(drugi.czyZawiera(zbior[i]))
  175. New.dodaj(zbior[i]);
  176.  
  177.  
  178. return New;
  179.  
  180. }
  181.  
  182. @Override
  183. public InterfejsZbior roznicaSymetryczna(InterfejsZbior drugi) {
  184. InterfejsZbior New =new Zbior();
  185. for(int i=0;i<rozmiar;i++)
  186. New.dodaj(zbior[i]);
  187. for(int i=0;i<rozmiar;i++)
  188. if(New.czyZawiera(drugi.GetTab()[i]))
  189. New.usun(i+1);
  190. else
  191. New.dodaj(drugi.GetTab()[i]);
  192. return New;
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement