Advertisement
Guest User

Untitled

a guest
May 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. package sob;
  2. import java.util.Random;
  3.  
  4.  
  5. public class NumberSet {
  6.     private static int MAX_SIZE = 100;
  7.     private int[] nSet = new int[MAX_SIZE];
  8.     private int size;
  9.  
  10.     /**
  11.      * Dodaje liczbę k do zbioru liczb.
  12.      * Jeżeli podana liczba już istnieje dodawana jest po raz drugi
  13.      * @param i liczba, którą należy dodać od zbioru
  14.      * @throws Exception występuje w przypadku przepełnienia tablicy
  15.      */
  16.     public void add(int i) throws Exception {
  17.         assert (size < MAX_SIZE) : "Array is full";
  18.         nSet[size] = i;
  19.         size++;
  20.     }
  21.     /**
  22.      * Usuwa liczbę k ze zbioru liczb.
  23.      * W przypadku gdy zbiór nie posiada liczby podanej jako parametr rzucany jest wyjątek.
  24.      * @param i liczba do usunięcia
  25.      * @throws Exception w przypadku gdy zbiór nie posiada danej liczby
  26.      */
  27.     public void remove(int i) throws Exception {
  28.         assert (contains(i)) : "Item not found!";
  29.  
  30.  
  31.         for (int k = 0; k < size; k++) {
  32.             if (i == nSet[k]) {
  33.                 for (int j = k; j < size; j++) {
  34.                     nSet[j] = nSet[j + 1];
  35.  
  36.                 }
  37.                 size--;
  38.             }
  39.  
  40.  
  41.         }
  42.     }
  43.  
  44.     /**
  45.      * Losuje jedną liczbę ze zbioru liczb oraz usuwa ją ze zbioru.
  46.      * @return wylosowana liczba
  47.      * @throws Exception występuje w przypadku pustego zbioru
  48.      */
  49.     public int getRandomValue() throws Exception {
  50.         assert (size > 0) : "Array is empty!";
  51.  
  52.         Random rand = new Random();
  53.         int index = rand.nextInt(size);
  54.         int toDeleteVal = nSet[index];
  55.         remove(index);
  56.         return toDeleteVal;
  57.     }
  58.  
  59.     /**
  60.      * Zwraca sumę wszystkich liczb ze zbioru.
  61.      * @return Suma liczb. W przypadku pustego zbioru wynosi 0.
  62.      */
  63.     public int getSumOfElements() throws Exception {
  64.         assert (size > 0) : "Array is empty!";
  65.  
  66.         int sum = 0;
  67.         for (int i = 0; i < size; i++) {
  68.             sum += nSet[i];
  69.         }
  70.         return sum;
  71.     }
  72.  
  73.     /**
  74.      * Dzieli każdy element ze zbioru przez n bez reszty.
  75.      * @param d liczba przez którą będzie wykonane dzielenie.
  76.      */
  77.     public void divideAllElementsBy(int d) throws Exception {
  78.         assert (size > 0) : "Array is empty!";
  79.         assert (d != 0) : "Dividing by zero!";
  80.  
  81.         for (int i = 0; i < size; i++) {
  82.             nSet[i] /= d;
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * Metoda sprawdza czy w zbiorze istnieje podany element
  88.      * @param j element do sprawdzenia
  89.      * @return true w przypadku odnalezienia wartości,
  90.      * false w przeciwnym razie
  91.      */
  92.     public boolean contains(int j) {
  93.         assert (size > 0) : "Array is empty!";
  94.  
  95.         boolean isInArray = false;
  96.         for (int i = 0; i < size; i++) {
  97.             if (nSet[i] == j) isInArray = true;
  98.         }
  99.         return isInArray;
  100.     }
  101.  
  102.     /**
  103.      * Zwraca rozmiar zbioru, czyli faktycznie dodanych liczbę elementów
  104.      * @return rozmiar zbioru
  105.      */
  106.     public int getSize() {
  107.         assert (size > 0) : "Array is empty!";
  108.         return size;
  109.     }
  110.  
  111.     /**
  112.      * Zwraca element zbioru
  113.      * @return element zbioru
  114.      */
  115.     public int getElement(int i) {
  116.         // assert (size > 0) : "Array is empty!";
  117.         return nSet[i];
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement