Parcourir la source

committing StringArrayUtilities

chitraBegerhotta il y a 8 ans
Parent
révision
ff9542b327
3 fichiers modifiés avec 142 ajouts et 103 suppressions
  1. 99
    16
      StringArrayUtils.java
  2. 1
    87
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 99
- 16
StringArrayUtils.java Voir le fichier

@@ -1,15 +1,13 @@
1
- 
2 1
 
3
-/**
4
- * Created by leon on 1/29/18.
5
- */
2
+import java.util.*;
6 3
 public class StringArrayUtils {
7 4
     /**
8 5
      * @param array array of String objects
9 6
      * @return first element of specified array
10 7
      */ // TODO
11 8
     public static String getFirstElement(String[] array) {
12
-        return null;
9
+
10
+        return array[0];
13 11
     }
14 12
 
15 13
     /**
@@ -17,7 +15,7 @@ public class StringArrayUtils {
17 15
      * @return second element in specified array
18 16
      */
19 17
     public static String getSecondElement(String[] array) {
20
-        return null;
18
+        return array[1];
21 19
     }
22 20
 
23 21
     /**
@@ -25,7 +23,7 @@ public class StringArrayUtils {
25 23
      * @return last element in specified array
26 24
      */ // TODO
27 25
     public static String getLastElement(String[] array) {
28
-        return null;
26
+        return array[array.length - 1];
29 27
     }
30 28
 
31 29
     /**
@@ -33,7 +31,7 @@ public class StringArrayUtils {
33 31
      * @return second to last element in specified array
34 32
      */ // TODO
35 33
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
34
+        return array[array.length - 2];
37 35
     }
38 36
 
39 37
     /**
@@ -42,6 +40,11 @@ public class StringArrayUtils {
42 40
      * @return true if the array contains the specified `value`
43 41
      */ // TODO
44 42
     public static boolean contains(String[] array, String value) {
43
+        for(int i=0; i<array.length; i++){
44
+            if(array[i]==value){
45
+                return true;
46
+            }
47
+        }
45 48
         return false;
46 49
     }
47 50
 
@@ -50,7 +53,13 @@ public class StringArrayUtils {
50 53
      * @return an array with identical contents in reverse order
51 54
      */ // TODO
52 55
     public static String[] reverse(String[] array) {
53
-        return null;
56
+        String[] arr = new String[array.length];
57
+        int j = 0;
58
+        for(int i=array.length-1; i>=0; i--){
59
+            arr [j] = array[i]; 
60
+            j++;
61
+        }
62
+        return arr;
54 63
     }
55 64
 
56 65
     /**
@@ -58,7 +67,18 @@ public class StringArrayUtils {
58 67
      * @return true if the order of the array is the same backwards and forwards
59 68
      */ // TODO
60 69
     public static boolean isPalindromic(String[] array) {
61
-        return false;
70
+        String[] reversedArr = new String[array.length];
71
+        reversedArr = reverse(array);
72
+        boolean flag = true;
73
+        for(int i=0; i<reversedArr.length; i++){
74
+            if(!array[i].equalsIgnoreCase(reversedArr[i])){
75
+                flag = false;
76
+                break;
77
+            }
78
+        }
79
+        System.out.println(reversedArr);
80
+
81
+        return flag;
62 82
     }
63 83
 
64 84
     /**
@@ -66,7 +86,21 @@ public class StringArrayUtils {
66 86
      * @return true if each letter in the alphabet has been used in the array
67 87
      */ // TODO
68 88
     public static boolean isPangramic(String[] array) {
69
-        return false;
89
+        String s = "";
90
+        for(String element: array){
91
+            s += element;
92
+        }
93
+        int NO_OF_LETTERS_OF_ALPHABET = 26;
94
+        if(s.length() < NO_OF_LETTERS_OF_ALPHABET){
95
+            return false;
96
+        }
97
+        for (char ch = 'A'; ch <= 'Z'; ch++) {
98
+            if (s.indexOf(ch) < 0
99
+            && s.indexOf((char) (ch + 32)) < 0) {
100
+                return false;
101
+            }
102
+        }
103
+        return true;
70 104
     }
71 105
 
72 106
     /**
@@ -75,7 +109,13 @@ public class StringArrayUtils {
75 109
      * @return number of occurrences the specified `value` has occurred
76 110
      */ // TODO
77 111
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
112
+        int count = 0;
113
+        for(int i=0; i<array.length;i++){
114
+            if(value.equalsIgnoreCase(array[i].toString())){
115
+                count++;
116
+            }
117
+        }
118
+        return count;
79 119
     }
80 120
 
81 121
     /**
@@ -84,15 +124,38 @@ public class StringArrayUtils {
84 124
      * @return array with identical contents excluding values of `value`
85 125
      */ // TODO
86 126
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
127
+        int k = 0;
128
+        int n = getNumberOfOccurrences(array, valueToRemove);
129
+        String[] str = new String[array.length - n];
130
+        for(int i=0; i<array.length; i++)
131
+        {
132
+            if(!array[i].equals(valueToRemove))
133
+            {
134
+                str[k] = array[i];
135
+                k++;
136
+            }
137
+           
138
+        }
139
+        
140
+        return str; 
88 141
     }
89 142
 
143
+
90 144
     /**
91 145
      * @param array array of chars
92 146
      * @return array of Strings with consecutive duplicates removes
93 147
      */ // TODO
94 148
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
149
+        ArrayList<String> removedList = new ArrayList<String>();
150
+        removedList.add(array[0]);
151
+        for(int i=1; i<array.length; i++){
152
+            if(!array[i-1].equalsIgnoreCase(array[i])){
153
+                removedList.add(array[i]);
154
+            }
155
+        }
156
+        String[] finalArray = removedList.toArray(new String[removedList.size()]);
157
+
158
+        return finalArray;
96 159
     }
97 160
 
98 161
     /**
@@ -100,8 +163,28 @@ public class StringArrayUtils {
100 163
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 164
      */ // TODO
102 165
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
166
+        ArrayList<String> list = new ArrayList<>();
167
+        list.add(array[0]);
168
+        int k=0;
105 169
 
170
+        for(int i = 1 ; i < array.length ; i++) {
171
+
172
+            if(array[i-1].equalsIgnoreCase(array[i])){
173
+
174
+                String lastElement = list.get(list.size()-1);
175
+
176
+                lastElement = lastElement.concat(array[i]);
177
+
178
+                list.set(list.size()-1,lastElement);
179
+            }
180
+            else{
181
+                list.add(array[i]);
182
+            }
183
+        }
184
+
185
+        String[] newArray = list.toArray(new String[list.size()]);
186
+        return newArray;
187
+    }
106 188
 
107 189
 }
190
+

+ 1
- 87
StringArrayUtilsTest.java Voir le fichier

@@ -1,4 +1,3 @@
1
- 
2 1
 
3 2
 import org.junit.Assert;
4 3
 import org.junit.Test;
@@ -24,7 +23,6 @@ public class StringArrayUtilsTest {
24 23
         Assert.assertEquals(expected, actual);
25 24
     }
26 25
 
27
-
28 26
     @Test
29 27
     public void testGetFirstElement3() {
30 28
         String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -39,13 +37,6 @@ public class StringArrayUtilsTest {
39 37
 
40 38
 
41 39
 
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49 40
     @Test
50 41
     public void testGetSecondElement1() {
51 42
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -62,7 +53,6 @@ public class StringArrayUtilsTest {
62 53
         Assert.assertEquals(expected, actual);
63 54
     }
64 55
 
65
-
66 56
     @Test
67 57
     public void testGetSecondElement3() {
68 58
         String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -76,11 +66,6 @@ public class StringArrayUtilsTest {
76 66
 
77 67
 
78 68
 
79
-
80
-
81
-
82
-
83
-
84 69
     @Test
85 70
     public void testGetLastElement1() {
86 71
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -97,7 +82,6 @@ public class StringArrayUtilsTest {
97 82
         Assert.assertEquals(expected, actual);
98 83
     }
99 84
 
100
-
101 85
     @Test
102 86
     public void testGetLastElement3() {
103 87
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
@@ -116,16 +100,6 @@ public class StringArrayUtilsTest {
116 100
 
117 101
 
118 102
 
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129 103
     @Test
130 104
     public void testGetSecondToLastElement1() {
131 105
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -142,7 +116,6 @@ public class StringArrayUtilsTest {
142 116
         Assert.assertEquals(expected, actual);
143 117
     }
144 118
 
145
-
146 119
     @Test
147 120
     public void testGetSecondToLastElement3() {
148 121
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
@@ -159,14 +132,6 @@ public class StringArrayUtilsTest {
159 132
 
160 133
 
161 134
 
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170 135
     @Test
171 136
     public void testContains() {
172 137
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -182,12 +147,6 @@ public class StringArrayUtilsTest {
182 147
 
183 148
 
184 149
 
185
-
186
-
187
-
188
-
189
-
190
-
191 150
     @Test
192 151
     public void testReverse1() {
193 152
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -196,7 +155,6 @@ public class StringArrayUtilsTest {
196 155
         Assert.assertEquals(expected, actual);
197 156
     }
198 157
 
199
-
200 158
     @Test
201 159
     public void testReverse2() {
202 160
         String[] array  = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
@@ -205,7 +163,6 @@ public class StringArrayUtilsTest {
205 163
         Assert.assertEquals(expected, actual);
206 164
     }
207 165
 
208
-
209 166
     @Test
210 167
     public void testReverse3() {
211 168
         String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -217,11 +174,6 @@ public class StringArrayUtilsTest {
217 174
 
218 175
 
219 176
 
220
-
221
-
222
-
223
-
224
-
225 177
     @Test
226 178
     public void testIsPalindromic1() {
227 179
         String[] array = {"a", "b", "c", "b", "a"};
@@ -230,7 +182,6 @@ public class StringArrayUtilsTest {
230 182
     }
231 183
 
232 184
 
233
-
234 185
     @Test
235 186
     public void testIsPalindromic2() {
236 187
         String[] array = {"Is this a palindrome?", "This is a palindrome", "Is this a palindrome?"};
@@ -238,7 +189,6 @@ public class StringArrayUtilsTest {
238 189
         Assert.assertTrue(outcome);
239 190
     }
240 191
 
241
-
242 192
     @Test
243 193
     public void testIsPalindromic3() {
244 194
         String[] array = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
@@ -250,10 +200,6 @@ public class StringArrayUtilsTest {
250 200
 
251 201
 
252 202
 
253
-
254
-
255
-
256
-
257 203
     @Test
258 204
     public void testIsPangramic1() {
259 205
         String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"};
@@ -275,7 +221,6 @@ public class StringArrayUtilsTest {
275 221
         Assert.assertTrue(outcome);
276 222
     }
277 223
 
278
-
279 224
     @Test
280 225
     public void testIsPangramic4() {
281 226
         String[] array = {"a", "b", "c", "d"};
@@ -289,12 +234,6 @@ public class StringArrayUtilsTest {
289 234
 
290 235
 
291 236
 
292
-
293
-
294
-
295
-
296
-
297
-
298 237
     @Test
299 238
     public void testGetNumberOfOccurrences1() {
300 239
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -325,13 +264,6 @@ public class StringArrayUtilsTest {
325 264
 
326 265
 
327 266
 
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335 267
     @Test
336 268
     public void testRemoveConsecutiveDuplicates1() {
337 269
         String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
@@ -341,7 +273,6 @@ public class StringArrayUtilsTest {
341 273
     }
342 274
 
343 275
 
344
-
345 276
     @Test
346 277
     public void testRemoveConsecutiveDuplicates2() {
347 278
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
@@ -350,7 +281,6 @@ public class StringArrayUtilsTest {
350 281
         Assert.assertEquals(actual, expected);
351 282
     }
352 283
 
353
-
354 284
     @Test
355 285
     public void testRemoveConsecutiveDuplicates3() {
356 286
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "aba", "bbb"};
@@ -364,11 +294,6 @@ public class StringArrayUtilsTest {
364 294
 
365 295
 
366 296
 
367
-
368
-
369
-
370
-
371
-
372 297
     @Test
373 298
     public void testRemovePackDuplicates1() {
374 299
         String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
@@ -377,7 +302,6 @@ public class StringArrayUtilsTest {
377 302
         Assert.assertEquals(expected, actual);
378 303
     }
379 304
 
380
-
381 305
     @Test
382 306
     public void testRemovePackDuplicates2() {
383 307
         String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"};
@@ -387,7 +311,6 @@ public class StringArrayUtilsTest {
387 311
     }
388 312
 
389 313
 
390
-
391 314
     @Test
392 315
     public void testRemovePackDuplicates3() {
393 316
         String[] array = {"m", "o", "o", "n", "m", "a", "n"};
@@ -399,16 +322,13 @@ public class StringArrayUtilsTest {
399 322
 
400 323
 
401 324
 
402
-
403
-
404
-
405
-
406 325
     @Test
407 326
     public void testRemoveValue() {
408 327
         String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
409 328
         String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
410 329
         String[] actual = StringArrayUtils.removeValue(array, "The");
411 330
         Assert.assertEquals(expected, actual);
331
+
412 332
     }
413 333
 
414 334
     @Test
@@ -432,10 +352,4 @@ public class StringArrayUtilsTest {
432 352
 
433 353
 
434 354
 
435
-
436
-
437
-
438
-
439
-
440
-
441 355
 }

+ 42
- 0
package.bluej Voir le fichier

@@ -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=722
6
+editor.fx.0.width=800
7
+editor.fx.0.x=465
8
+editor.fx.0.y=24
9
+objectbench.height=101
10
+objectbench.width=461
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=299
16
+package.editor.y=128
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