Jared Norris il y a 8 ans
Parent
révision
d8423c6dbc
3 fichiers modifiés avec 49 ajouts et 22 suppressions
  1. 39
    12
      StringArrayUtils.java
  2. 1
    1
      StringArrayUtilsTest.java
  3. 9
    9
      package.bluej

+ 39
- 12
StringArrayUtils.java Voir le fichier

@@ -42,7 +42,7 @@ 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 Arrays.asList(array).contains(value);   
45
+        return Arrays.asList(array).contains(value);
46 46
     }
47 47
 
48 48
     /**
@@ -71,12 +71,19 @@ public class StringArrayUtils {
71 71
      * @return true if each letter in the alphabet has been used in the array
72 72
      */ // TODO
73 73
     public static boolean isPangramic(String[] array) {
74
-        String[] alpha = "abcdefghijklmnopqrstuvwxyz".split("");
75
-        System.out.println(alpha);
74
+        String[] alpha = {"a","b","c","d","e","f","g","h","i","j",
75
+                          "k","l","m","n","o","p","q","r","s","t",
76
+                          "u","v","w","x","y","z"};
77
+        String result = "";   
78
+        int count = 0;
79
+        for (String e : array) {
80
+            result += e.replace(" ", "");
81
+        }
82
+        String[] check = result.toLowerCase().split("");
76 83
         for (String letter : alpha) {
77
-            if(!contains(array, letter)) return false; 
84
+            if (contains(check, letter)) count++;
78 85
         }
79
-        return true;
86
+        return count == alpha.length;
80 87
     }
81 88
 
82 89
     /**
@@ -98,18 +105,24 @@ public class StringArrayUtils {
98 105
      * @return array with identical contents excluding values of `value`
99 106
      */ // TODO
100 107
     public static String[] removeValue(String[] array, String valueToRemove) {
101
-    String[] result = array.toString()
102
-                           .replaceAll(valueToRemove, "")
103
-                           .split("");
104
-    return result;
108
+    ArrayList<String> result = new ArrayList<>();
109
+    for (String s : array) {
110
+        if (!s.equals(valueToRemove)) result.add(s);
111
+    }    
112
+    return result.toArray(new String[result.size()]);
105 113
     }
106 114
 
107 115
     /**
108 116
      * @param array array of chars
109 117
      * @return array of Strings with consecutive duplicates removes
110 118
      */ // TODO
111
-    public static String[] removeConsecutiveDuplicates(String[] array) {
112
-        return null;
119
+    public static String[] removeConsecutiveDuplicates(String[] array) {        
120
+        ArrayList<String> result = new ArrayList<>();
121
+        for (int i = 0; i < array.length - 1; i++) {
122
+            if (!array[i].equals(array[i + 1])) result.add(array[i]); 
123
+        }
124
+        result.add(array[array.length - 1]);
125
+        return result.toArray(new String[result.size()]);
113 126
     }
114 127
 
115 128
     /**
@@ -117,7 +130,21 @@ public class StringArrayUtils {
117 130
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
118 131
      */ // TODO
119 132
     public static String[] packConsecutiveDuplicates(String[] array) {
120
-        return null;
133
+        ArrayList<String> result = new ArrayList<>();
134
+        StringBuilder addMe = new StringBuilder();
135
+        for (int i = 0; i < array.length - 1; i++) {          
136
+            if (!array[i].equals(array[i + 1])) {
137
+                addMe.append(array[i]);
138
+                result.add(addMe.toString());
139
+                addMe = new StringBuilder();
140
+            }
141
+            else {
142
+                addMe.append(array[i]);
143
+            }
144
+        }
145
+        addMe.append(array[array.length - 1]);
146
+        result.add(addMe.toString());
147
+        return result.toArray(new String[result.size()]);
121 148
     }
122 149
 
123 150
 

+ 1
- 1
StringArrayUtilsTest.java Voir le fichier

@@ -179,7 +179,7 @@ public class StringArrayUtilsTest {
179 179
     public void testContainsTwo() {
180 180
        String[] array = {"Five quacking", "zephyrs", "jolt my", "wax bed"};
181 181
        for (String s : array) {
182
-           boolean outcome = StringArrayUtils.contains(array, "a");
182
+           boolean outcome = StringArrayUtils.contains(array, s);
183 183
            Assert.assertTrue(outcome);
184 184
        }
185 185
         

+ 9
- 9
package.bluej Voir le fichier

@@ -2,18 +2,18 @@
2 2
 dependency1.from=StringArrayUtilsTest
3 3
 dependency1.to=StringArrayUtils
4 4
 dependency1.type=UsesDependency
5
-editor.fx.0.height=722
6
-editor.fx.0.width=800
7
-editor.fx.0.x=240
8
-editor.fx.0.y=24
9
-objectbench.height=101
5
+editor.fx.0.height=712
6
+editor.fx.0.width=910
7
+editor.fx.0.x=143
8
+editor.fx.0.y=59
9
+objectbench.height=164
10 10
 objectbench.width=776
11 11
 package.divider.horizontal=0.6
12
-package.divider.vertical=0.8007380073800738
13
-package.editor.height=427
12
+package.divider.vertical=0.6845018450184502
13
+package.editor.height=364
14 14
 package.editor.width=674
15
-package.editor.x=254
16
-package.editor.y=74
15
+package.editor.x=112
16
+package.editor.y=174
17 17
 package.frame.height=600
18 18
 package.frame.width=800
19 19
 package.numDependencies=1