Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. package number.set;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Random;
  5.  
  6. public class NumberSet {
  7.    
  8.     private static int MAX_SIZE = 100;
  9.     private int[] nSet = new int[MAX_SIZE];
  10.     private int size;
  11.     private int max;
  12.     private int min;
  13.    
  14.     public NumberSet(int min, int max) {
  15.         this.size = 0;
  16.         this.min = min;
  17.         this.max = max;
  18.     }
  19.    
  20.     public void add(int i) throws Exception {
  21.         assert(this.size >= 0): "Rozmiar zbioru mniejszy od 0... coś poszło nie tak";
  22.         if(this.size == this.MAX_SIZE) {
  23.             throw new Exception("Zbiór przepełniony");
  24.         }
  25.         assert (i >= this.min && i <= this.max) : "Podana liczba jest spoza zakresu";
  26.         this.nSet[this.size++] = i;
  27.     }
  28.    
  29.     public void remove(int i) throws Exception {
  30.         assert(this.size >= 0): "Rozmiar zbioru mniejszy od 0... coś poszło nie tak";
  31.         assert (i >= this.min && i <= this.max) : "Podana liczba jest spoza zakresu";
  32.         int[] arrayBefore = this.nSet;
  33.         this.nSet = Arrays.stream(this.nSet).filter(val -> val != i).toArray();
  34.         if(Arrays.equals(arrayBefore, this.nSet)) {
  35.             throw new Exception("W zbiorze nie ma takiej liczby");
  36.         }
  37.     }
  38.    
  39.     public int getRandomValue() throws Exception {
  40.         assert(this.size >= 0): "Rozmiar zbioru mniejszy od 0... coś poszło nie tak";
  41.         if(this.size == 0) {
  42.             throw new Exception("Zbiór jest pusty");
  43.         }
  44.         Random randomGenerator = new Random();
  45.         int randomInt = randomGenerator.nextInt(this.size);
  46.         assert(randomInt >= 0 && randomInt < this.size) : "Wygenerowany liczbę spoza zakresu";
  47.         return this.nSet[randomInt];
  48.     }
  49.    
  50.     public int getSumOfElements() throws Exception {
  51.         assert(this.size >= 0): "Rozmiar zbioru mniejszy od 0... coś poszło nie tak";
  52.         if(this.size == 0) {
  53.             throw new Exception("Zbiór jest pusty");
  54.         }
  55.         int sum = 0;
  56.         for(int j = 0; j < this.size; j++) {
  57.             sum += this.nSet[j];
  58.         }
  59.         return sum;
  60.     }
  61.    
  62.     public void divideAllElementsBy(int d) throws Exception {
  63.         assert(this.size >= 0): "Rozmiar zbioru mniejszy od 0... coś poszło nie tak";
  64.         assert(d != 0) : "Nie można dzielić przez zero!";
  65.         for(int j = 0; j < this.size; j++) {
  66.             assert( this.nSet[j] % d == 0) : "Nie można podzielić całego zbioru bez reszty";
  67.         }
  68.         for(int j = 0; j < this.size; j++) {
  69.             this.nSet[j] /= d;
  70.         }
  71.     }
  72.    
  73.     public boolean contains(int i) {
  74.         assert(this.size >= 0): "Rozmiar zbioru mniejszy od 0... coś poszło nie tak";
  75.         assert (i >= this.min && i <= this.max) : "Podana liczba jest spoza zakresu";
  76.         for(int j = 0; j < this.size; j++) {
  77.             if(this.nSet[j] == i) {
  78.                 return true;
  79.             }
  80.         }
  81.         return false;
  82.     }
  83.    
  84.     public int getSize() {
  85.         assert(this.size >= 0): "Rozmiar zbioru mniejszy od 0... coś poszło nie tak";
  86.         return this.size;
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement