Rachelle il y a 8 ans
Parent
révision
b908a2c4f4
3 fichiers modifiés avec 158 ajouts et 26 suppressions
  1. 90
    23
      StringArrayUtils.java
  2. 26
    3
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 90
- 23
StringArrayUtils.java Voir le fichier

1
- 
2
-
1
+import java.util.Arrays;
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 containsString = false;
45
+        for (String element : array)
46
+            if(element.equals(value)){
47
+                containsString = true;  
48
+                break;
49
+            }
50
+        return containsString;
46
     }
51
     }
47
 
52
 
48
     /**
53
     /**
50
      * @return an array with identical contents in reverse order
55
      * @return an array with identical contents in reverse order
51
      */ // TODO
56
      */ // TODO
52
     public static String[] reverse(String[] array) {
57
     public static String[] reverse(String[] array) {
53
-        return null;
58
+        String[] reverseString = new String[array.length];
59
+        for(int i = 0; i < array.length; i++){
60
+            reverseString [array.length-1-i] = array[i];
61
+        }
62
+        return reverseString;
54
     }
63
     }
55
 
64
 
56
     /**
65
     /**
58
      * @return true if the order of the array is the same backwards and forwards
67
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
68
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
69
     public static boolean isPalindromic(String[] array) {
61
-        return false;
70
+        String[] reverseString = reverse(array);
71
+        boolean isTrue = false;
72
+        if(Arrays.equals(reverseString, array)){
73
+            return true;
74
+        }
75
+        return isTrue;
62
     }
76
     }
63
 
77
 
64
     /**
78
     /**
65
      * @param array array of String objects
79
      * @param array array of String objects
66
      * @return true if each letter in the alphabet has been used in the array
80
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
81
      */ // TODO
68
-    public static boolean isPangramic(String[] array) {
69
-        return false;
82
+
83
+    public static boolean isPangramic(String[] array){
84
+        char [] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
85
+        boolean containsEveryLetter = true;
86
+        for(char letter:alphabet){
87
+            if(arrayContainsLetter(array,letter)==false){
88
+                containsEveryLetter = false;
89
+                break;
90
+            }
91
+        }
92
+        return containsEveryLetter;
93
+    }
94
+
95
+    public static boolean arrayContainsLetter(String[] array, char letter){
96
+        boolean containsLetter = false;
97
+        for(String string : array){
98
+            if(stringContainsLetter(string.toLowerCase(),letter)){
99
+                containsLetter = true;
100
+                break;
101
+            }
102
+        }
103
+        return containsLetter;
104
+    }
105
+
106
+    public static boolean stringContainsLetter(String string, char letter){
107
+        boolean containsLetter = false;
108
+        for(int i = 0; i < string.length(); i++){
109
+            if (string.charAt(i)==letter){
110
+                containsLetter = true;
111
+                break;
112
+            }
113
+        }
114
+        return containsLetter;
70
     }
115
     }
71
 
116
 
72
     /**
117
     /**
75
      * @return number of occurrences the specified `value` has occurred
120
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
121
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
122
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
123
+        int counter = 0;
124
+        for (String element : array){
125
+            if(element.equals(value)){
126
+                counter += 1;
127
+            }
128
+        }
129
+        return counter;
79
     }
130
     }
80
 
131
 
81
     /**
132
     /**
82
-     * @param array         array of String objects
133
+     * @param array array of String objects
83
      * @param valueToRemove value to remove from array
134
      * @param valueToRemove value to remove from array
84
      * @return array with identical contents excluding values of `value`
135
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
136
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
137
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
138
+        int lengthOfNewArray = array.length-getNumberOfOccurrences(array,valueToRemove);
139
+        String[] newArray = new String[lengthOfNewArray];
140
+        int index = 0;
141
+        for(String element : array){           
142
+            if(element != valueToRemove){
143
+                newArray[index] = element;
144
+                index += 1;
145
+            }
146
+        }
147
+        return newArray;
88
     }
148
     }
89
 
149
 
90
     /**
150
     /**
92
      * @return array of Strings with consecutive duplicates removes
152
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
153
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
154
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
96
-    }
97
-
98
-    /**
99
-     * @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
101
-     */ // TODO
102
-    public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
155
+        String[] sortedArray = array.sort();
156
+        
157
+        for(int i = 1; i < sortedArray.length; i++){
158
+            if(sortedArray[i]!=sortedArray[i-1]){
159
+                    add to array list
160
+            }
161
+        }
104
     }
162
     }
163
+    return null;
164
+}
105
 
165
 
166
+/**
167
+ * @param array array of chars
168
+ * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
169
+ */ // TODO
170
+public static String[] packConsecutiveDuplicates(String[] array) {
171
+return null;
172
+}
106
 
173
 
107
 }
174
 }

+ 26
- 3
StringArrayUtilsTest.java Voir le fichier

164
 
164
 
165
 
165
 
166
 
166
 
167
-
167
+    
168
 
168
 
169
 
169
 
170
     @Test
170
     @Test
278
 
278
 
279
     @Test
279
     @Test
280
     public void testIsPangramic4() {
280
     public void testIsPangramic4() {
281
-        String[] array = {"a", "b", "c", "d"};
281
+        String[] array = {"aa", "bb", "cc", "dd"};
282
         boolean outcome = StringArrayUtils.isPangramic(array);
282
         boolean outcome = StringArrayUtils.isPangramic(array);
283
         Assert.assertFalse(outcome);
283
         Assert.assertFalse(outcome);
284
     }
284
     }
285
 
285
 
286
+    @Test
287
+    public void teststringContainsLetter(){
288
+        String string = "A fox";
289
+        boolean outcome = StringArrayUtils.stringContainsLetter(string,'e');
290
+        Assert.assertFalse(outcome);
291
+    }
286
 
292
 
293
+    @Test
294
+    public void teststringContainsLetter2(){
295
+        String string = "A fox";
296
+        boolean outcome = StringArrayUtils.stringContainsLetter(string,'o');
297
+        Assert.assertTrue(outcome);
298
+    }
287
 
299
 
300
+    @Test
301
+    public void arrayContainsLetter(){
302
+        String[] string = {"A fox","bunny"};
303
+        boolean outcome = StringArrayUtils.arrayContainsLetter(string,'e');
304
+        Assert.assertFalse(outcome);
305
+    }
288
 
306
 
289
-
307
+    @Test
308
+    public void arrayContainsLetter2(){
309
+        String[] string = {"A fox","bunny"};
310
+        boolean outcome = StringArrayUtils.arrayContainsLetter(string,'o');
311
+        Assert.assertTrue(outcome);
312
+    }
290
 
313
 
291
 
314
 
292
 
315
 

+ 42
- 0
package.bluej Voir le fichier

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=0
16
+package.editor.y=23
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=ClassTarget
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