David Thornley 8 yıl önce
ebeveyn
işleme
dc43be75a5
3 değiştirilmiş dosya ile 143 ekleme ve 14 silme
  1. 0
    0
      README.TXT
  2. 101
    14
      StringArrayUtils.java
  3. 42
    0
      package.bluej

+ 0
- 0
README.TXT Dosyayı Görüntüle


+ 101
- 14
StringArrayUtils.java Dosyayı Görüntüle

1
- 
2
 
1
 
3
 /**
2
 /**
4
  * Created by leon on 1/29/18.
3
  * Created by leon on 1/29/18.
5
  */
4
  */
5
+import java.util.Arrays;
6
 public class StringArrayUtils {
6
 public class StringArrayUtils {
7
     /**
7
     /**
8
      * @param array array of String objects
8
      * @param array array of String objects
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
     /**
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
+
46
+        boolean answer = false;
47
+        for(String arr: array) {
48
+            if (arr.equals(value)){
49
+                answer = true;
50
+                break;
51
+            } 
52
+        }
53
+        return answer;
46
     }
54
     }
47
 
55
 
48
     /**
56
     /**
50
      * @return an array with identical contents in reverse order
58
      * @return an array with identical contents in reverse order
51
      */ // TODO
59
      */ // TODO
52
     public static String[] reverse(String[] array) {
60
     public static String[] reverse(String[] array) {
53
-        return null;
61
+        String[] answer = new String[array.length];
62
+        int count = 0;
63
+
64
+        for(int i = array.length - 1; i >= 0; i--){
65
+            answer[count] = array[i];
66
+            count++;
67
+        }
68
+
69
+        return answer;
54
     }
70
     }
55
 
71
 
56
     /**
72
     /**
58
      * @return true if the order of the array is the same backwards and forwards
74
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
75
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
76
     public static boolean isPalindromic(String[] array) {
61
-        return false;
77
+        boolean answer = false;
78
+        String[] flipped = reverse(array);
79
+
80
+        if( Arrays.equals(array, flipped)){
81
+
82
+            answer = true;}
83
+
84
+        return answer;
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 = {"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"};
93
+        String bigString = "";
94
+
95
+        boolean result = true;
96
+        //could be seperate method makeString(String[] arr)
97
+        for(int i = 0; i<array.length;i++){
98
+            bigString = bigString + array[i];
99
+        }
100
+        // could be seperate method backToArray(String string)
101
+        String[] arrBig = new String[bigString.length()];
102
+        for(int j = 0; j < arrBig.length; j++){
103
+            arrBig[j]= (bigString.charAt(j)+"").toLowerCase();
104
+        }
105
+
106
+        for(int i = 0; i < alpha.length; i++){
107
+            if(!contains(arrBig,alpha[i])){
108
+                result = false;
109
+                break;
110
+            }
111
+        }
112
+
113
+        return result;
70
     }
114
     }
71
 
115
 
72
     /**
116
     /**
75
      * @return number of occurrences the specified `value` has occurred
119
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
120
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
121
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
122
+        int count = 0;
123
+        for(int i = 0; i < array.length; i++){
124
+            if(array[i].contains(value)){
125
+                count++;
126
+            }
127
+        }
128
+
129
+        return count;
79
     }
130
     }
80
 
131
 
81
     /**
132
     /**
84
      * @return array with identical contents excluding values of `value`
135
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
136
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
137
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
138
+        //iterate over an array of strings
139
+        // if array[i] !contains valueToRemove, add to new string
140
+        // convert string to array
141
+        // return new array
142
+        String saveMe = "";
143
+
144
+        for(int i = 0; i < array.length; i++){
145
+            if(array[i] != valueToRemove){
146
+                saveMe = saveMe + array[i] + " ";
147
+            }
148
+        }
149
+
150
+        String[] answer = saveMe.split(" ", 0);
151
+
152
+        return answer;
88
     }
153
     }
89
 
154
 
90
     /**
155
     /**
92
      * @return array of Strings with consecutive duplicates removes
157
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
158
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
159
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
160
+        //create string to save needed values
161
+        int count = 0;
162
+        String saveMe = "";
163
+        //loop array and compare array[i] and array[i+1]
164
+        for(int i = 0; i < array.length; i++){
165
+            
166
+            if(count > 1 && array[i] == array[i+1]){
167
+            saveMe = saveMe;
168
+            } else if(count == 0 && array[i] == array[i + 1] && i < array.length - 2){
169
+            count ++;
170
+            saveMe = saveMe + array[i] + " ";
171
+            }else if(array[i] != array[i -1]){
172
+            saveMe = saveMe + array[i] + " ";
173
+            }
174
+        }
175
+        String[] answer = saveMe.split(" ");
176
+        return answer;
96
     }
177
     }
97
 
178
 
98
     /**
179
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
181
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
182
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
183
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
184
+        String saveMe = "";
105
 
185
 
186
+        for(int i = 0; i < array.length; i++){
187
+            saveMe = saveMe +array[i];
188
+        }
189
+        String[]answer = saveMe.split("(?<=(.))(?!\\1)");
190
+
191
+        return answer;
192
+    }
106
 
193
 
107
 }
194
 }

+ 42
- 0
package.bluej Dosyayı Görüntüle

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=991
7
+editor.fx.0.x=119
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=139
16
+package.editor.y=39
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