#32 dmjpub

Otwarty
dmjpub chce scalić 2 commity/ów z dmjpub/ZCW-Arrays-StringArrayUtilities-BlueJ:master do master
2 zmienionych plików z 148 dodań i 12 usunięć
  1. 106
    12
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 106
- 12
StringArrayUtils.java Wyświetl plik

@@ -1,15 +1,16 @@
1
- 
1
+import java.util.Arrays;
2 2
 
3 3
 /**
4 4
  * Created by leon on 1/29/18.
5 5
  */
6 6
 public class StringArrayUtils {
7
+    
7 8
     /**
8 9
      * @param array array of String objects
9 10
      * @return first element of specified array
10 11
      */ // TODO
11 12
     public static String getFirstElement(String[] array) {
12
-        return null;
13
+        return array[0];
13 14
     }
14 15
 
15 16
     /**
@@ -17,7 +18,7 @@ public class StringArrayUtils {
17 18
      * @return second element in specified array
18 19
      */
19 20
     public static String getSecondElement(String[] array) {
20
-        return null;
21
+        return array[1];
21 22
     }
22 23
 
23 24
     /**
@@ -25,7 +26,7 @@ public class StringArrayUtils {
25 26
      * @return last element in specified array
26 27
      */ // TODO
27 28
     public static String getLastElement(String[] array) {
28
-        return null;
29
+        return array[array.length-1];
29 30
     }
30 31
 
31 32
     /**
@@ -33,7 +34,7 @@ public class StringArrayUtils {
33 34
      * @return second to last element in specified array
34 35
      */ // TODO
35 36
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
37
+        return array[array.length-2];
37 38
     }
38 39
 
39 40
     /**
@@ -42,6 +43,9 @@ public class StringArrayUtils {
42 43
      * @return true if the array contains the specified `value`
43 44
      */ // TODO
44 45
     public static boolean contains(String[] array, String value) {
46
+        for(int i = 0; i < array.length; i++) {
47
+            if(array[i].equals(value)) return true;
48
+        }
45 49
         return false;
46 50
     }
47 51
 
@@ -50,7 +54,11 @@ public class StringArrayUtils {
50 54
      * @return an array with identical contents in reverse order
51 55
      */ // TODO
52 56
     public static String[] reverse(String[] array) {
53
-        return null;
57
+        String[] revArray = new String[array.length];
58
+        for (int i = 0; i < array.length; i++) {
59
+            revArray[i] = array[array.length - 1 - i];
60
+        }
61
+        return revArray;
54 62
     }
55 63
 
56 64
     /**
@@ -58,7 +66,13 @@ public class StringArrayUtils {
58 66
      * @return true if the order of the array is the same backwards and forwards
59 67
      */ // TODO
60 68
     public static boolean isPalindromic(String[] array) {
61
-        return false;
69
+
70
+        for ( int i = 0; i < array.length / 2; i++) {
71
+            if (array[i] != array[array.length - 1 - i]) {
72
+                return false;
73
+            }
74
+        }    
75
+        return true;
62 76
     }
63 77
 
64 78
     /**
@@ -66,16 +80,41 @@ public class StringArrayUtils {
66 80
      * @return true if each letter in the alphabet has been used in the array
67 81
      */ // TODO
68 82
     public static boolean isPangramic(String[] array) {
83
+        //boolean result= true;
84
+        String words = String.join("", array).toLowerCase();
85
+
86
+      int counterTrue = 0;
87
+        for(char c = 'a';c <= 'z'; c++) {
88
+
89
+            if(words.contains(String.valueOf(c))) {
90
+                //result = false;
91
+                counterTrue++;
92
+                //return true;
93
+            } 
94
+        }
95
+
96
+        if (counterTrue == 26) return true;
69 97
         return false;
70 98
     }
99
+        
100
+    
71 101
 
72 102
     /**
73 103
      * @param array array of String objects
74 104
      * @param value value to check array for
75 105
      * @return number of occurrences the specified `value` has occurred
76 106
      */ // TODO
107
+
77 108
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
109
+        int num = 0;
110
+        for (int i=0;i<array.length;i++){
111
+            if (array[i]== value){
112
+                num++;
113
+            }
114
+
115
+        }
116
+
117
+        return num;
79 118
     }
80 119
 
81 120
     /**
@@ -84,7 +123,23 @@ public class StringArrayUtils {
84 123
      * @return array with identical contents excluding values of `value`
85 124
      */ // TODO
86 125
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
126
+        int newLength = array.length;
127
+        String[] result = new String[array.length-1];
128
+        String[] removedValue = new String[1];
129
+        int count1 = 0; // count tracks the current index of the output array
130
+        int count2 = 0; // count tracks the current index of the output array
131
+        
132
+        for(int i = 0; i < array.length; i++) // i tracks the current index of the input array
133
+        {
134
+            if(!array[i].contains(valueToRemove)) {
135
+                result[count1] = array[i]; 
136
+                count1++;
137
+            }else {
138
+                removedValue[count2] = array[i];
139
+                count2++;
140
+            }
141
+        }
142
+        return result;
88 143
     }
89 144
 
90 145
     /**
@@ -92,7 +147,25 @@ public class StringArrayUtils {
92 147
      * @return array of Strings with consecutive duplicates removes
93 148
      */ // TODO
94 149
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
150
+        String[] resultArray = new String[array.length];
151
+        int resultIndex = 0;
152
+        for(int i = 0; i < array.length; i++) {
153
+            
154
+            if(i == 0) {
155
+                resultArray[resultIndex] = array[i];
156
+                resultIndex++;
157
+            }
158
+            else if((i > 0 && i < array.length-2) && (!array[i].equals(array[i-1]) || !array[i].equals(array[i+1])))
159
+            {
160
+                resultArray[resultIndex] = array[i];
161
+                resultIndex++;
162
+            }else if (i == array.length-1 && !array[i].equals(array[i-1])) {
163
+                resultArray[resultIndex] = array[i];
164
+                resultIndex++;
165
+            }
166
+        }
167
+        String[] realAnswer = Arrays.copyOf(resultArray, resultIndex);
168
+        return realAnswer;
96 169
     }
97 170
 
98 171
     /**
@@ -100,8 +173,29 @@ public class StringArrayUtils {
100 173
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 174
      */ // TODO
102 175
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
176
+        String[] resultArray = new String[array.length];
177
+        StringBuilder dups = new StringBuilder();
178
+        int resultIndex = 0;
179
+        for(int i = 0; i < array.length; i++) {
180
+            
181
+            if(i == 0) {
182
+                resultArray[resultIndex] = array[i];
183
+                dups.append(array[i].toString());
184
+                resultIndex++;
185
+            }
186
+            else if((i > 0 && i < array.length-2) && (array[i].equals(array[i-1]) || array[i].equals(array[i+1])))
187
+            {
188
+                resultArray[resultIndex] = array[i];
189
+                dups.append(array[i].toString());
190
+                resultIndex++;
191
+            }else if (i == array.length-1 && array[i].equals(array[i-1])) {
192
+                resultArray[resultIndex] = array[i];
193
+                dups.append(array[i].toString());
194
+                resultIndex++;
195
+            }
196
+        }
197
+        String[] realAnswer = Arrays.copyOf(resultArray, resultIndex);
198
+        return realAnswer;
104 199
     }
105 200
 
106
-
107 201
 }

+ 42
- 0
package.bluej Wyświetl plik

@@ -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=714
6
+editor.fx.0.width=800
7
+editor.fx.0.x=442
8
+editor.fx.0.y=33
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=267
16
+package.editor.y=85
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