Jared Norris 8 лет назад
Родитель
Сommit
4ce792220e
3 измененных файлов: 79 добавлений и 11 удалений
  1. 28
    11
      StringArrayUtils.java
  2. 9
    0
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 28
- 11
StringArrayUtils.java Просмотреть файл

@@ -1,5 +1,5 @@
1 1
  
2
-
2
+import java.util.*;
3 3
 /**
4 4
  * Created by leon on 1/29/18.
5 5
  */
@@ -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,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 false;
45
+        return Arrays.asList(array).contains(value);   
46 46
     }
47 47
 
48 48
     /**
@@ -50,7 +50,12 @@ public class StringArrayUtils {
50 50
      * @return an array with identical contents in reverse order
51 51
      */ // TODO
52 52
     public static String[] reverse(String[] array) {
53
-        return null;
53
+        int length = array.length;
54
+        String[] result = new String[length];
55
+        for (int i = 0; i < length; i++) {
56
+            result[i] = array[(length - 1) - i];
57
+        }
58
+        return result;   
54 59
     }
55 60
 
56 61
     /**
@@ -58,7 +63,7 @@ public class StringArrayUtils {
58 63
      * @return true if the order of the array is the same backwards and forwards
59 64
      */ // TODO
60 65
     public static boolean isPalindromic(String[] array) {
61
-        return false;
66
+        return Arrays.equals(array, reverse(array));
62 67
     }
63 68
 
64 69
     /**
@@ -66,7 +71,12 @@ public class StringArrayUtils {
66 71
      * @return true if each letter in the alphabet has been used in the array
67 72
      */ // TODO
68 73
     public static boolean isPangramic(String[] array) {
69
-        return false;
74
+        String[] alpha = "abcdefghijklmnopqrstuvwxyz".split("");
75
+        System.out.println(alpha);
76
+        for (String letter : alpha) {
77
+            if(!contains(array, letter)) return false; 
78
+        }
79
+        return true;
70 80
     }
71 81
 
72 82
     /**
@@ -75,7 +85,11 @@ public class StringArrayUtils {
75 85
      * @return number of occurrences the specified `value` has occurred
76 86
      */ // TODO
77 87
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
88
+        int count = 0;
89
+        for (String i : array) {
90
+            count += (i == value) ? 1 : 0;    
91
+        }
92
+        return count;
79 93
     }
80 94
 
81 95
     /**
@@ -84,7 +98,10 @@ public class StringArrayUtils {
84 98
      * @return array with identical contents excluding values of `value`
85 99
      */ // TODO
86 100
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
101
+    String[] result = array.toString()
102
+                           .replaceAll(valueToRemove, "")
103
+                           .split("");
104
+    return result;
88 105
     }
89 106
 
90 107
     /**

+ 9
- 0
StringArrayUtilsTest.java Просмотреть файл

@@ -175,6 +175,15 @@ public class StringArrayUtilsTest {
175 175
             Assert.assertTrue(outcome);
176 176
         }
177 177
     }
178
+    @Test 
179
+    public void testContainsTwo() {
180
+       String[] array = {"Five quacking", "zephyrs", "jolt my", "wax bed"};
181
+       for (String s : array) {
182
+           boolean outcome = StringArrayUtils.contains(array, "a");
183
+           Assert.assertTrue(outcome);
184
+       }
185
+        
186
+    }
178 187
 
179 188
 
180 189
 

+ 42
- 0
package.bluej Просмотреть файл

@@ -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=722
6
+editor.fx.0.width=800
7
+editor.fx.0.x=240
8
+editor.fx.0.y=24
9
+objectbench.height=101
10
+objectbench.width=776
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=254
16
+package.editor.y=74
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