jpsp91 8 anos atrás
pai
commit
5c188163e1
3 arquivos alterados com 172 adições e 17 exclusões
  1. 130
    16
      StringArrayUtils.java
  2. 0
    1
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 130
- 16
StringArrayUtils.java Ver arquivo

@@ -1,4 +1,4 @@
1
- 
1
+ import java.util.Arrays;
2 2
 
3 3
 /**
4 4
  * Created by leon on 1/29/18.
@@ -9,7 +9,7 @@ public class StringArrayUtils {
9 9
      * @return first element of specified array
10 10
      */ // TODO
11 11
     public static String getFirstElement(String[] array) {
12
-        return null;
12
+        return array[0];
13 13
     }
14 14
 
15 15
     /**
@@ -17,7 +17,7 @@ public class StringArrayUtils {
17 17
      * @return second element in specified array
18 18
      */
19 19
     public static String getSecondElement(String[] array) {
20
-        return null;
20
+        return array[1];
21 21
     }
22 22
 
23 23
     /**
@@ -25,7 +25,7 @@ public class StringArrayUtils {
25 25
      * @return last element in specified array
26 26
      */ // TODO
27 27
     public static String getLastElement(String[] array) {
28
-        return null;
28
+        return array[array.length-1];
29 29
     }
30 30
 
31 31
     /**
@@ -33,7 +33,7 @@ public class StringArrayUtils {
33 33
      * @return second to last element in specified array
34 34
      */ // TODO
35 35
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
36
+        return array[array.length-2];
37 37
     }
38 38
 
39 39
     /**
@@ -42,7 +42,19 @@ public class StringArrayUtils {
42 42
      * @return true if the array contains the specified `value`
43 43
      */ // TODO
44 44
     public static boolean contains(String[] array, String value) {
45
-        return false;
45
+        boolean a = true;
46
+        
47
+        for(int i = 0; i <= array.length; i++){
48
+            if(array[i].equals(value)){
49
+                a = true;
50
+                break;
51
+            }
52
+            else{
53
+                a = false;
54
+            }
55
+        }
56
+        
57
+        return a;
46 58
     }
47 59
 
48 60
     /**
@@ -50,7 +62,14 @@ public class StringArrayUtils {
50 62
      * @return an array with identical contents in reverse order
51 63
      */ // TODO
52 64
     public static String[] reverse(String[] array) {
53
-        return null;
65
+        String[] result = new String[array.length];
66
+        
67
+        for (int i = 0; i< array.length; i++){
68
+            result[i] = 
69
+            array[array.length-i-1];
70
+        }
71
+        
72
+        return result;
54 73
     }
55 74
 
56 75
     /**
@@ -58,7 +77,23 @@ public class StringArrayUtils {
58 77
      * @return true if the order of the array is the same backwards and forwards
59 78
      */ // TODO
60 79
     public static boolean isPalindromic(String[] array) {
61
-        return false;
80
+        int length = array.length;
81
+        
82
+        for (int index = 0; index < array.length; index++) {
83
+        String start = array[index];
84
+        
85
+        String end = array[--length];
86
+        
87
+        if (length < index) {
88
+            return true;
89
+        }
90
+        
91
+        if (start != end) {
92
+            return false;
93
+        }
94
+       }
95
+        
96
+        return true;
62 97
     }
63 98
 
64 99
     /**
@@ -66,7 +101,19 @@ public class StringArrayUtils {
66 101
      * @return true if each letter in the alphabet has been used in the array
67 102
      */ // TODO
68 103
     public static boolean isPangramic(String[] array) {
69
-        return false;
104
+        String arrayToString = array.toString();
105
+        String str = arrayToString.toLowerCase();
106
+        
107
+        boolean answer = true;
108
+        
109
+        for(char c = 'a';c <= 'z'; ++c) {
110
+            if(!str.contains(String.valueOf(c))) {
111
+                answer = false;
112
+                break;
113
+            }
114
+        }
115
+
116
+        return answer;
70 117
     }
71 118
 
72 119
     /**
@@ -75,7 +122,15 @@ public class StringArrayUtils {
75 122
      * @return number of occurrences the specified `value` has occurred
76 123
      */ // TODO
77 124
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
125
+        int counter = 0;
126
+        
127
+        for(int i = 0; i <= array.length-1; i++){
128
+            if(array[i].equals(value)){
129
+                counter = counter + 1;
130
+            }
131
+        }
132
+        
133
+        return counter;
79 134
     }
80 135
 
81 136
     /**
@@ -84,7 +139,24 @@ public class StringArrayUtils {
84 139
      * @return array with identical contents excluding values of `value`
85 140
      */ // TODO
86 141
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
142
+        int count = 0;
143
+        
144
+        for(int i = 0; i < array.length; i++){
145
+            if(array[i].equals(valueToRemove)){
146
+                count = count + 1;
147
+            }
148
+        }
149
+        
150
+        String[] newArray = new String[array.length - count];
151
+        
152
+        int j = 0;
153
+        for(int i = 0; i < array.length; i++){
154
+            if(!array[i].equals(valueToRemove)){
155
+                newArray[j++] = array[i];
156
+            }
157
+        }
158
+        
159
+        return newArray;
88 160
     }
89 161
 
90 162
     /**
@@ -92,16 +164,58 @@ public class StringArrayUtils {
92 164
      * @return array of Strings with consecutive duplicates removes
93 165
      */ // TODO
94 166
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
167
+        int dupCounter = 0;
168
+        
169
+        for(int i = 0; i < array.length-1; i++){
170
+            if(array[i].equals(array[i+1])){
171
+                dupCounter = dupCounter + 1;
172
+            }
173
+        }
174
+        
175
+        String[] newArray = new String[array.length - dupCounter];
176
+        
177
+        //String [] newArray = new String[array.length];
178
+        
179
+        int j = 0;
180
+        for(int i = 0; i < array.length-1; i++){
181
+            if(!array[i].equals(array[i + 1])){
182
+                newArray[j++] = (array[i]);
183
+            }
184
+        }
185
+        
186
+        newArray[newArray.length-1] = array[array.length-1];
187
+        
188
+        return newArray;
96 189
     }
97 190
 
98 191
     /**
99 192
      * @param array array of chars
100
-     * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
193
+     * @return array of Strings with each consecutive duplicate occurrence concatenated 
194
+     * @as a single string in an array of Strings
101 195
      */ // TODO
102 196
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105
-
197
+        //StringBuilder sb = new StringBuilder();
198
+        
199
+        int dupCounter = 0;
200
+        
201
+        for(int i = 0; i < array.length-1; i++){
202
+            if(array[i].equals(array[i+1])){
203
+                dupCounter = dupCounter + 1;
204
+            }
205
+        }
206
+        
207
+        String[] newArray = new String[array.length - dupCounter];
208
+        
209
+        int j = 0;
210
+        for(int i = 0; i < array.length-1; i++){
211
+            if(array[i].equals(array[i+1])){
212
+                StringBuilder sb = new StringBuilder(array[i]);
213
+                sb.append(array[i+1]);
214
+                
215
+                newArray[j++] = (array[i]);
216
+            }
217
+        }
106 218
 
219
+        return newArray;
220
+    }
107 221
 }

+ 0
- 1
StringArrayUtilsTest.java Ver arquivo

@@ -341,7 +341,6 @@ public class StringArrayUtilsTest {
341 341
     }
342 342
 
343 343
 
344
-
345 344
     @Test
346 345
     public void testRemoveConsecutiveDuplicates2() {
347 346
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};

+ 42
- 0
package.bluej Ver arquivo

@@ -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=0
6
+editor.fx.0.width=0
7
+editor.fx.0.x=0
8
+editor.fx.0.y=0
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=114
16
+package.editor.y=-921
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