Eric Foster 6 gadus atpakaļ
vecāks
revīzija
00f36a62c1
2 mainītis faili ar 111 papildinājumiem un 13 dzēšanām
  1. 69
    13
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 69
- 13
StringArrayUtils.java Parādīt failu

@@ -1,4 +1,6 @@
1
- 
1
+import java.util.Arrays;
2
+import java.io.*;
3
+import java.util.*; 
2 4
 
3 5
 /**
4 6
  * Created by leon on 1/29/18.
@@ -9,7 +11,7 @@ public class StringArrayUtils {
9 11
      * @return first element of specified array
10 12
      */ // TODO
11 13
     public static String getFirstElement(String[] array) {
12
-        return null;
14
+        return array[0];
13 15
     }
14 16
 
15 17
     /**
@@ -17,7 +19,7 @@ public class StringArrayUtils {
17 19
      * @return second element in specified array
18 20
      */
19 21
     public static String getSecondElement(String[] array) {
20
-        return null;
22
+        return array[1];
21 23
     }
22 24
 
23 25
     /**
@@ -25,7 +27,7 @@ public class StringArrayUtils {
25 27
      * @return last element in specified array
26 28
      */ // TODO
27 29
     public static String getLastElement(String[] array) {
28
-        return null;
30
+        return array[array.length - 1];
29 31
     }
30 32
 
31 33
     /**
@@ -33,7 +35,7 @@ public class StringArrayUtils {
33 35
      * @return second to last element in specified array
34 36
      */ // TODO
35 37
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
38
+        return array[array.length - 2];
37 39
     }
38 40
 
39 41
     /**
@@ -42,7 +44,13 @@ public class StringArrayUtils {
42 44
      * @return true if the array contains the specified `value`
43 45
      */ // TODO
44 46
     public static boolean contains(String[] array, String value) {
45
-        return false;
47
+        boolean contains = false;
48
+        for(String element : array){
49
+            if(element.equals(value)){
50
+                contains = true;
51
+            }
52
+        }
53
+        return contains;
46 54
     }
47 55
 
48 56
     /**
@@ -50,7 +58,11 @@ public class StringArrayUtils {
50 58
      * @return an array with identical contents in reverse order
51 59
      */ // TODO
52 60
     public static String[] reverse(String[] array) {
53
-        return null;
61
+        String[] reverse = new String[array.length];
62
+        for (int j=array.length-1, i=0; j>=0; j--, i++){    
63
+            reverse[i] = array[j];
64
+        }
65
+        return reverse;
54 66
     }
55 67
 
56 68
     /**
@@ -58,7 +70,12 @@ public class StringArrayUtils {
58 70
      * @return true if the order of the array is the same backwards and forwards
59 71
      */ // TODO
60 72
     public static boolean isPalindromic(String[] array) {
61
-        return false;
73
+        boolean isPalindromic = false;
74
+        String[] reverse = reverse(array);
75
+        if (Arrays.equals(array,reverse)){
76
+            isPalindromic = true;
77
+        }
78
+        return isPalindromic;
62 79
     }
63 80
 
64 81
     /**
@@ -66,7 +83,19 @@ public class StringArrayUtils {
66 83
      * @return true if each letter in the alphabet has been used in the array
67 84
      */ // TODO
68 85
     public static boolean isPangramic(String[] array) {
69
-        return false;
86
+        String arrayString = Arrays.toString(array).toLowerCase();
87
+        
88
+        if(arrayString.length() < 26){
89
+            return false;
90
+        }
91
+
92
+        for (char c = 'a'; c <='z'; c++){
93
+            if(arrayString.indexOf(c) < 0){
94
+                return false;
95
+            }
96
+        }
97
+        
98
+        return true;
70 99
     }
71 100
 
72 101
     /**
@@ -75,7 +104,15 @@ public class StringArrayUtils {
75 104
      * @return number of occurrences the specified `value` has occurred
76 105
      */ // TODO
77 106
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
107
+        int numberOfOccurrences = 0;
108
+
109
+        for (String element : array){
110
+            if(element.equals(value)){
111
+                numberOfOccurrences += 1;
112
+            }
113
+        }
114
+        
115
+        return numberOfOccurrences;
79 116
     }
80 117
 
81 118
     /**
@@ -84,7 +121,27 @@ public class StringArrayUtils {
84 121
      * @return array with identical contents excluding values of `value`
85 122
      */ // TODO
86 123
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
124
+        int numberOfOccurrences = getNumberOfOccurrences(array, valueToRemove);
125
+        String[] newArray = new String[array.length - numberOfOccurrences];
126
+        
127
+        int i = 0;
128
+        for(String element : array){
129
+            if(!element.equals(valueToRemove)){
130
+                newArray[i] = element;
131
+                i++;
132
+            }
133
+        }
134
+        return newArray;
135
+    }
136
+    
137
+    public static int numberOfConsecutiveDuplicates(String[] array) {
138
+        int numberOfConsecDups = 0;
139
+        for(int i=1; i<=array.length-1; i++){
140
+            if(array[i].equals(array[i-1])){
141
+                numberOfConsecDups += 1;
142
+            }
143
+        }
144
+        return numberOfConsecDups;
88 145
     }
89 146
 
90 147
     /**
@@ -92,7 +149,7 @@ public class StringArrayUtils {
92 149
      * @return array of Strings with consecutive duplicates removes
93 150
      */ // TODO
94 151
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
152
+        
96 153
     }
97 154
 
98 155
     /**
@@ -103,5 +160,4 @@ public class StringArrayUtils {
103 160
         return null;
104 161
     }
105 162
 
106
-
107 163
 }

+ 42
- 0
package.bluej Parādīt failu

@@ -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=713
6
+editor.fx.0.width=800
7
+editor.fx.0.x=466
8
+editor.fx.0.y=35
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=202
16
+package.editor.y=149
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