Clement Ojie 8 gadus atpakaļ
vecāks
revīzija
c007a5bee9
3 mainītis faili ar 194 papildinājumiem un 15 dzēšanām
  1. 151
    14
      StringArrayUtils.java
  2. 1
    1
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 151
- 14
StringArrayUtils.java Parādīt failu

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
+        String result = "";
12
+
13
+        result += array[0];
14
+
15
+        return result ;
13
     }
16
     }
14
 
17
 
15
     /**
18
     /**
17
      * @return second element in specified array
20
      * @return second element in specified array
18
      */
21
      */
19
     public static String getSecondElement(String[] array) {
22
     public static String getSecondElement(String[] array) {
20
-        return null;
23
+        String result = "";
24
+
25
+        result += array[1];
26
+
27
+        return result ;
28
+
21
     }
29
     }
22
 
30
 
23
     /**
31
     /**
25
      * @return last element in specified array
33
      * @return last element in specified array
26
      */ // TODO
34
      */ // TODO
27
     public static String getLastElement(String[] array) {
35
     public static String getLastElement(String[] array) {
28
-        return null;
36
+        String result = "";
37
+
38
+        return array[array.length-1] ;
29
     }
39
     }
30
 
40
 
31
     /**
41
     /**
33
      * @return second to last element in specified array
43
      * @return second to last element in specified array
34
      */ // TODO
44
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
45
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
46
+        String result = "";
47
+
48
+        return array[array.length-2];
37
     }
49
     }
38
 
50
 
39
     /**
51
     /**
42
      * @return true if the array contains the specified `value`
54
      * @return true if the array contains the specified `value`
43
      */ // TODO
55
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
56
     public static boolean contains(String[] array, String value) {
45
-        return false;
57
+        boolean x = false;
58
+        for (String i : array){
59
+
60
+            if (i == value){
61
+
62
+                x = true; 
63
+
64
+            }
65
+        }
66
+
67
+        return x;
46
     }
68
     }
47
 
69
 
48
     /**
70
     /**
50
      * @return an array with identical contents in reverse order
72
      * @return an array with identical contents in reverse order
51
      */ // TODO
73
      */ // TODO
52
     public static String[] reverse(String[] array) {
74
     public static String[] reverse(String[] array) {
53
-        return null;
75
+        String order;
76
+
77
+        for (int x = 0; x < array.length/2; x++){
78
+            order = array[x];
79
+
80
+            array[x] = array[array.length -x -1];
81
+            array[array.length -x -1] = order;
82
+
83
+        }
84
+        return array;
54
     }
85
     }
55
 
86
 
56
     /**
87
     /**
58
      * @return true if the order of the array is the same backwards and forwards
89
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
90
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
91
     public static boolean isPalindromic(String[] array) {
61
-        return false;
92
+
93
+        boolean rev = false;
94
+        for (int x = 0; x < array.length/2; x++){
95
+
96
+            if (array[x] == array[array.length -x -1]){
97
+
98
+                rev = true;
99
+
100
+            }
101
+
102
+        }
103
+        return rev;
62
     }
104
     }
63
 
105
 
64
     /**
106
     /**
66
      * @return true if each letter in the alphabet has been used in the array
108
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
109
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
110
     public static boolean isPangramic(String[] array) {
69
-        return false;
111
+        boolean every = false;
112
+        StringBuilder build = new StringBuilder();
113
+
114
+        for(String s: array){
115
+            build.append(s);
116
+        }
117
+        String ofArr = "";
118
+        ofArr = build.toString();
119
+        for (char x = 'A'; x < 'Z'; x++){
120
+            if ((ofArr.indexOf(x) <0) && (ofArr.indexOf((char) (x + 32)) <0)){
121
+                every =false;
122
+            }
123
+            else{  every = true;
124
+
125
+            }
126
+
127
+        }
128
+        return every;
129
+
70
     }
130
     }
71
 
131
 
72
     /**
132
     /**
75
      * @return number of occurrences the specified `value` has occurred
135
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
136
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
137
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
138
+
139
+        int occur = 0;
140
+
141
+        for (int x = 0; x < array.length; x++){
142
+
143
+            if (value.equals(array[x])){
144
+                occur++;
145
+            }
146
+
147
+        }
148
+
149
+        return occur;
79
     }
150
     }
80
 
151
 
81
     /**
152
     /**
84
      * @return array with identical contents excluding values of `value`
155
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
156
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
157
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
158
+
159
+        int place = 0;
160
+        int count = getNumberOfOccurrences(array , valueToRemove);
161
+        String[] newArr = new String [array.length - count];
162
+
163
+        for (int x = 0; x < array.length; x++){
164
+            if (array[x] != valueToRemove){
165
+                newArr[place] = array[x];
166
+
167
+                place++;
168
+            }
169
+
170
+        }
171
+
172
+        return newArr;
88
     }
173
     }
89
 
174
 
90
     /**
175
     /**
92
      * @return array of Strings with consecutive duplicates removes
177
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
178
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
179
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
180
+        int newArrIndex= 0;
181
+
182
+        String[] newArr = new String[array.length];
183
+        newArr[newArrIndex] = array[0];
184
+        newArrIndex++;
185
+
186
+        for (int x = 1; x < array.length; x++){
187
+            if (!array[x].equals(newArr[newArrIndex - 1])){
188
+
189
+                newArr[newArrIndex]= array[x]; 
190
+
191
+                newArrIndex++;
192
+            }
193
+
194
+        }
195
+        String[] newArr2 = new String [newArrIndex];
196
+        System.arraycopy(newArr, 0 , newArr2,  0, newArrIndex);
197
+
198
+        return newArr2;
199
+
200
+        
96
     }
201
     }
97
 
202
 
98
     /**
203
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
205
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
206
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
207
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
208
+        StringBuilder dups = new StringBuilder(array[0]);
209
+
210
+        String[] newArr = new String[array.length-count(array)];
211
+        int adding = 0;
212
+
213
+        for (int x = 1; x < array.length; x++){
214
+            if (array[x-1].equals(array[x])){
215
+                dups.append(array[x]);
216
+            }
217
+            else {
218
+                newArr[adding] = dups.toString();
219
+                dups.setLength(0);
220
+                dups.append(array[x]);
221
+                adding++;
222
+
223
+            }
224
+        }
225
+
226
+        newArr[adding] = dups.toString();
227
+        return newArr;
104
     }
228
     }
105
 
229
 
230
+    public static int count(String[] array){
231
+        int count = 0;
232
+
233
+        for (int x = 1; x < array.length; x++){
234
+            if (array[x-1].equals(array[x])){
235
+
236
+                count++;    
237
+            }
238
+
239
+        }
240
+        
241
+        return count;
242
+    }
106
 
243
 
107
 }
244
 }

+ 1
- 1
StringArrayUtilsTest.java Parādīt failu

329
 
329
 
330
 
330
 
331
 
331
 
332
-
332
+   
333
 
333
 
334
 
334
 
335
     @Test
335
     @Test

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

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=375
8
+editor.fx.0.y=23
9
+objectbench.height=164
10
+objectbench.width=776
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.6845018450184502
13
+package.editor.height=364
14
+package.editor.width=674
15
+package.editor.x=154
16
+package.editor.y=51
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