Arin Turpin 8 лет назад
Родитель
Сommit
d134227706
3 измененных файлов: 125 добавлений и 169 удалений
  1. 81
    12
      StringArrayUtils.java
  2. 2
    157
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 81
- 12
StringArrayUtils.java Просмотреть файл

@@ -1,5 +1,4 @@
1
- 
2
-
1
+import java.util.*;
3 2
 /**
4 3
  * Created by leon on 1/29/18.
5 4
  */
@@ -9,7 +8,7 @@ public class StringArrayUtils {
9 8
      * @return first element of specified array
10 9
      */ // TODO
11 10
     public static String getFirstElement(String[] array) {
12
-        return null;
11
+        return array[0];
13 12
     }
14 13
 
15 14
     /**
@@ -17,7 +16,7 @@ public class StringArrayUtils {
17 16
      * @return second element in specified array
18 17
      */
19 18
     public static String getSecondElement(String[] array) {
20
-        return null;
19
+        return array[1];
21 20
     }
22 21
 
23 22
     /**
@@ -25,7 +24,7 @@ public class StringArrayUtils {
25 24
      * @return last element in specified array
26 25
      */ // TODO
27 26
     public static String getLastElement(String[] array) {
28
-        return null;
27
+        return array[array.length-1];
29 28
     }
30 29
 
31 30
     /**
@@ -33,7 +32,7 @@ public class StringArrayUtils {
33 32
      * @return second to last element in specified array
34 33
      */ // TODO
35 34
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
35
+        return array[array.length-2];
37 36
     }
38 37
 
39 38
     /**
@@ -42,7 +41,14 @@ public class StringArrayUtils {
42 41
      * @return true if the array contains the specified `value`
43 42
      */ // TODO
44 43
     public static boolean contains(String[] array, String value) {
45
-        return false;
44
+        boolean answer = false;
45
+        for(String x : array){
46
+            if(x.equals(value)){
47
+                answer = true;
48
+            }
49
+
50
+        }
51
+        return answer;
46 52
     }
47 53
 
48 54
     /**
@@ -50,7 +56,18 @@ public class StringArrayUtils {
50 56
      * @return an array with identical contents in reverse order
51 57
      */ // TODO
52 58
     public static String[] reverse(String[] array) {
53
-        return null;
59
+        /*   StringBuilder x = new StringBuilder(array.toString());
60
+        x.reverse();
61
+        String[] answer = new String[x.toString().split(" ").length](x.reverse().toString());
62
+
63
+         */   
64
+        String[] x = new String[array.length];
65
+        int p = 0;
66
+        for(int i = array.length-1; i >= 0; i--){           
67
+            x[p] = array[i];
68
+            p++;
69
+        }
70
+        return x;
54 71
     }
55 72
 
56 73
     /**
@@ -58,7 +75,36 @@ public class StringArrayUtils {
58 75
      * @return true if the order of the array is the same backwards and forwards
59 76
      */ // TODO
60 77
     public static boolean isPalindromic(String[] array) {
78
+        int x = 0;
79
+        int y = array.length - 1;
80
+        boolean answer = true;
81
+        if(array.length % 2 == 0){
82
+            while(x < y && answer == true){
83
+                if(array[x] == array[y]){
84
+                    x++;
85
+                    y--;
86
+                } else{
87
+                    answer = false;
88
+                }
89
+            }
90
+        } else{
91
+            while(x != y && answer == true){
92
+                if(array[x] == array[y]){
93
+                    x++;
94
+                    y--;
95
+                } else{
96
+                    answer = false;
97
+                }
98
+            }
99
+        }
100
+
101
+        /*      if(reverse(array) != array){
61 102
         return false;
103
+        }
104
+        return true;
105
+
106
+         */
107
+        return answer;
62 108
     }
63 109
 
64 110
     /**
@@ -75,7 +121,13 @@ public class StringArrayUtils {
75 121
      * @return number of occurrences the specified `value` has occurred
76 122
      */ // TODO
77 123
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
124
+        int counter = 0;
125
+        for(String x : array){
126
+            if(x == value){
127
+                counter++;
128
+            }
129
+        }
130
+        return counter;
79 131
     }
80 132
 
81 133
     /**
@@ -84,7 +136,16 @@ public class StringArrayUtils {
84 136
      * @return array with identical contents excluding values of `value`
85 137
      */ // TODO
86 138
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
139
+        ArrayList<String> x = new ArrayList<String>((Arrays.asList(array)));
140
+        for(String h : x){
141
+            if(h == valueToRemove){
142
+                x.remove(x.indexOf(h));
143
+            }
144
+        }
145
+
146
+        String[] q = new String[x.size()];
147
+        q = x.toArray(q);
148
+        return q;
88 149
     }
89 150
 
90 151
     /**
@@ -92,7 +153,16 @@ public class StringArrayUtils {
92 153
      * @return array of Strings with consecutive duplicates removes
93 154
      */ // TODO
94 155
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
156
+        ArrayList<String> x = new ArrayList<String>();
157
+        // String[] y = new String[];
158
+        for(int i = 0; i < array.length; i++){
159
+            if(array[i] == array[i + 1]){
160
+                x.add(array[i]);
161
+            }	
162
+        }
163
+        String[] y = new String[x.size()];
164
+        x.toArray(y);
165
+        return y;
96 166
     }
97 167
 
98 168
     /**
@@ -103,5 +173,4 @@ public class StringArrayUtils {
103 173
         return null;
104 174
     }
105 175
 
106
-
107 176
 }

+ 2
- 157
StringArrayUtilsTest.java Просмотреть файл

@@ -33,19 +33,6 @@ public class StringArrayUtilsTest {
33 33
         Assert.assertEquals(expected, actual);
34 34
     }
35 35
 
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49 36
     @Test
50 37
     public void testGetSecondElement1() {
51 38
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -71,16 +58,6 @@ public class StringArrayUtilsTest {
71 58
         Assert.assertEquals(expected, actual);
72 59
     }
73 60
 
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84 61
     @Test
85 62
     public void testGetLastElement1() {
86 63
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -97,7 +74,6 @@ public class StringArrayUtilsTest {
97 74
         Assert.assertEquals(expected, actual);
98 75
     }
99 76
 
100
-
101 77
     @Test
102 78
     public void testGetLastElement3() {
103 79
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
@@ -106,26 +82,6 @@ public class StringArrayUtilsTest {
106 82
         Assert.assertEquals(expected, actual);
107 83
     }
108 84
 
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129 85
     @Test
130 86
     public void testGetSecondToLastElement1() {
131 87
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -151,22 +107,6 @@ public class StringArrayUtilsTest {
151 107
         Assert.assertEquals(expected, actual);
152 108
     }
153 109
 
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170 110
     @Test
171 111
     public void testContains() {
172 112
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -176,18 +116,6 @@ public class StringArrayUtilsTest {
176 116
         }
177 117
     }
178 118
 
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191 119
     @Test
192 120
     public void testReverse1() {
193 121
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -196,7 +124,6 @@ public class StringArrayUtilsTest {
196 124
         Assert.assertEquals(expected, actual);
197 125
     }
198 126
 
199
-
200 127
     @Test
201 128
     public void testReverse2() {
202 129
         String[] array  = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
@@ -205,7 +132,6 @@ public class StringArrayUtilsTest {
205 132
         Assert.assertEquals(expected, actual);
206 133
     }
207 134
 
208
-
209 135
     @Test
210 136
     public void testReverse3() {
211 137
         String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -213,15 +139,6 @@ public class StringArrayUtilsTest {
213 139
         Assert.assertEquals(expected, actual);
214 140
     }
215 141
 
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225 142
     @Test
226 143
     public void testIsPalindromic1() {
227 144
         String[] array = {"a", "b", "c", "b", "a"};
@@ -229,8 +146,6 @@ public class StringArrayUtilsTest {
229 146
         Assert.assertTrue(outcome);
230 147
     }
231 148
 
232
-
233
-
234 149
     @Test
235 150
     public void testIsPalindromic2() {
236 151
         String[] array = {"Is this a palindrome?", "This is a palindrome", "Is this a palindrome?"};
@@ -238,22 +153,13 @@ public class StringArrayUtilsTest {
238 153
         Assert.assertTrue(outcome);
239 154
     }
240 155
 
241
-
242 156
     @Test
243 157
     public void testIsPalindromic3() {
244 158
         String[] array = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
245 159
         boolean outcome = StringArrayUtils.isPalindromic(array);
246 160
         Assert.assertFalse(outcome);
247 161
     }
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
162
+    
257 163
     @Test
258 164
     public void testIsPangramic1() {
259 165
         String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"};
@@ -275,7 +181,6 @@ public class StringArrayUtilsTest {
275 181
         Assert.assertTrue(outcome);
276 182
     }
277 183
 
278
-
279 184
     @Test
280 185
     public void testIsPangramic4() {
281 186
         String[] array = {"a", "b", "c", "d"};
@@ -283,18 +188,6 @@ public class StringArrayUtilsTest {
283 188
         Assert.assertFalse(outcome);
284 189
     }
285 190
 
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298 191
     @Test
299 192
     public void testGetNumberOfOccurrences1() {
300 193
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -319,19 +212,6 @@ public class StringArrayUtilsTest {
319 212
         Assert.assertEquals(actual, expected);
320 213
     }
321 214
 
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335 215
     @Test
336 216
     public void testRemoveConsecutiveDuplicates1() {
337 217
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -340,8 +220,6 @@ public class StringArrayUtilsTest {
340 220
         Assert.assertEquals(actual, expected);
341 221
     }
342 222
 
343
-
344
-
345 223
     @Test
346 224
     public void testRemoveConsecutiveDuplicates2() {
347 225
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
@@ -350,7 +228,6 @@ public class StringArrayUtilsTest {
350 228
         Assert.assertEquals(actual, expected);
351 229
     }
352 230
 
353
-
354 231
     @Test
355 232
     public void testRemoveConsecutiveDuplicates3() {
356 233
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "aba", "bbb"};
@@ -359,16 +236,6 @@ public class StringArrayUtilsTest {
359 236
         Assert.assertEquals(actual, expected);
360 237
     }
361 238
 
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372 239
     @Test
373 240
     public void testRemovePackDuplicates1() {
374 241
         String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
@@ -377,7 +244,6 @@ public class StringArrayUtilsTest {
377 244
         Assert.assertEquals(expected, actual);
378 245
     }
379 246
 
380
-
381 247
     @Test
382 248
     public void testRemovePackDuplicates2() {
383 249
         String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"};
@@ -386,8 +252,6 @@ public class StringArrayUtilsTest {
386 252
         Assert.assertEquals(expected, actual);
387 253
     }
388 254
 
389
-
390
-
391 255
     @Test
392 256
     public void testRemovePackDuplicates3() {
393 257
         String[] array = {"m", "o", "o", "n", "m", "a", "n"};
@@ -395,14 +259,7 @@ public class StringArrayUtilsTest {
395 259
         String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
396 260
         Assert.assertEquals(expected, actual);
397 261
     }
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
262
+    
406 263
     @Test
407 264
     public void testRemoveValue() {
408 265
         String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -426,16 +283,4 @@ public class StringArrayUtilsTest {
426 283
         String[] actual = StringArrayUtils.removeValue(array, "brown");
427 284
         Assert.assertEquals(expected, actual);
428 285
     }
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441 286
 }

+ 42
- 0
package.bluej Просмотреть файл

@@ -0,0 +1,42 @@
1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=707
6
+editor.fx.0.width=762
7
+editor.fx.0.x=0
8
+editor.fx.0.y=22
9
+objectbench.height=101
10
+objectbench.width=776
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.8007380073800738
13
+package.editor.height=427
14
+package.editor.width=674
15
+package.editor.x=20
16
+package.editor.y=42
17
+package.frame.height=600
18
+package.frame.width=800
19
+package.numDependencies=1
20
+package.numTargets=2
21
+package.showExtends=true
22
+package.showUses=true
23
+project.charset=UTF-8
24
+readme.height=58
25
+readme.name=@README
26
+readme.width=47
27
+readme.x=10
28
+readme.y=10
29
+target1.height=50
30
+target1.name=StringArrayUtilsTest
31
+target1.showInterface=false
32
+target1.type=UnitTestTargetJunit4
33
+target1.width=150
34
+target1.x=130
35
+target1.y=10
36
+target2.height=50
37
+target2.name=StringArrayUtils
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=120
41
+target2.x=70
42
+target2.y=70