Alfredo Castillo 8 년 전
부모
커밋
b2f2d7576b
2개의 변경된 파일156개의 추가작업 그리고 13개의 파일을 삭제
  1. 114
    13
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 114
- 13
StringArrayUtils.java 파일 보기

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 Arrays.array
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];//null;
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
+        for (int i =0; i < array.length; i++) {
46
+            if (array[i].equals(value)) {
47
+                return true;
48
+            }
49
+
50
+        }
45
         return false;
51
         return false;
46
     }
52
     }
47
 
53
 
50
      * @return an array with identical contents in reverse order
56
      * @return an array with identical contents in reverse order
51
      */ // TODO
57
      */ // TODO
52
     public static String[] reverse(String[] array) {
58
     public static String[] reverse(String[] array) {
53
-        return null;
59
+        String[] arr = new String[array.length];
60
+        int n = 0;
61
+        for (int i = array.length -1; i >=0; i--){
62
+            arr[n] = array[i];
63
+            n++;
64
+        }
65
+        return arr;
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
+        boolean bool = false;
74
+
75
+        for (int i = 0; i < array.length / 2; i++){
76
+            if (array[i] == array[array.length-i-1]){
77
+                bool = true;
78
+            } 
79
+
80
+        }
81
+        return bool;
62
     }
82
     }
63
 
83
 
64
     /**
84
     /**
66
      * @return true if each letter in the alphabet has been used in the array
86
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
87
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
88
     public static boolean isPangramic(String[] array) {
69
-        return false;
89
+        boolean bool = false;
90
+
91
+        StringBuilder stringBuilder = new StringBuilder();
92
+
93
+        for (String x : array){
94
+            stringBuilder.append(x);
95
+        }
96
+
97
+        String s = "";
98
+        s = stringBuilder.toString();
99
+        for(char i = 'A'; i <= 'Z'; i++){
100
+            if ((s.indexOf(i) < 0) && (s.indexOf((char)i+32)< 0)){
101
+                bool = false;
102
+            }else{
103
+                bool = true;
104
+            }
105
+        }
106
+        return bool;
70
     }
107
     }
71
 
108
 
72
     /**
109
     /**
75
      * @return number of occurrences the specified `value` has occurred
112
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
113
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
114
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
115
+        int count = 0;
116
+        for (int i = 0; i < array.length; i++){
117
+            if(array[i].equals(value)) {
118
+                count++;
119
+            }
120
+        }
121
+
122
+        return count;
79
     }
123
     }
80
 
124
 
81
     /**
125
     /**
84
      * @return array with identical contents excluding values of `value`
128
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
129
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
130
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
131
+        int count = 0;
132
+        int occurrences = getNumberOfOccurrences(array, valueToRemove);
133
+        String[] arr = new String[array.length-occurrences]; 
134
+        for (String s : array) {
135
+            if (s != valueToRemove) {
136
+                arr[count] = s;
137
+                count++;
138
+            }
139
+        }
140
+        return arr;
88
     }
141
     }
89
 
142
 
90
     /**
143
     /**
92
      * @return array of Strings with consecutive duplicates removes
145
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
146
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
147
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
148
+        String [] arr = new String[array.length];
149
+        int newArrayIndex = 0;
150
+
151
+        arr[newArrayIndex] = array[0];
152
+
153
+        newArrayIndex++;
154
+        for (int i = 1; i < array.length; i++){
155
+            if(!(arr[newArrayIndex-1]).equals(array[i])){
156
+                arr[newArrayIndex] = array[i];
157
+                newArrayIndex++;
158
+            }
159
+
160
+        }
161
+        String[] newArray = new String[newArrayIndex];
162
+        System.arraycopy(arr, 0, newArray, 0, newArrayIndex);
163
+        return newArray;
96
     }
164
     }
97
 
165
 
98
     /**
166
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
168
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
169
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
170
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
171
+         StringBuilder build = new StringBuilder(array[0]);
172
+
173
+        String[] newArr = new String[array.length-count(array)]; 
174
+        int counter = 0; 
175
+        for(int i = 1; i<array.length; i++){
176
+            if(array[i-1].equals(array[i])){
177
+                build.append(array[i]);
178
+
179
+            }
180
+            else{
181
+                newArr[counter] = build.toString();
182
+                build.setLength(0);
183
+                build.append(array[i]);
184
+                counter++;
185
+            }
186
+        }
187
+        
188
+        newArr[counter] = build.toString();
189
+        return newArr;
190
+    }
191
+
192
+    public static int count(String[] array){
193
+        int count = 0;
194
+        for(int i = 1; i<array.length; i++){
195
+            if(array[i-1].equals(array[i])) {
196
+
197
+                count++;
198
+            }
199
+        }
200
+
201
+        return count;
202
+    }
203
+
204
+
205
+
104
     }
206
     }
105
 
207
 
106
 
208
 
107
-}

+ 42
- 0
package.bluej 파일 보기

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=92
8
+editor.fx.0.y=112
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=158
16
+package.editor.y=76
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