Przeglądaj źródła

Legacy code all the way!

Chad 8 lat temu
rodzic
commit
7e64588e6f
2 zmienionych plików z 168 dodań i 12 usunięć
  1. 126
    12
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 126
- 12
StringArrayUtils.java Wyświetl plik

1
- 
1
+import java.util.Arrays;
2
 
2
 
3
 /**
3
 /**
4
  * Created by leon on 1/29/18.
4
  * Created by leon on 1/29/18.
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
+
13
+        return array[0];
13
     }
14
     }
14
 
15
 
15
     /**
16
     /**
17
      * @return second element in specified array
18
      * @return second element in specified array
18
      */
19
      */
19
     public static String getSecondElement(String[] array) {
20
     public static String getSecondElement(String[] array) {
20
-        return null;
21
+        return array[1];
21
     }
22
     }
22
 
23
 
23
     /**
24
     /**
25
      * @return last element in specified array
26
      * @return last element in specified array
26
      */ // TODO
27
      */ // TODO
27
     public static String getLastElement(String[] array) {
28
     public static String getLastElement(String[] array) {
28
-        return null;
29
+        return array[array.length-1];
29
     }
30
     }
30
 
31
 
31
     /**
32
     /**
33
      * @return second to last element in specified array
34
      * @return second to last element in specified array
34
      */ // TODO
35
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
36
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
37
+        return array[array.length-2];
37
     }
38
     }
38
 
39
 
39
     /**
40
     /**
42
      * @return true if the array contains the specified `value`
43
      * @return true if the array contains the specified `value`
43
      */ // TODO
44
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
45
     public static boolean contains(String[] array, String value) {
46
+        for(String entry: array){
47
+            if(entry.equalsIgnoreCase(value)){
48
+                return true;
49
+            }
50
+        }
51
+
45
         return false;
52
         return false;
46
     }
53
     }
47
 
54
 
50
      * @return an array with identical contents in reverse order
57
      * @return an array with identical contents in reverse order
51
      */ // TODO
58
      */ // TODO
52
     public static String[] reverse(String[] array) {
59
     public static String[] reverse(String[] array) {
53
-        return null;
60
+        String[] reverse = new String[array.length];
61
+        int j= 0;
62
+        for(int i=array.length-1; i> -1; i--){
63
+            reverse[j] = array[i];
64
+
65
+            j++;
66
+        } 
67
+
68
+        return reverse;
54
     }
69
     }
55
 
70
 
56
     /**
71
     /**
58
      * @return true if the order of the array is the same backwards and forwards
73
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
74
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
75
     public static boolean isPalindromic(String[] array) {
76
+        String [] reverse = new String[array.length];
77
+        reverse = reverse(array);
78
+
79
+        if(Arrays.equals(array, reverse)){
80
+            return true;
81
+        }
61
         return false;
82
         return false;
62
     }
83
     }
63
 
84
 
66
      * @return true if each letter in the alphabet has been used in the array
87
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
88
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
89
     public static boolean isPangramic(String[] array) {
69
-        return false;
90
+        boolean output = false;
91
+        char [] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
92
+        int count = 0;
93
+        for(int i =0; i< array.length; i++){
94
+            for(int j =0; j< array[i].length(); j++){
95
+                for( int k = 0; k<alphabet.length; k++){
96
+                    if(array[i].toUpperCase().charAt(j) == (alphabet[k])){
97
+                        count++;
98
+                        continue;
99
+                    }
100
+                    if(count == 26){
101
+                        output=true;
102
+                        break;
103
+                    }
104
+                }
105
+            }
106
+        }
107
+
108
+        return output;
70
     }
109
     }
71
 
110
 
72
     /**
111
     /**
75
      * @return number of occurrences the specified `value` has occurred
114
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
115
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
116
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
117
+        int count = 0;
118
+
119
+        for(String entry: array){
120
+            if(entry.equals(value)){
121
+                count++;
122
+            }
123
+        }
124
+
125
+        return count;
79
     }
126
     }
80
 
127
 
81
     /**
128
     /**
84
      * @return array with identical contents excluding values of `value`
131
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
132
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
133
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
134
+
135
+        int totaltodelete = getNumberOfOccurrences(array,valueToRemove);
136
+        String[] output = new String[array.length- totaltodelete];
137
+        int count = 0;
138
+
139
+        for(int i =0; i< array.length; i++){
140
+            if(array[i].equals(valueToRemove)){
141
+
142
+            } else{
143
+                output[count] =array[i];
144
+                count++;
145
+            }   
146
+        }
147
+
148
+        return output;
88
     }
149
     }
89
 
150
 
90
     /**
151
     /**
92
      * @return array of Strings with consecutive duplicates removes
153
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
154
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
155
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
156
+        int count = 0;
157
+        int entry = 0;
158
+
159
+        for(int i =0; i< array.length; i++){
160
+            if((i-1>-1) && (array[i].equals(array[i-1]))){
161
+                count++;
162
+            } 
163
+        }
164
+        String[] output = new String[array.length - count];
165
+        for(int i =0; i< array.length; i++){
166
+            if((i-1>-1) && (array[i].equals(array[i-1]))){
167
+
168
+            }else{
169
+                output[entry] =array[i];
170
+                entry++;
171
+            }
172
+        }
173
+        return output;
96
     }
174
     }
97
 
175
 
98
     /**
176
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
178
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
179
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
180
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105
 
181
 
182
+        int count = 0;
183
+        int entry = 0;
184
+        int dupes = 0;
185
+
186
+        for(int i =0; i< array.length; i++){
187
+            if(((i+1< array.length) && (array[i].equals(array[i+1])))){
106
 
188
 
189
+            } else{
190
+                count++;
191
+            }
192
+        }
193
+
194
+        String[] output = new String[count];
195
+        StringBuilder consecutive = new StringBuilder("");
196
+        for(int i =0; i< array.length; i++){
197
+            //Is the entry equal to an adjacent entry? If so apeend to consecutive
198
+            if((i == array.length -1) || (entry == 0) || (i+1< array.length) && (i-1>-1) && (array[i].equals(array[i-1]) || array[i].equals(array[i+1]))){
199
+                consecutive.append(array[i]);
200
+                entry++;
201
+                if((i == array.length -1) || (i+1< array.length) && !(array[i].equals(array[i+1]))){
202
+                    output[dupes] = consecutive.toString();
203
+                    consecutive.setLength(0);
204
+                    dupes++;
205
+                }
206
+            }
207
+            //If not add it to the array
208
+            else{
209
+                //but make sure to add consecutive to the array first
210
+                if(consecutive.length() > 0){
211
+                    output[dupes] = array[i];
212
+                }else{
213
+                    output[dupes] = array[i];
214
+                }
215
+                dupes++;
216
+            }
217
+        }
218
+        System.out.print(output.toString());
219
+        return output;
220
+    }
107
 }
221
 }

+ 42
- 0
package.bluej Wyświetl plik

1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=709
6
+editor.fx.0.width=800
7
+editor.fx.0.x=240
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=208
16
+package.editor.y=135
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