|
@@ -0,0 +1,245 @@
|
|
1
|
+## Building Utility Class for String Array Objects
|
|
2
|
+* **Objective**
|
|
3
|
+ * To complete the utility methods in the class [StringArrayUtils]()
|
|
4
|
+ * `int getNumberOfOccurrences(String[] stringArray, String stringToCheck)`
|
|
5
|
+ * `boolean contains(String[] stringArray, String stringToCheck)`
|
|
6
|
+ * `String[] removeValue(String[] stringArray, String stringToRemove)`
|
|
7
|
+
|
|
8
|
+* **Purpose**
|
|
9
|
+ * To establish greater familiarity with loops and arrays.
|
|
10
|
+ * To demonstrate the implementation of a [Utility class](https://www.quora.com/What-is-a-utility-class)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+<br><br><br><br>
|
|
21
|
+## Sample Behavior - `StringArrayUtils.removeValues(array, valueToRemove)`
|
|
22
|
+* **Description**
|
|
23
|
+ * Given an array of `String` objects named `array` and a `String` object named `valueToRemove`<br>create and return an array containing identical contents excluding objects whose value is equivalent to `valueToRemove`. Ensure that the length of the newly created array has been resized based on the removal of the undesired elements.
|
|
24
|
+
|
|
25
|
+### Example 1
|
|
26
|
+* Sample Script
|
|
27
|
+
|
|
28
|
+ ```
|
|
29
|
+ // : Given
|
|
30
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
31
|
+
|
|
32
|
+ // : When
|
|
33
|
+ String[] actual = StringArrayUtils.removeValues(array, "aba");
|
|
34
|
+
|
|
35
|
+ // : Then
|
|
36
|
+ System.out.println(Arrays.toString(actual));
|
|
37
|
+ ```
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+* Sample Output
|
|
42
|
+ ```
|
|
43
|
+ [baa, bab, bba, bba, bba, bba, bbb, bbb};
|
|
44
|
+ ```
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+### Example 2
|
|
49
|
+* Sample Script
|
|
50
|
+
|
|
51
|
+ ```
|
|
52
|
+ // : Given
|
|
53
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
54
|
+
|
|
55
|
+ // : When
|
|
56
|
+ String[] actual = StringArrayUtils.removeValues(array, "bba");
|
|
57
|
+
|
|
58
|
+ // : Then
|
|
59
|
+ System.out.println(Arrays.toString(actual));
|
|
60
|
+ ```
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+* Sample Output
|
|
65
|
+ ```
|
|
66
|
+ [aba, aba, baa, bab, bbb, bbb];
|
|
67
|
+ ```
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+### Example 3
|
|
72
|
+* Sample Script
|
|
73
|
+
|
|
74
|
+ ```
|
|
75
|
+ // : Given
|
|
76
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
77
|
+
|
|
78
|
+ // : When
|
|
79
|
+ String[] actual = StringArrayUtils.removeValues(array, "bbb");
|
|
80
|
+
|
|
81
|
+ // : Then
|
|
82
|
+ System.out.println(Arrays.toString(actual));
|
|
83
|
+ ```
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+* Sample Output
|
|
88
|
+ ```
|
|
89
|
+ [aba, aba, baa, bab, bba, bba, bba, bba];
|
|
90
|
+ ```
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+<br><br><br><br>
|
|
107
|
+## Sample Behavior - `StringArrayUtils.getNumberOfOccurrences(array, value)`
|
|
108
|
+* **Method Description**
|
|
109
|
+ * Given an array of `String` objects named `array` and a `String` object named `value`<br>return the number of times `value` appears in `arrays`
|
|
110
|
+
|
|
111
|
+### Example 1
|
|
112
|
+* Sample Script
|
|
113
|
+
|
|
114
|
+ ```
|
|
115
|
+ // : Given
|
|
116
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
117
|
+
|
|
118
|
+ // : When
|
|
119
|
+ int numberOfOccurrences = StringArrayUtils.getNumberOfOccurrences(array, "bba");
|
|
120
|
+
|
|
121
|
+ // : Then
|
|
122
|
+ System.out.println(numberOfOccurrences);
|
|
123
|
+ ```
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+* Sample Output
|
|
128
|
+
|
|
129
|
+ ```
|
|
130
|
+ 4
|
|
131
|
+ ```
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+### Example 2
|
|
137
|
+* Sample Script
|
|
138
|
+
|
|
139
|
+ ```
|
|
140
|
+ // : Given
|
|
141
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
142
|
+
|
|
143
|
+ // : When
|
|
144
|
+ int numberOfOccurrences = StringArrayUtils.getNumberOfOccurrences(array, "bbb");
|
|
145
|
+
|
|
146
|
+ // : Then
|
|
147
|
+ System.out.println(numberOfOccurrences);
|
|
148
|
+ ```
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+* Sample Output
|
|
153
|
+
|
|
154
|
+ ```
|
|
155
|
+ 2
|
|
156
|
+ ```
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+### Example 3
|
|
160
|
+* Sample Script
|
|
161
|
+
|
|
162
|
+ ```
|
|
163
|
+ // : Given
|
|
164
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
165
|
+
|
|
166
|
+ // : When
|
|
167
|
+ int numberOfOccurrences = StringArrayUtils.getNumberOfOccurrences(array, "baa");
|
|
168
|
+
|
|
169
|
+ // : Then
|
|
170
|
+ System.out.println(numberOfOccurrences);
|
|
171
|
+ ```
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+* Sample Output
|
|
176
|
+
|
|
177
|
+ ```
|
|
178
|
+ 1
|
|
179
|
+ ```
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+<br><br><br><br>
|
|
198
|
+## Sample Behavior - `StringArrayUtils.contains(array, value)`
|
|
199
|
+
|
|
200
|
+### Example 1
|
|
201
|
+* Sample Script
|
|
202
|
+
|
|
203
|
+ ```
|
|
204
|
+ // : Given
|
|
205
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
206
|
+
|
|
207
|
+ // : When
|
|
208
|
+ boolean outcome = StringArrayUtils.contains(array, "the");
|
|
209
|
+
|
|
210
|
+ // : Then
|
|
211
|
+ System.out.println(outcome);
|
|
212
|
+ ```
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+* Sample Output
|
|
217
|
+
|
|
218
|
+ ```
|
|
219
|
+ true
|
|
220
|
+ ```
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+### Example 2
|
|
226
|
+* Sample Script
|
|
227
|
+
|
|
228
|
+ ```
|
|
229
|
+ // : Given
|
|
230
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
231
|
+
|
|
232
|
+ // : When
|
|
233
|
+ boolean outcome = StringArrayUtils.contains(array, "potatoes");
|
|
234
|
+
|
|
235
|
+ // : Then
|
|
236
|
+ System.out.println(outcome);
|
|
237
|
+ ```
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+* Sample Output
|
|
242
|
+
|
|
243
|
+ ```
|
|
244
|
+ false
|
|
245
|
+ ```
|