Kris Blassingame 8 år sedan
förälder
incheckning
93aee761b9
2 ändrade filer med 148 tillägg och 13 borttagningar
  1. 106
    13
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 106
- 13
StringArrayUtils.java Visa fil

1
- 
2
-
1
+import java.util.*;
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 array[0];
11
+        return array[0];
13
     }
12
     }
14
 
13
 
15
     /**
14
     /**
25
      * @return last element in specified array
24
      * @return last element in specified array
26
      */ // TODO
25
      */ // TODO
27
     public static String getLastElement(String[] array) {
26
     public static String getLastElement(String[] array) {
28
-        return null;
27
+        String lastStr = array[array.length - 1];
28
+        return lastStr;
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
+        String lastStr = array[array.length - 2];
37
+        return lastStr;
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 (int i = 0; i < array.length; i++){
47
+            if (array[i] == value) {
48
+                String result = array[i];
49
+            }
50
+            return true;
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
+        for (int i = 0; i < array.length/2; i++){
61
+            String temp = array[i];
62
+            array[i] = array[array.length - i - 1];
63
+            array[array.length - i - 1] = temp;
64
+        }
65
+        return array;
54
     }
66
     }
55
 
67
 
56
     /**
68
     /**
58
      * @return true if the order of the array is the same backwards and forwards
70
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
71
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
72
     public static boolean isPalindromic(String[] array) {
61
-        return false;
73
+        int i1 = 0;
74
+        int i2 = array.length - 1;
75
+        while (i2 > i1) {
76
+            if (array[i1] != array[i2]) {
77
+                return false;
78
+            }
79
+            ++i1;
80
+            --i2;
81
+        }
82
+        return true;
83
+
62
     }
84
     }
63
 
85
 
64
     /**
86
     /**
66
      * @return true if each letter in the alphabet has been used in the array
88
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
89
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
90
     public static boolean isPangramic(String[] array) {
69
-        return false;
91
+        boolean bool = true;
92
+        for (char letter = 'a'; letter <= 'z'; letter++){
93
+            boolean bool2 = false;
94
+
95
+            for (int i = 0; i < array.length; i++){
96
+                if (array[i].toLowerCase().contains(Character.toString(letter))){
97
+                    bool2 = true;
98
+                }
99
+            }
100
+            if (!bool2) {
101
+                bool = false;
102
+            }
103
+        }
104
+
105
+        return bool;
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 counter = 0;
115
+        for (String word : array) {
116
+            if (value.equals(word)){
117
+                counter++;
118
+            }
119
+        }
120
+
121
+        return counter;
79
     }
122
     }
80
 
123
 
81
     /**
124
     /**
84
      * @return array with identical contents excluding values of `value`
127
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
128
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
129
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
130
+        ArrayList<String> strArr = new ArrayList<String> (Arrays.asList(array));
131
+        strArr.remove(valueToRemove);
132
+        String[] newArr = strArr.toArray(new String[strArr.size()]);
133
+        return newArr;
88
     }
134
     }
89
 
135
 
90
     /**
136
     /**
92
      * @return array of Strings with consecutive duplicates removes
138
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
139
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
140
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
141
+        int duplicate = 0;
142
+        int counter = 0;
143
+
144
+        for (int i = 0; i < array.length - 1 ; i++){
145
+            if (array[i].equals(array[i+1])){
146
+                counter++;
147
+            }
148
+        }
149
+
150
+        String[] newArr = new String[array.length - counter];
151
+        String str = null;
152
+
153
+        for (int i = 0; i < array.length; i++){
154
+            if(!array[i].equals(str)){
155
+                newArr[duplicate] = array[i];
156
+                str = array[i];
157
+                duplicate++;
158
+
159
+            }
160
+        } 
161
+        return newArr;
96
     }
162
     }
97
 
163
 
98
     /**
164
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
166
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
167
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
168
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
169
+        int counter = getCounter(array);
170
+        String[] result = new String[array.length - counter];
171
+        String last = array[0];
172
+        result[0] = array[0];
173
+        int index = 0;
105
 
174
 
175
+        for (int i = 1; i < array.length; i++){
176
+            if (array[i].equals(last)){
177
+                result[index] += array[i];
178
+                last = array[i];
179
+            }
180
+            else {
181
+                index++;
182
+                result[index] = array[i];
183
+                last = array[i];
184
+            }
185
+        }
106
 
186
 
187
+        return result;
188
+    }
189
+
190
+    public static int getCounter(String[] array){
191
+        int counter = 0;
192
+        for(int i = 0; i < array.length - 1; i++){
193
+            if(array[i].equals(array[i+1])){
194
+                counter++;
195
+            }
196
+        }
197
+        return counter;
198
+    }
107
 }
199
 }
200
+

+ 42
- 0
package.bluej Visa fil

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=283
8
+editor.fx.0.y=94
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=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