Advertisement
GieeF

Untitled

May 4th, 2020
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. import java.rmi.AlreadyBoundException;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4.  
  5. import javax.naming.SizeLimitExceededException;
  6.  
  7. public class Zad2 {
  8.  
  9. }
  10.  
  11. class Kwadrat implements Comparable<Kwadrat> {
  12.     private int bok;
  13.    
  14.     Kwadrat(int bok) {
  15.         this.bok = bok;
  16.     }
  17.    
  18.     int getBok() {
  19.         return this.bok;
  20.     }
  21.    
  22.     @Override
  23.     public int compareTo(Kwadrat k) {
  24.         // TODO Auto-generated method stub
  25.         return Integer.compare(this.getBok(), k.getBok());
  26.     }
  27. }
  28.  
  29. class Biblioteczka implements Comparable<Biblioteczka> {
  30.     private int iloscKsiazek;
  31.    
  32.     Biblioteczka(int iloscKsiazek) {
  33.         this.iloscKsiazek = iloscKsiazek;
  34.     }
  35.    
  36.     int getIlosc() {
  37.         return this.iloscKsiazek;
  38.     }
  39.    
  40.     @Override
  41.     public int compareTo(Biblioteczka b) {
  42.         return Integer.compare(this.getIlosc(), b.getIlosc());
  43.     }
  44. }
  45.  
  46. class Set<T> implements Comparable<T>{
  47.     T [] set;
  48.     int pojemnosc;
  49.     int rozmiar;
  50.    
  51.     @SuppressWarnings("unchecked")
  52.     Set(int pojemnosc) {
  53.         this.pojemnosc = pojemnosc;
  54.         set = (T[]) new Set[pojemnosc];
  55.     }
  56.    
  57.     @SuppressWarnings("unchecked")
  58.     void dodajElement(T elem) throws AlreadyBoundException, SizeLimitExceededException{
  59.         for(int i = 0; i < rozmiar; i++) {
  60.             if(((Comparable<T>) set[i]).compareTo(elem) != -1) {
  61.                 throw new IllegalArgumentException("Wartosc znajduje sie w tablicy");
  62.             }
  63.         }
  64.         if(rozmiar == pojemnosc) {
  65.             throw new SizeLimitExceededException();
  66.         }
  67.        
  68.         set[rozmiar] = elem;
  69.         rozmiar++;
  70.        
  71.         Arrays.sort(set);
  72.     }
  73.    
  74.     int szukaj(T elem) {
  75.         for(int i = 0; i < rozmiar; i++) {
  76.             if(elem.equals(set[i])) {
  77.                 return i;
  78.             }
  79.         }
  80.         throw new IllegalArgumentException("Brak elementu");
  81.     }
  82.    
  83.     public String toString() {
  84.         String result = "";
  85.        
  86.         result += "Rozmiar: " + rozmiar;
  87.         result += "\nPojemnosc: " + pojemnosc;
  88.         result += "\nLista elementow:";
  89.        
  90.         for(int i = 0; i < rozmiar; i++) {
  91.             result += set[i] + " ";
  92.         }
  93.        
  94.         return result;
  95.     }
  96.    
  97.     void usunElement(T elem) {
  98.         int pos = -1;
  99.         try {
  100.         pos = this.szukaj(elem);
  101.         }
  102.         catch(Exception e) {
  103.             System.out.println(e.getMessage());
  104.         }
  105.        
  106.         if(pos != -1) {
  107.             set[pos] = null;
  108.             Arrays.sort(set);
  109.             rozmiar--;
  110.         }
  111.  
  112.     }
  113.    
  114.     Set<T> dodajElementy(Set<T> set2) {
  115.         Set<T> newSet = new Set<T>(this.pojemnosc + set2.pojemnosc);
  116.        
  117.         for(int i = 0; i < this.rozmiar; i++) {
  118.             try {
  119.                 newSet.dodajElement(this.set[i]);
  120.                 newSet.rozmiar++;
  121.             }
  122.             catch(Exception e) {
  123.                 System.out.println(e.getMessage());
  124.             }
  125.         }
  126.            
  127.         for(int i = 0; i < set2.rozmiar; i++) {
  128.             try {
  129.                 newSet.dodajElement(set2.set[i]);
  130.                 newSet.rozmiar++;
  131.             }
  132.             catch(Exception e) {
  133.                 System.out.println(e.getMessage());
  134.             }
  135.         }
  136.        
  137.         return newSet;
  138.     }
  139.    
  140.     void odejmijElementy(Set <T> set2) {
  141.         for(int i = 0; i < set2.rozmiar; i++) {
  142.             this.usunElement(set2.set[i]);
  143.         }
  144.     }
  145.    
  146.     Set <T> przeciecie(Set <T> set2) {
  147.         Set<T> newSet = new Set<T>(this.pojemnosc);
  148.         for(int i = 0; i < this.rozmiar; i++) {
  149.             for(int j = 0; j < set2.rozmiar; j++) {
  150.                 if(this.set[i].equals(set2.set[j])) {
  151.                     try {
  152.                     newSet.dodajElement(this.set[i]);
  153.                     }
  154.                     catch(Exception e) {
  155.                         System.out.println(e.getMessage());
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.        
  161.         return newSet;
  162.     }
  163.  
  164.     @Override
  165.     public int compareTo(T o) {
  166.         // TODO Auto-generated method stub
  167.         return 0;
  168.     }  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement