Roy 8 lat temu
rodzic
commit
d94af78fa6
3 zmienionych plików z 181 dodań i 14 usunięć
  1. 134
    13
      StringArrayUtils.java
  2. 5
    1
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 134
- 13
StringArrayUtils.java Wyświetl plik

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
+import java.lang.*;
6
 public class StringArrayUtils {
7
 public class StringArrayUtils {
7
     /**
8
     /**
8
      * @param array array of String objects
9
      * @param array array of String objects
9
      * @return first element of specified array
10
      * @return first element of specified array
10
      */ // TODO
11
      */ // TODO
12
+    static int countRoy = 0;
11
     public static String getFirstElement(String[] array) {
13
     public static String getFirstElement(String[] array) {
12
-        return null;
14
+        return array[0];
13
     }
15
     }
14
 
16
 
15
     /**
17
     /**
17
      * @return second element in specified array
19
      * @return second element in specified array
18
      */
20
      */
19
     public static String getSecondElement(String[] array) {
21
     public static String getSecondElement(String[] array) {
20
-        return null;
22
+        return array[1];
21
     }
23
     }
22
 
24
 
23
     /**
25
     /**
25
      * @return last element in specified array
27
      * @return last element in specified array
26
      */ // TODO
28
      */ // TODO
27
     public static String getLastElement(String[] array) {
29
     public static String getLastElement(String[] array) {
28
-        return null;
30
+        return array[array.length-1];
29
     }
31
     }
30
 
32
 
31
     /**
33
     /**
33
      * @return second to last element in specified array
35
      * @return second to last element in specified array
34
      */ // TODO
36
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
37
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
38
+        return array[array.length-2];
37
     }
39
     }
38
 
40
 
39
     /**
41
     /**
42
      * @return true if the array contains the specified `value`
44
      * @return true if the array contains the specified `value`
43
      */ // TODO
45
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
         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
+        
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
      * @return true if the order of the array is the same backwards and forwards
71
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
72
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
         return false;
86
         return false;
87
+        
62
     }
88
     }
63
 
89
 
64
     /**
90
     /**
65
      * @param array array of String objects
91
      * @param array array of String objects
66
      * @return true if each letter in the alphabet has been used in the array
92
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
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
      * @return number of occurrences the specified `value` has occurred
130
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
131
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
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
      * @return array with identical contents excluding values of `value`
145
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
146
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
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
      * @return array of Strings with consecutive duplicates removes
175
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
176
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
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
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
201
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
202
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
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 Wyświetl plik

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

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