-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh54
More file actions
269 lines (234 loc) · 4.48 KB
/
Ch54
File metadata and controls
269 lines (234 loc) · 4.48 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import java.util.*;
2
3
/**
4
TODO Write a one-sentence summary of your class here.
5
TODO Follow it with additional details about its purpose, what abstraction
6
it represents, and how to use it.
7
@author TODO Your Name
8
@version TODO date
9
@author Period - TODO Your Period
10
@author Assignment - Ch54 IntegerSet
11
@author Sources - TODO list collaborators
12
*/
13
public class IntegerSet {
14
15
private ArrayList<Integer> set;
16
17
/* construct a new empty IntegerSet object */
18
public IntegerSet() {
19
set = new ArrayList<Integer>();
20
}
21
22
/* utility method: return size of set */
23
public int size() {
24
25
return set.size();
26
27
}
28
29
/*
30
* @param True if k is in the set
31
*/
32
public boolean contains(int k) {
33
int count = 0;
34
for(int i=0; i<set.size();i++)
35
{
36
if (set.get(i) == k)
37
{
38
count++;
39
}
40
41
}
42
43
if (count == 0)
44
{
45
return false;
46
}
(i.e. return x < 5;). If you are returning false when the if statments condition is true and returning true in the else branch, you can just return the NOT (!) of the condition instead (i.e. return !okay;).
47
48
return true;
49
50
51
}
52
53
/**
54
* add the num into the set but only if it is not already in the set
55
* @return True if number was added to the set; False if it did not need to be added
56
*/
57
public boolean add(int num) {
58
59
if (set.contains(num)){
60
return false;
61
} else {
62
set.add(num);
63
return true;
64
}
65
}
66
67
/* add elements from Array nums to set
68
* @param nums array of integers to add to the list
69
* @return count of elements successfully added
70
* postcondition: set has no duplicates (do not add duplicates)
71
*/
72
public int addFromArray(int [] nums) {
73
74
int num = 0;
75
for(int n: nums){
76
if(add(n)){
77
num++;
78
}
79
}
80
return num;
81
82
}
83
84
/**
85
* @param return a string that looks like {1 2 3}
86
* don't worry about the spaces as long as there is a space between the numbers
87
* there can be extra spaces before or after the { }
88
*/
89
public String toString() {
90
String s = "{";
91
92
// TODO - concatenate all the number of the set into the string s
93
for (Integer i: set){
94
s = s + i + " ";
95
}
96
97
s = s + "}";
98
99
return s;
100
}
101
102
public static void main (String[] args) {
103
104
IntegerSet s = new IntegerSet();
105
106
System.out.println("Initial size is " + s.size());
107
108
System.out.println("Found a 6 in the list: " + s.contains(6));
109
110
s.add(2);
111
s.add(6);
112
s.add(8);
113
s.add(2);
114
s.add(6);
115
s.add(0);
116
System.out.println(s);
117
118
System.out.println("Size is now: " + s.size()); // should be 4
119
120
System.out.println("Adding another 0: " + s.add(0));
121
System.out.println("Adding a 10: " + s.add(10));
122
123
System.out.println("Size is now: " + s.size()); // should be 5
124
125
s = new IntegerSet();
126
int[] n = {2, 6, 8, 2, 6, 0};
127
int numAdded = s.addFromArray(n);
128
System.out.println("Size of new set from addFromArray: " + numAdded); // should be 4
129
130
System.out.println(s);
131
132
}
133
}