Arin Turpin před 8 roky
rodič
revize
d134227706
3 změnil soubory, kde provedl 125 přidání a 169 odebrání
  1. 81
    12
      StringArrayUtils.java
  2. 2
    157
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 81
- 12
StringArrayUtils.java Zobrazit soubor

1
- 
2
-
1
+import java.util.*;
3
 /**
2
 /**
4
  * Created by leon on 1/29/18.
3
  * Created by leon on 1/29/18.
5
  */
4
  */
9
      * @return first element of specified array
8
      * @return first element of specified array
10
      */ // TODO
9
      */ // TODO
11
     public static String getFirstElement(String[] array) {
10
     public static String getFirstElement(String[] array) {
12
-        return null;
11
+        return array[0];
13
     }
12
     }
14
 
13
 
15
     /**
14
     /**
17
      * @return second element in specified array
16
      * @return second element in specified array
18
      */
17
      */
19
     public static String getSecondElement(String[] array) {
18
     public static String getSecondElement(String[] array) {
20
-        return null;
19
+        return array[1];
21
     }
20
     }
22
 
21
 
23
     /**
22
     /**
25
      * @return last element in specified array
24
      * @return last element in specified array
26
      */ // TODO
25
      */ // TODO
27
     public static String getLastElement(String[] array) {
26
     public static String getLastElement(String[] array) {
28
-        return null;
27
+        return array[array.length-1];
29
     }
28
     }
30
 
29
 
31
     /**
30
     /**
33
      * @return second to last element in specified array
32
      * @return second to last element in specified array
34
      */ // TODO
33
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
34
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
35
+        return array[array.length-2];
37
     }
36
     }
38
 
37
 
39
     /**
38
     /**
42
      * @return true if the array contains the specified `value`
41
      * @return true if the array contains the specified `value`
43
      */ // TODO
42
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
      * @return an array with identical contents in reverse order
56
      * @return an array with identical contents in reverse order
51
      */ // TODO
57
      */ // TODO
52
     public static String[] reverse(String[] array) {
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
      * @return true if the order of the array is the same backwards and forwards
75
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
76
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
         return false;
102
         return false;
103
+        }
104
+        return true;
105
+
106
+         */
107
+        return answer;
62
     }
108
     }
63
 
109
 
64
     /**
110
     /**
75
      * @return number of occurrences the specified `value` has occurred
121
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
122
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
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
      * @return array with identical contents excluding values of `value`
136
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
137
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
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
      * @return array of Strings with consecutive duplicates removes
153
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
154
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
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
         return null;
173
         return null;
104
     }
174
     }
105
 
175
 
106
-
107
 }
176
 }

+ 2
- 157
StringArrayUtilsTest.java Zobrazit soubor

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

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