Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.rmi.AlreadyBoundException;
- import java.util.Arrays;
- import java.util.Collections;
- import javax.naming.SizeLimitExceededException;
- public class Zad2 {
- }
- class Kwadrat implements Comparable<Kwadrat> {
- private int bok;
- Kwadrat(int bok) {
- this.bok = bok;
- }
- int getBok() {
- return this.bok;
- }
- @Override
- public int compareTo(Kwadrat k) {
- // TODO Auto-generated method stub
- return Integer.compare(this.getBok(), k.getBok());
- }
- }
- class Biblioteczka implements Comparable<Biblioteczka> {
- private int iloscKsiazek;
- Biblioteczka(int iloscKsiazek) {
- this.iloscKsiazek = iloscKsiazek;
- }
- int getIlosc() {
- return this.iloscKsiazek;
- }
- @Override
- public int compareTo(Biblioteczka b) {
- return Integer.compare(this.getIlosc(), b.getIlosc());
- }
- }
- class Set<T> implements Comparable<T>{
- T [] set;
- int pojemnosc;
- int rozmiar;
- @SuppressWarnings("unchecked")
- Set(int pojemnosc) {
- this.pojemnosc = pojemnosc;
- set = (T[]) new Set[pojemnosc];
- }
- @SuppressWarnings("unchecked")
- void dodajElement(T elem) throws AlreadyBoundException, SizeLimitExceededException{
- for(int i = 0; i < rozmiar; i++) {
- if(((Comparable<T>) set[i]).compareTo(elem) != -1) {
- throw new IllegalArgumentException("Wartosc znajduje sie w tablicy");
- }
- }
- if(rozmiar == pojemnosc) {
- throw new SizeLimitExceededException();
- }
- set[rozmiar] = elem;
- rozmiar++;
- Arrays.sort(set);
- }
- int szukaj(T elem) {
- for(int i = 0; i < rozmiar; i++) {
- if(elem.equals(set[i])) {
- return i;
- }
- }
- throw new IllegalArgumentException("Brak elementu");
- }
- public String toString() {
- String result = "";
- result += "Rozmiar: " + rozmiar;
- result += "\nPojemnosc: " + pojemnosc;
- result += "\nLista elementow:";
- for(int i = 0; i < rozmiar; i++) {
- result += set[i] + " ";
- }
- return result;
- }
- void usunElement(T elem) {
- int pos = -1;
- try {
- pos = this.szukaj(elem);
- }
- catch(Exception e) {
- System.out.println(e.getMessage());
- }
- if(pos != -1) {
- set[pos] = null;
- Arrays.sort(set);
- rozmiar--;
- }
- }
- Set<T> dodajElementy(Set<T> set2) {
- Set<T> newSet = new Set<T>(this.pojemnosc + set2.pojemnosc);
- for(int i = 0; i < this.rozmiar; i++) {
- try {
- newSet.dodajElement(this.set[i]);
- newSet.rozmiar++;
- }
- catch(Exception e) {
- System.out.println(e.getMessage());
- }
- }
- for(int i = 0; i < set2.rozmiar; i++) {
- try {
- newSet.dodajElement(set2.set[i]);
- newSet.rozmiar++;
- }
- catch(Exception e) {
- System.out.println(e.getMessage());
- }
- }
- return newSet;
- }
- void odejmijElementy(Set <T> set2) {
- for(int i = 0; i < set2.rozmiar; i++) {
- this.usunElement(set2.set[i]);
- }
- }
- Set <T> przeciecie(Set <T> set2) {
- Set<T> newSet = new Set<T>(this.pojemnosc);
- for(int i = 0; i < this.rozmiar; i++) {
- for(int j = 0; j < set2.rozmiar; j++) {
- if(this.set[i].equals(set2.set[j])) {
- try {
- newSet.dodajElement(this.set[i]);
- }
- catch(Exception e) {
- System.out.println(e.getMessage());
- }
- }
- }
- }
- return newSet;
- }
- @Override
- public int compareTo(T o) {
- // TODO Auto-generated method stub
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement