Roy 8 vuotta sitten
vanhempi
commit
d94af78fa6
3 muutettua tiedostoa jossa 181 lisäystä ja 14 poistoa
  1. 134
    13
      StringArrayUtils.java
  2. 5
    1
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 134
- 13
StringArrayUtils.java Näytä tiedosto

@@ -1,15 +1,17 @@
1
- 
2 1
 
3 2
 /**
4 3
  * Created by leon on 1/29/18.
5 4
  */
5
+import java.util.Arrays;
6
+import java.lang.*;
6 7
 public class StringArrayUtils {
7 8
     /**
8 9
      * @param array array of String objects
9 10
      * @return first element of specified array
10 11
      */ // TODO
12
+    static int countRoy = 0;
11 13
     public static String getFirstElement(String[] array) {
12
-        return null;
14
+        return array[0];
13 15
     }
14 16
 
15 17
     /**
@@ -17,7 +19,7 @@ public class StringArrayUtils {
17 19
      * @return second element in specified array
18 20
      */
19 21
     public static String getSecondElement(String[] array) {
20
-        return null;
22
+        return array[1];
21 23
     }
22 24
 
23 25
     /**
@@ -25,7 +27,7 @@ public class StringArrayUtils {
25 27
      * @return last element in specified array
26 28
      */ // TODO
27 29
     public static String getLastElement(String[] array) {
28
-        return null;
30
+        return array[array.length-1];
29 31
     }
30 32
 
31 33
     /**
@@ -33,7 +35,7 @@ public class StringArrayUtils {
33 35
      * @return second to last element in specified array
34 36
      */ // TODO
35 37
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
38
+        return array[array.length-2];
37 39
     }
38 40
 
39 41
     /**
@@ -42,6 +44,11 @@ public class StringArrayUtils {
42 44
      * @return true if the array contains the specified `value`
43 45
      */ // TODO
44 46
     public static boolean contains(String[] array, String value) {
47
+        for(int i =0; i<array.length; i++){
48
+            if (array[i].equals(value)){
49
+                return true;
50
+            }
51
+        }
45 52
         return false;
46 53
     }
47 54
 
@@ -50,7 +57,13 @@ public class StringArrayUtils {
50 57
      * @return an array with identical contents in reverse order
51 58
      */ // TODO
52 59
     public static String[] reverse(String[] array) {
53
-        return null;
60
+        
61
+        for(int i = 0 ; i < array.length/2; i++){
62
+            String temp1 = array[i];
63
+            array[i] = array[array.length-i-1];
64
+            array[array.length-i-1] = temp1;
65
+        }
66
+        return array;
54 67
     }
55 68
 
56 69
     /**
@@ -58,15 +71,57 @@ public class StringArrayUtils {
58 71
      * @return true if the order of the array is the same backwards and forwards
59 72
      */ // TODO
60 73
     public static boolean isPalindromic(String[] array) {
74
+        // String[] arrayCopy = reverse(array);
75
+        String[] arrayCopy = new String[array.length];
76
+        int j = 0;
77
+        for (int i = array.length - 1; i >= 0; i--) {
78
+            System.out.println(j);
79
+            arrayCopy[j++] = array[i];
80
+            System.out.println(j);
81
+        }
82
+        if (Arrays.equals(array, arrayCopy)){
83
+        return true;
84
+        }
85
+        //System.out.println(array[]);
61 86
         return false;
87
+        
62 88
     }
63 89
 
64 90
     /**
65 91
      * @param array array of String objects
66 92
      * @return true if each letter in the alphabet has been used in the array
67 93
      */ // TODO
68
-    public static boolean isPangramic(String[] array) {
69
-        return false;
94
+    public static boolean isPangramic(String[] array){
95
+        String alphabet = "abcdefghijklmnopqrstuvwxyz";
96
+        //System.out.println(newArray);
97
+        boolean pangramic = true;
98
+        for(int i =0; i<alphabet.length(); i++){
99
+            boolean letterFound = false;
100
+            for(int j =0; j<array.length; j++){
101
+                
102
+                if(array[j].toLowerCase().contains(Character.toString(alphabet.charAt(i)))){
103
+                    letterFound = true;
104
+                    break;
105
+                }
106
+                
107
+            }
108
+            if (!letterFound){
109
+                pangramic = false;
110
+                break;
111
+            }
112
+            
113
+        }
114
+
115
+        return pangramic;
116
+    }
117
+
118
+    public void println(){
119
+        String[] s = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
120
+        s = reverse(s);
121
+        for (String sa: s){
122
+            System.out.println(sa);
123
+        }
124
+        System.out.println(countRoy);
70 125
     }
71 126
 
72 127
     /**
@@ -75,7 +130,13 @@ public class StringArrayUtils {
75 130
      * @return number of occurrences the specified `value` has occurred
76 131
      */ // TODO
77 132
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
133
+        int numberOfOccurances =0;
134
+        for(int i=0; i<array.length; i++){
135
+            if (array[i] == value){
136
+                numberOfOccurances++;
137
+            }
138
+        }
139
+        return numberOfOccurances;
79 140
     }
80 141
 
81 142
     /**
@@ -84,7 +145,29 @@ public class StringArrayUtils {
84 145
      * @return array with identical contents excluding values of `value`
85 146
      */ // TODO
86 147
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
148
+        int occurance = getNumberOfOccurrences(array, valueToRemove);
149
+
150
+        String[] newArray = new String[array.length - occurance];
151
+        int j = 0;
152
+        for(int i=0; i<array.length; i++){
153
+            if (array[i] != valueToRemove){
154
+                newArray[j] = array[i];
155
+                j++;
156
+            }
157
+
158
+        }
159
+        return newArray;
160
+    }
161
+
162
+    public static int countOcuurances(String[] array){
163
+        int count =0;
164
+        for(int i=1; i<array.length; i++){
165
+
166
+            if (array[i-1] == array[i]){
167
+                count++;
168
+            }
169
+        }
170
+        return count;
88 171
     }
89 172
 
90 173
     /**
@@ -92,7 +175,25 @@ public class StringArrayUtils {
92 175
      * @return array of Strings with consecutive duplicates removes
93 176
      */ // TODO
94 177
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
178
+        int count = countOcuurances(array);
179
+        String[] newArray = new String[array.length - count];
180
+        String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
181
+        int j =0;
182
+        for(int i=1; i<array.length; i++){
183
+            if(array[i-1] != array[i] ){
184
+                System.out.println(array[i]);
185
+                newArray[j] = array[i-1];
186
+                j++;
187
+            }
188
+
189
+            if (i == array.length - 1 ) {
190
+                newArray[j] = array[array.length - 1];
191
+
192
+            }
193
+        }
194
+        //System.out.println(newArray[0]);
195
+        return newArray;
196
+
96 197
     }
97 198
 
98 199
     /**
@@ -100,8 +201,28 @@ public class StringArrayUtils {
100 201
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 202
      */ // TODO
102 203
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
204
+        int count = countOcuurances(array);
205
+        int j=0;
206
+        String[] newArray = new String[array.length - count];
207
+        newArray[0] = array[0];
208
+        for(int i=1; i<array.length; i++){
209
+            if(array[i-1].equals(array[i]) ){
210
+                //System.out.println(array[i]);
211
+                newArray[j] += array[i];
212
+                //j++;
213
+
214
+            }
105 215
 
216
+            else {
217
+                j++;
218
+                newArray[j] = array[i];  
219
+            }
220
+            // else if (i == array.length - 1 ) {
221
+            // newArray[j] = array[array.length - 1];
222
+
223
+            // }
224
+        }
225
+        return newArray;
226
+    }
106 227
 
107 228
 }

+ 5
- 1
StringArrayUtilsTest.java Näytä tiedosto

@@ -2,6 +2,7 @@
2 2
 
3 3
 import org.junit.Assert;
4 4
 import org.junit.Test;
5
+import java.util.*;
5 6
 
6 7
 /**
7 8
  * Created by leon on 1/29/18.
@@ -243,6 +244,7 @@ public class StringArrayUtilsTest {
243 244
     public void testIsPalindromic3() {
244 245
         String[] array = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
245 246
         boolean outcome = StringArrayUtils.isPalindromic(array);
247
+        //System.out.println(outcome);
246 248
         Assert.assertFalse(outcome);
247 249
     }
248 250
 
@@ -355,8 +357,10 @@ public class StringArrayUtilsTest {
355 357
     public void testRemoveConsecutiveDuplicates3() {
356 358
         String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "aba", "bbb"};
357 359
         String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
360
+        
361
+        System.out.println(Arrays.toString(actual));
358 362
         String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
359
-        Assert.assertEquals(actual, expected);
363
+        Assert.assertEquals(expected, actual);
360 364
     }
361 365
 
362 366
 

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

@@ -0,0 +1,42 @@
1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=800
6
+editor.fx.0.width=1280
7
+editor.fx.0.x=0
8
+editor.fx.0.y=0
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=20
16
+package.editor.y=126
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=30
35
+target1.y=0
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