-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.java
169 lines (150 loc) · 3.48 KB
/
Set.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.ArrayList;
/**
* This class recreates the behavior of a Set.
* Also calculates different subsets of given Sets.
*
* @author jonasblome
* @author n-c0de-r
* @version 17.06.21
*/
public class Set<type>{
private ArrayList<type> set;
/**
* Constructor for the Set class.
*/
public Set() {
set = new ArrayList<>();
}
// Basic data methods start here
/**
* Setter method for Set.
*
* @param e Adding an element to the Set.
*/
public void add(type e) {
if (!contains(e)) {
set.add(e);
} else {
return;
}
}
/**
* Getter method, checking if an element is already in the Set.
*
* @param e Element to be inserted into the Set.
* @return True if the element is already in the Set.
*/
public boolean contains(type e) {
if (set.contains(e)) {
return true;
} else {
return false;
}
}
/**
* Getter method, returning element at a position.
*
* @param i Index number to get the element at.
* @return Return the Object of the given type.
*/
public type get(int i) {
return set.get(i);
}
/**
* Getter method for number of elements in the Set
*
* @return Integer number of elements.
*/
public int length() {
return set.size();
}
/**
* Setter method to remove an element from the Set.
*
* @param e Element to be removed from the Set.
*/
public void remove(type e) {
if (contains(e)) {
int i = set.indexOf(e);
set.remove(i);
} else {
return;
}
}
/**
* Convert all element as a String
*
* @return Resulting String of elements in curly braces
*/
public String toString() {
String res = "{ ";
for (type t : set) {
res += t + ", ";
}
// Remove the last instance of ', ' adding closing parentheses
res = res.substring(0, res.length()-2) + " }";
return res;
}
//Calculation methods start here
/**
* Setter method, create in intersection of two Sets.
*
* @param sub The second Set to find the intersection with the main one.
* @return Resulting Set, intersection of both.
*/
public Set<type> intersection(Set<type> sub) {
Set<type> res = new Set<>();
for (int i = 0; i<sub.length(); i++) {
if (contains(sub.get(i))) {
res.add(sub.get(i));
}
}
return res;
}
/**
* Setter method, create a power Set of two given Sets.
*
* @param sub The second Set to combine with the main one.
* @return Resulting Set, permutation of both.
*/
public Set<type> powerset(Set<type> sub) {
Set<type> res = this;
//
// for (int i = 0; i<sub.length(); i++) {
// if (!contains(sub.get(i))) {
// res.add(sub.get(i));
// }
// }
return res;
}
/**
* Setter method, create in subtraction of two Sets.
*
* @param sub The second Set to subtract from the main one.
* @return Resulting Set, subtraction of both.
*/
public Set<type> subtraction(Set<type> sub) {
Set<type> res = this;
for (int i = 0; i<sub.length(); i++) {
if (contains(sub.get(i))) {
res.remove(sub.get(i));
}
}
return res;
}
/**
* Setter method, unite two given Sets together.
*
* @param sub The second Set to merge with the main one.
* @return Resulting Set, combination of both.
*/
public Set<type> union(Set<type> sub) {
Set<type> res = this;
for (int i = 0; i<sub.length(); i++) {
if (!contains(sub.get(i))) {
res.add(sub.get(i));
}
}
return res;
}
}