Selaa lähdekoodia

need pack to finsh

Shivam Patel 8 vuotta sitten
vanhempi
commit
318b88febb
3 muutettua tiedostoa jossa 165 lisäystä ja 21 poistoa
  1. 121
    14
      StringArrayUtils.java
  2. 2
    7
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 121
- 14
StringArrayUtils.java Näytä tiedosto

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
+        String first = "";
13
+
14
+        first += array[0];
15
+        return first;
13
     }
16
     }
14
 
17
 
15
     /**
18
     /**
17
      * @return second element in specified array
20
      * @return second element in specified array
18
      */
21
      */
19
     public static String getSecondElement(String[] array) {
22
     public static String getSecondElement(String[] array) {
20
-        return null;
23
+        String second = "";
24
+
25
+        second += array[1];
26
+
27
+        return second;
21
     }
28
     }
22
 
29
 
23
     /**
30
     /**
25
      * @return last element in specified array
32
      * @return last element in specified array
26
      */ // TODO
33
      */ // TODO
27
     public static String getLastElement(String[] array) {
34
     public static String getLastElement(String[] array) {
28
-        return null;
35
+        String last = "";
36
+
37
+        last += array[array.length-1];
38
+        return last;
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
+        String secLast = "";
47
+
48
+        secLast += array[array.length-2];
49
+        return secLast;
37
     }
50
     }
38
 
51
 
39
     /**
52
     /**
42
      * @return true if the array contains the specified `value`
55
      * @return true if the array contains the specified `value`
43
      */ // TODO
56
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
57
     public static boolean contains(String[] array, String value) {
45
-        return false;
58
+        boolean contain = false;
59
+
60
+        for(String s : array){
61
+            if(s==value){
62
+                contain = true;
63
+
64
+            }
65
+
66
+        }
67
+
68
+        return contain;
46
     }
69
     }
47
 
70
 
48
     /**
71
     /**
50
      * @return an array with identical contents in reverse order
73
      * @return an array with identical contents in reverse order
51
      */ // TODO
74
      */ // TODO
52
     public static String[] reverse(String[] array) {
75
     public static String[] reverse(String[] array) {
53
-        return null;
76
+
77
+        String reversal;
78
+
79
+        for(int i = 0; i<array.length/2; i++){
80
+            reversal = array[i];
81
+            array[i] = array[array.length - i -1];
82
+            array[array.length - i -1] = reversal;
83
+        }
84
+
85
+        return array;
86
+
54
     }
87
     }
55
 
88
 
56
     /**
89
     /**
58
      * @return true if the order of the array is the same backwards and forwards
91
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
92
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
93
     public static boolean isPalindromic(String[] array) {
61
-        return false;
94
+        boolean pal = false;
95
+
96
+        String reversal;
97
+
98
+        for(int i = 0; i<array.length/2; i++){
99
+
100
+            if(array[i] == array[array.length - i -1]){
101
+                pal = true;
102
+            }
103
+
104
+        }
105
+        return pal;
62
     }
106
     }
63
 
107
 
64
     /**
108
     /**
66
      * @return true if each letter in the alphabet has been used in the array
110
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
111
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
112
     public static boolean isPangramic(String[] array) {
69
-        return false;
113
+        boolean pan = false;
114
+
115
+        StringBuilder build = new StringBuilder();
116
+
117
+        for(String s : array){
118
+            build.append(s);
119
+        }
120
+
121
+        String ofArray = "";
122
+        ofArray = build.toString();
123
+        for(char i = 'A'; i <= 'Z'; i++){ 
124
+            if((ofArray.indexOf(i) < 0) && (ofArray.indexOf((char)(i+32)) < 0))
125
+            { // goes thru the ASCII list
126
+                pan = false;
127
+            }
128
+            else{
129
+                pan = true;
130
+            }
131
+        }
132
+        return pan;
133
+
70
     }
134
     }
71
 
135
 
72
     /**
136
     /**
75
      * @return number of occurrences the specified `value` has occurred
139
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
140
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
141
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
142
+        int occurs = 0;
143
+
144
+        for(int i=0; i<array.length; i++){
145
+            if(value.equals(array[i])){
146
+                occurs += 1 ;
147
+            }
148
+
149
+        }
150
+
151
+        return occurs;
79
     }
152
     }
80
 
153
 
81
     /**
154
     /**
84
      * @return array with identical contents excluding values of `value`
157
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
158
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
159
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
160
+        int count = 0;
161
+        int occur = getNumberOfOccurrences(array, valueToRemove);
162
+
163
+        String[] exclude = new String[array.length - occur];
164
+        for(String i : array){
165
+            if(i != valueToRemove){ 
166
+
167
+                exclude[count] = i;
168
+                count++;
169
+
170
+            }
171
+
172
+        }
173
+
174
+        return exclude;
88
     }
175
     }
89
 
176
 
90
     /**
177
     /**
92
      * @return array of Strings with consecutive duplicates removes
179
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
180
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
181
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
182
+        String[] excluded = new String[array.length];
183
+        int newArrIndex = 0;
184
+        excluded[newArrIndex] = array[0];
185
+
186
+        newArrIndex++;
187
+        for(int i = 1; i<array.length; i++){
188
+            if(!(excluded[newArrIndex-1]).equals(array[i])){
189
+                excluded[newArrIndex] = array[i];
190
+                newArrIndex++;
191
+                
192
+            }    
193
+        }
194
+        
195
+        String[] newArr = new String[newArrIndex];
196
+        System.arraycopy(excluded, 0, newArr, 0, newArrIndex);
197
+        
198
+        return newArr;
199
+
96
     }
200
     }
97
 
201
 
98
     /**
202
     /**
99
      * @param array array of chars
203
      * @param array array of chars
100
-     * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
204
+     * @return array of Strings with each consecutive duplicate 
205
+     *  occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
206
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
207
     public static String[] packConsecutiveDuplicates(String[] array) {
208
+        String[] newArr = new String[array.length]; 
209
+        
210
+        
103
         return null;
211
         return null;
104
     }
212
     }
105
 
213
 
106
-
107
 }
214
 }

+ 2
- 7
StringArrayUtilsTest.java Näytä tiedosto

261
         Assert.assertTrue(outcome);
261
         Assert.assertTrue(outcome);
262
     }
262
     }
263
 
263
 
264
+    
264
     @Test
265
     @Test
265
     public void testIsPangramic2() {
266
     public void testIsPangramic2() {
266
         String[] array = {"The", "quick", "onyx", "goblin", "jumps", "over", "the", "lazy", "dwarf"};
267
         String[] array = {"The", "quick", "onyx", "goblin", "jumps", "over", "the", "lazy", "dwarf"};
282
         boolean outcome = StringArrayUtils.isPangramic(array);
283
         boolean outcome = StringArrayUtils.isPangramic(array);
283
         Assert.assertFalse(outcome);
284
         Assert.assertFalse(outcome);
284
     }
285
     }
285
-
286
-
287
-
286
+    
288
 
287
 
289
 
288
 
290
 
289
 
365
 
364
 
366
 
365
 
367
 
366
 
368
-
369
-
370
-
371
-
372
     @Test
367
     @Test
373
     public void testRemovePackDuplicates1() {
368
     public void testRemovePackDuplicates1() {
374
         String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
369
         String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};

+ 42
- 0
package.bluej Näytä tiedosto

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=-79
8
+editor.fx.0.y=-818
9
+objectbench.height=101
10
+objectbench.width=776
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=22
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=300
35
+target1.y=30
36
+target2.height=50
37
+target2.name=StringArrayUtils
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=120
41
+target2.x=240
42
+target2.y=210