Ben Blinebury 8 år sedan
förälder
incheckning
852448833f
2 ändrade filer med 180 tillägg och 13 borttagningar
  1. 138
    13
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 138
- 13
StringArrayUtils.java Visa fil

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

+ 42
- 0
package.bluej Visa fil

1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=794
6
+editor.fx.0.width=925
7
+editor.fx.0.x=451
8
+editor.fx.0.y=23
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=459
16
+package.editor.y=226
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=240
35
+target1.y=20
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