瀏覽代碼

tests pass

Allison Ziegler 8 年之前
父節點
當前提交
150e0d9cd0
共有 2 個文件被更改,包括 151 次插入14 次删除
  1. 109
    14
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 109
- 14
StringArrayUtils.java 查看文件

1
- 
1
+import java.util.Arrays; 
2
 
2
 
3
 /**
3
 /**
4
  * Created by leon on 1/29/18.
4
  * Created by leon on 1/29/18.
9
      * @return first element of specified array
9
      * @return first element of specified array
10
      */ // TODO
10
      */ // TODO
11
     public static String getFirstElement(String[] array) {
11
     public static String getFirstElement(String[] array) {
12
-        return null;
12
+        return array[0];
13
     }
13
     }
14
 
14
 
15
     /**
15
     /**
17
      * @return second element in specified array
17
      * @return second element in specified array
18
      */
18
      */
19
     public static String getSecondElement(String[] array) {
19
     public static String getSecondElement(String[] array) {
20
-        return null;
20
+        return array[1];
21
     }
21
     }
22
 
22
 
23
     /**
23
     /**
25
      * @return last element in specified array
25
      * @return last element in specified array
26
      */ // TODO
26
      */ // TODO
27
     public static String getLastElement(String[] array) {
27
     public static String getLastElement(String[] array) {
28
-        return null;
28
+        return array[array.length-1];
29
     }
29
     }
30
 
30
 
31
     /**
31
     /**
33
      * @return second to last element in specified array
33
      * @return second to last element in specified array
34
      */ // TODO
34
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
35
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
36
+        return array[array.length-2];
37
     }
37
     }
38
 
38
 
39
     /**
39
     /**
42
      * @return true if the array contains the specified `value`
42
      * @return true if the array contains the specified `value`
43
      */ // TODO
43
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
44
     public static boolean contains(String[] array, String value) {
45
-        return false;
45
+        boolean found = false;
46
+        for (String currVal : array) {
47
+            if (currVal.equals(value)) {
48
+                found = true;
49
+                break;
50
+            }
51
+        }
52
+        return found;
46
     }
53
     }
47
 
54
 
48
     /**
55
     /**
50
      * @return an array with identical contents in reverse order
57
      * @return an array with identical contents in reverse order
51
      */ // TODO
58
      */ // TODO
52
     public static String[] reverse(String[] array) {
59
     public static String[] reverse(String[] array) {
53
-        return null;
60
+        String[] reverse = new String[array.length];
61
+        
62
+        for (int i = 0; i < array.length; i++) {
63
+            reverse[i] = array[array.length-1-i];
64
+        }
65
+        
66
+        return reverse;
54
     }
67
     }
55
 
68
 
56
     /**
69
     /**
58
      * @return true if the order of the array is the same backwards and forwards
71
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
72
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
73
     public static boolean isPalindromic(String[] array) {
61
-        return false;
74
+       return Arrays.deepEquals(array, reverse(array));
62
     }
75
     }
63
 
76
 
64
     /**
77
     /**
66
      * @return true if each letter in the alphabet has been used in the array
79
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
80
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
81
     public static boolean isPangramic(String[] array) {
69
-        return false;
82
+        String[] abc = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
83
+                      "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
84
+                      "w", "x", "y", "z" };
85
+        
86
+        boolean pangramic = true;
87
+                 
88
+        for (String letter : abc) {
89
+            boolean thisLetterFound = false;
90
+            for (String string : array) {
91
+                if (string.toLowerCase().contains(letter)) {
92
+                    thisLetterFound = true;
93
+                    break;
94
+                }
95
+            }
96
+            if (!thisLetterFound) {
97
+                pangramic = false;
98
+                break;
99
+            }
100
+        }
101
+
102
+        return pangramic;
70
     }
103
     }
71
 
104
 
72
     /**
105
     /**
75
      * @return number of occurrences the specified `value` has occurred
108
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
109
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
110
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
111
+        int count = 0;
112
+        for (String string : array){
113
+            if (string.equals(value)) count++;
114
+        }
115
+        return count;
79
     }
116
     }
80
 
117
 
81
     /**
118
     /**
84
      * @return array with identical contents excluding values of `value`
121
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
122
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
123
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
124
+        int numOccurrences = getNumberOfOccurrences(array, valueToRemove);
125
+        String[] arrRemoved;
126
+        if (numOccurrences == 0) {
127
+            arrRemoved = array;
128
+        }
129
+        else {
130
+            arrRemoved = new String[array.length - numOccurrences];
131
+            int removedArrPos = 0;
132
+            for (int i = 0; i < array.length; i++) {
133
+                if (!array[i].equals(valueToRemove)) {
134
+                    arrRemoved[removedArrPos] = array[i];
135
+                    removedArrPos++;
136
+                }
137
+            }
138
+        }
139
+        return arrRemoved;
88
     }
140
     }
89
 
141
 
90
     /**
142
     /**
92
      * @return array of Strings with consecutive duplicates removes
144
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
145
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
146
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
147
+        String prevString = null;
148
+        int consecDupCount = getConsecDupCount(array);
149
+        String[] removedArr = new String[array.length - consecDupCount];
150
+        
151
+        int position = 0;
152
+        for (int i = 0; i < array.length; i++) {
153
+            if (!array[i].equals(prevString)){
154
+                removedArr[position] = array[i];
155
+                prevString = array[i];
156
+                position++;
157
+            }
158
+        }
159
+        
160
+        return removedArr;
96
     }
161
     }
97
 
162
 
98
     /**
163
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
165
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
166
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
167
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
168
+        int consecDupCount = getConsecDupCount(array);
169
+        String[] packedArr = new String[array.length - consecDupCount];;
170
+        
171
+        String prevString = array[0];
172
+        packedArr[0] = array[0];
173
+        int position = 0;
174
+        for (int i = 1; i < array.length; i++) {
175
+            if (array[i].equals(prevString)) {
176
+                packedArr[position] += prevString;
177
+            }
178
+            else {
179
+                position++;
180
+                packedArr[position] = array[i];
181
+                prevString = array[i];
182
+            }
183
+        }
184
+        
185
+        return packedArr;
186
+    }
187
+    
188
+    private static int getConsecDupCount(String[] array) {
189
+        String prevString = null;
190
+        int count = 0;
191
+        
192
+        for (String string : array) {
193
+            if (string.equals(prevString)){
194
+                count++;
195
+            }
196
+            prevString = string;
197
+        }
198
+        
199
+        return count;
104
     }
200
     }
105
-
106
 
201
 
107
 }
202
 }

+ 42
- 0
package.bluej 查看文件

1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=717
6
+editor.fx.0.width=971
7
+editor.fx.0.x=224
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=15
16
+package.editor.y=102
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=140
35
+target1.y=140
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