Browse Source

Working on methods

Trinh Tong 8 years ago
parent
commit
ccfb5c3892
1 changed files with 96 additions and 11 deletions
  1. 96
    11
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 96
- 11
src/main/java/com/zipcodewilmington/StringArrayUtils.java View File

1
 package com.zipcodewilmington;
1
 package com.zipcodewilmington;
2
 
2
 
3
+import com.sun.deploy.util.ArrayUtil;
4
+
5
+import java.lang.reflect.Array;
6
+import java.util.ArrayList;
7
+import java.util.Arrays;
8
+import java.util.Collections;
9
+
3
 /**
10
 /**
4
  * Created by leon on 1/29/18.
11
  * Created by leon on 1/29/18.
5
  */
12
  */
9
      * @return first element of specified array
16
      * @return first element of specified array
10
      */ // TODO
17
      */ // TODO
11
     public static String getFirstElement(String[] array) {
18
     public static String getFirstElement(String[] array) {
12
-        return null;
19
+
20
+        return array[0];
13
     }
21
     }
14
 
22
 
15
     /**
23
     /**
17
      * @return second element in specified array
25
      * @return second element in specified array
18
      */
26
      */
19
     public static String getSecondElement(String[] array) {
27
     public static String getSecondElement(String[] array) {
20
-        return null;
28
+
29
+        return array[1];
21
     }
30
     }
22
 
31
 
23
     /**
32
     /**
25
      * @return last element in specified array
34
      * @return last element in specified array
26
      */ // TODO
35
      */ // TODO
27
     public static String getLastElement(String[] array) {
36
     public static String getLastElement(String[] array) {
28
-        return null;
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
+
47
+        return array[array.length - 2];
37
     }
48
     }
38
 
49
 
39
     /**
50
     /**
42
      * @return true if the array contains the specified `value`
53
      * @return true if the array contains the specified `value`
43
      */ // TODO
54
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
55
     public static boolean contains(String[] array, String value) {
45
-        return false;
56
+        boolean checker = false;
57
+
58
+        for (String word: array) {
59
+            if (word.equals(value)) {
60
+                checker = true;
61
+                break;
62
+            }
63
+        }
64
+
65
+        return checker;
46
     }
66
     }
47
 
67
 
48
     /**
68
     /**
50
      * @return an array with identical contents in reverse order
70
      * @return an array with identical contents in reverse order
51
      */ // TODO
71
      */ // TODO
52
     public static String[] reverse(String[] array) {
72
     public static String[] reverse(String[] array) {
53
-        return null;
73
+
74
+        String[] reversedString = new String[array.length];
75
+
76
+        for (int i = array.length - 1, j = 0; i >= 0; i--, j++) {
77
+            reversedString[j] = array[i];
78
+        }
79
+        return reversedString;
54
     }
80
     }
55
 
81
 
56
     /**
82
     /**
58
      * @return true if the order of the array is the same backwards and forwards
84
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
85
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
86
     public static boolean isPalindromic(String[] array) {
61
-        return false;
87
+        boolean palindromToken = false;
88
+        String[] reversedArray = StringArrayUtils.reverse(array);
89
+
90
+        for (int i = 0; i < array.length; i++) {
91
+            if (reversedArray[i].equals(array[i]) == true) {
92
+                palindromToken = true;
93
+            }
94
+        }
95
+        return palindromToken;
62
     }
96
     }
63
 
97
 
64
     /**
98
     /**
66
      * @return true if each letter in the alphabet has been used in the array
100
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
101
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
102
     public static boolean isPangramic(String[] array) {
69
-        return false;
103
+
104
+        boolean finderToken = false;
105
+
106
+        StringBuilder stringMaker = new StringBuilder();
107
+        for (String word: array) {
108
+            stringMaker.append(word);
109
+        }
110
+
111
+        String fullArrayString = stringMaker.toString().toLowerCase();
112
+
113
+        for (char find = 'a'; find <= 'z'; find++) {
114
+            if (fullArrayString.indexOf(find) == -1) {
115
+                finderToken = false;
116
+            } else {
117
+                finderToken = true;
118
+            }
119
+        }
120
+        return finderToken;
70
     }
121
     }
71
 
122
 
72
     /**
123
     /**
75
      * @return number of occurrences the specified `value` has occurred
126
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
127
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
128
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
129
+        int counter = 0;
130
+        for (String word: array) {
131
+            if (word.equals(value) == true) {
132
+                counter += 1;
133
+            }
134
+        }
135
+        return counter;
79
     }
136
     }
80
 
137
 
81
     /**
138
     /**
84
      * @return array with identical contents excluding values of `value`
141
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
142
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
143
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
144
+
145
+        ArrayList<String> removedWordsArrayList = new ArrayList<String>(Arrays.asList(array));
146
+
147
+        for (int i = 0; i < removedWordsArrayList.size(); i++) {
148
+            if (removedWordsArrayList.get(i) == valueToRemove ) {
149
+                removedWordsArrayList.remove(i);
150
+            }
151
+        }
152
+
153
+        String[] newArray = new String[removedWordsArrayList.size()];
154
+
155
+        newArray = removedWordsArrayList.toArray(newArray);
156
+
157
+        return newArray;
88
     }
158
     }
89
 
159
 
90
     /**
160
     /**
92
      * @return array of Strings with consecutive duplicates removes
162
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
163
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
164
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
165
+
166
+        String[] newArray = new String[array.length];
167
+        String compare = "";
168
+        int newLength = 0;
169
+
170
+        for (int i = 0; i < array.length; i++) {
171
+            if (array[i].equals(compare) == false) {
172
+                compare = array[i];
173
+                newArray[newLength] = compare;
174
+                newLength++;
175
+            }
176
+        }
177
+        return Arrays.copyOf(newArray, newLength);
96
     }
178
     }
97
 
179
 
98
     /**
180
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
182
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
183
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
184
     public static String[] packConsecutiveDuplicates(String[] array) {
185
+
186
+        // First a loop to check for duplicates
187
+        
103
         return null;
188
         return null;
104
     }
189
     }
105
 
190