Ahmad Rusdi před 8 roky
rodič
revize
3bcdb14158
2 změnil soubory, kde provedl 161 přidání a 16 odebrání
  1. 119
    16
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 119
- 16
StringArrayUtils.java Zobrazit soubor

1
- 
2
 
1
 
2
+import java.util.*;
3
 /**
3
 /**
4
  * Created by leon on 1/29/18.
4
  * Created by leon on 1/29/18.
5
  */
5
  */
9
      * @return first element of specified array
9
      * @return first element of specified array
10
      */ // TODO
10
      */ // TODO
11
     public static String getFirstElement(String[] array) {
11
     public static String getFirstElement(String[] array) {
12
-        return null;
12
+        return array[0];
13
     }
13
     }
14
 
14
 
15
     /**
15
     /**
17
      * @return second element in specified array
17
      * @return second element in specified array
18
      */
18
      */
19
     public static String getSecondElement(String[] array) {
19
     public static String getSecondElement(String[] array) {
20
-        return null;
20
+        return array[1];
21
     }
21
     }
22
 
22
 
23
     /**
23
     /**
25
      * @return last element in specified array
25
      * @return last element in specified array
26
      */ // TODO
26
      */ // TODO
27
     public static String getLastElement(String[] array) {
27
     public static String getLastElement(String[] array) {
28
-        return null;
28
+        return array[array.length - 1];
29
     }
29
     }
30
 
30
 
31
     /**
31
     /**
33
      * @return second to last element in specified array
33
      * @return second to last element in specified array
34
      */ // TODO
34
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
35
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
36
+        return array[array.length - 2];
37
     }
37
     }
38
 
38
 
39
-    /**
39
+    /*
40
      * @param array array of String objects
40
      * @param array array of String objects
41
      * @param value value to check array for
41
      * @param value value to check array for
42
      * @return true if the array contains the specified `value`
42
      * @return true if the array contains the specified `value`
43
      */ // TODO
43
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
44
     public static boolean contains(String[] array, String value) {
45
-        return false;
45
+        boolean result = false;
46
+        for (String val : array) {
47
+            if (val.equals(value)) result = true;
48
+        }
49
+        return result;
46
     }
50
     }
47
 
51
 
48
     /**
52
     /**
50
      * @return an array with identical contents in reverse order
54
      * @return an array with identical contents in reverse order
51
      */ // TODO
55
      */ // TODO
52
     public static String[] reverse(String[] array) {
56
     public static String[] reverse(String[] array) {
53
-        return null;
57
+        String[] reverseArray = new String[array.length];
58
+        int j = 0;
59
+        for (int i = array.length - 1; i >= 0; i--) {
60
+            reverseArray[j] = array[i];
61
+            j++;
62
+        }
63
+        return reverseArray;
54
     }
64
     }
55
 
65
 
56
     /**
66
     /**
58
      * @return true if the order of the array is the same backwards and forwards
68
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
69
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
70
     public static boolean isPalindromic(String[] array) {
61
-        return false;
71
+        boolean result = true;
72
+        String[] revArr = new String[array.length];
73
+
74
+        int j = 0;
75
+        for (int i = array.length - 1; i >= 0; i--)  {
76
+            revArr[j] = array[i];
77
+            j++;
78
+        }
79
+
80
+        for (int i = 0; i < array.length - 1; i++) {
81
+            if (!(array[i].equals(revArr[i]))) result = false;
82
+        }
83
+
84
+        return result;
62
     }
85
     }
63
 
86
 
64
     /**
87
     /**
66
      * @return true if each letter in the alphabet has been used in the array
89
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
90
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
91
     public static boolean isPangramic(String[] array) {
69
-        return false;
92
+        String alpha = "abcdefghijklmnopqrstuvwxyz";
93
+
94
+        for (int i = 0; i < array.length; i++) {
95
+            String word = array[i];
96
+            for (int j = 0; j < word.length(); j++) {
97
+                String letter = Character.toString(word.charAt(j)).toLowerCase();
98
+                if (alpha.indexOf(letter) != -1) {
99
+                    alpha = alpha.replace(letter, "");
100
+                }
101
+            }
102
+        }
103
+
104
+        boolean result = alpha.length() <= 0;
105
+        return result;
70
     }
106
     }
71
 
107
 
72
     /**
108
     /**
75
      * @return number of occurrences the specified `value` has occurred
111
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
112
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
113
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
114
+        int result = 0;
115
+        for (String val : array) {
116
+            if (val.equals(value)) result += 1;
117
+        }
118
+        return result;
79
     }
119
     }
80
 
120
 
81
     /**
121
     /**
84
      * @return array with identical contents excluding values of `value`
124
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
125
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
126
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
127
+        int count = 0;
128
+        for (int i = 0; i < array.length; i++) {
129
+            if(array[i].equals(valueToRemove)) count++;
130
+        }
131
+
132
+        String[] newArr = new String[array.length - count];
133
+
134
+        int j = 0;
135
+        for (int i = 0; i < array.length; i++) {
136
+            if (!array[i].equals(valueToRemove)) {
137
+                newArr[j] = array[i];
138
+                j++;
139
+            }
140
+        }
141
+
142
+        return newArr;
88
     }
143
     }
89
 
144
 
90
     /**
145
     /**
92
      * @return array of Strings with consecutive duplicates removes
147
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
148
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
149
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
150
+        int count = 0;
151
+        for (int i = 0; i < array.length - 1; i++) {
152
+            if (array[i].equals(array[i + 1])) count++;
153
+        }
154
+
155
+        String[] newArr = new String[array.length - count];
156
+        newArr[newArr.length - 1] = array[array.length - 1];
157
+        int j = 0;
158
+
159
+        for (int i = 0; i < array.length - 1; i++) {
160
+            if (!array[i].equals(array[i + 1])) {
161
+                newArr[j] = array[i];
162
+                j++;
163
+            }
164
+        }
165
+
166
+        return newArr;
96
     }
167
     }
97
 
168
 
98
     /**
169
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
171
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
172
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
173
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
174
+        int count = 1;
105
 
175
 
176
+        for (int i = 0; i < array.length - 1; i++) {
177
+            System.out.println(!array[i].equals(array[i + 1]));
178
+            if (!array[i].equals(array[i + 1])) count++;
179
+        }
106
 
180
 
107
-}
181
+        String[] newArr = new String[count];
182
+
183
+        StringBuilder temp = new StringBuilder();
184
+        for (int i = array.length - 1; i >= 0; i--) {
185
+            if (array[i].equals(array[i - 1])) {
186
+                temp.append(array[i]);
187
+            } else {
188
+                temp.append(array[i]);
189
+                newArr[newArr.length - 1] = temp.toString();
190
+                temp = new StringBuilder();
191
+                break;
192
+            }
193
+        }
194
+
195
+        int j = 0;
196
+        for (int i = 0; i < array.length - 1; i++) {
197
+            if (array[i].equals(array[i + 1])) {
198
+                temp.append(array[i]);
199
+            } else {
200
+                temp.append(array[i]);
201
+                newArr[j] = temp.toString();
202
+                temp = new StringBuilder();
203
+                j++;
204
+            }
205
+        }
206
+
207
+        System.out.println(Arrays.toString(newArr));
208
+        return newArr;
209
+    }
210
+}

+ 42
- 0
package.bluej Zobrazit soubor

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=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=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=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