Advertisement
Ansaid

PolinaSet

Nov 26th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. class Set {
  6.     private ArrayList<Boolean> set = new ArrayList<>();
  7.     private int size;
  8.  
  9.     public Set() { };
  10.  
  11.     public Set(Set other) {
  12.         this.set = other.set;
  13.         this.size = other.size;
  14.     }
  15.  
  16.     public int getSize() {
  17.         return size;
  18.     }
  19.  
  20.     public boolean getElement(int index) {
  21.         return set.get(index);
  22.     }
  23.  
  24.     private void removeSet() {
  25.         set.removeAll(set);
  26.     }
  27.  
  28.     public void includeElement(int index) {
  29.         if (index <= size - 1) {
  30.             set.set(index, true);
  31.         } else {
  32.             for (int i = 0; i < index - size; i++) {
  33.                 set.add(false);
  34.             }
  35.             set.add(true);
  36.         }
  37.         size = set.size();
  38.     }
  39.  
  40.     public void excludeElement(int index) {
  41.         if (index <= size - 1) {
  42.             set.set(index, false);
  43.         } else {
  44.             System.out.println("Этого элемента итак нет в множестве");
  45.         }
  46.     }
  47.  
  48.     public void zeroSet() {
  49.         set.clear();
  50.         set = new ArrayList<>();
  51.         size = 0;
  52.     }
  53.  
  54.     public void checkElement(int index) {
  55.         if (index <= size - 1) {
  56.             if (set.get(index)) {
  57.                 System.out.println("Этот элемент входит в множество");
  58.             } else {
  59.                 System.out.println("Этот элемент не входит в множество");
  60.             }
  61.         } else {
  62.             System.out.println("Этот элемент не входит в множество");
  63.         }
  64.     }
  65.  
  66.     public void equally (Set other) {
  67.         boolean flag = true;
  68.         if (this.size == other.getSize()) {
  69.             for (int i = 0; i < size; i++) {
  70.                 if (set.get(i) == other.getElement(i)) {
  71.                     flag = true;
  72.                 } else {
  73.                     flag = false;
  74.                     break;
  75.                 }
  76.             }
  77.         } else {
  78.             flag = false;
  79.         }
  80.  
  81.         if (flag) {
  82.             System.out.println("Множества равны");
  83.         } else {
  84.             System.out.println("Множества не равны");
  85.         }
  86.     }
  87.  
  88.     public void output() {
  89.         System.out.print("Множество: ");
  90.         for (int i = 0; i < size; i++) {
  91.             if (set.get(i)) {
  92.                 System.out.print(i + " ");
  93.             }
  94.         }
  95.         System.out.println();
  96.     }
  97.  
  98.     public void association (Set other) {
  99.         if (this.size > other.size) {
  100.             for (int i = 0; i < this.size - other.size; i++) {
  101.                 other.set.add(false);
  102.             }
  103.         } else {
  104.             for (int i = 0; i < other.size - this.size; i++) {
  105.                 this.set.add(false);
  106.             }
  107.             size = set.size();
  108.         }
  109.         for (int i = 0; i < size; i++) {
  110.             if (other.getElement(i)) {
  111.                 set.set(i, true);
  112.             }
  113.         }
  114.     }
  115.  
  116.     public void intersection (Set other) {
  117.         if (this.size > other.size) {
  118.             this.size = other.size;
  119.         }
  120.  
  121.         for (int i = 0; i < size; i++) {
  122.             if (!other.getElement(i)) {
  123.                 set.set(i, false);
  124.             }
  125.         }
  126.     }
  127.  
  128.     public void difference (Set other) {
  129.         if (this.size > other.size) {
  130.             this.size = other.size;
  131.         }
  132.         for (int i = 0; i < size; i++) {
  133.             if (other.getElement(i)) {
  134.                 set.set(i, false);
  135.             }
  136.         }
  137.     }
  138.  
  139.     public String toString() {
  140.         StringBuilder buffer = new StringBuilder();
  141.         for (int i = 0; i < size; i++) {
  142.             if (set.get(i)) {
  143.                buffer.append(i).append("\n");
  144.             }
  145.         }
  146.         return String.valueOf(buffer);
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement