Randall Gibson Jr 6 lat temu
rodzic
commit
ee49ecaf73
3 zmienionych plików z 161 dodań i 18 usunięć
  1. 117
    16
      StringArrayUtils.java
  2. 2
    2
      StringArrayUtilsTest.java
  3. 42
    0
      package.bluej

+ 117
- 16
StringArrayUtils.java Wyświetl plik

@@ -1,5 +1,6 @@
1
- 
2
-
1
+import java.util.List;
2
+import java.util.ArrayList;
3
+import java.util.Arrays;
3 4
 /**
4 5
  * Created by leon on 1/29/18.
5 6
  */
@@ -9,7 +10,7 @@ public class StringArrayUtils {
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,32 +43,75 @@ 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) {
45
-        return false;
46
+        
47
+       
48
+       for(String string : array)
49
+       if(string.equalsIgnoreCase(value)){
50
+           return true;
51
+        }
52
+         return false;
46 53
     }
54
+    
55
+    //     for(int i=0;i<array.length;i++){
56
+   //         if(array[i]== value){
57
+                
58
+   //             return true;
59
+  //          }
60
+    
61
+
62
+   // }
63
+    
64
+   // return false;
65
+//}
66
+    
67
+
68
+
47 69
 
48 70
     /**
49 71
      * @param array of String objects
50 72
      * @return an array with identical contents in reverse order
51 73
      */ // TODO
52 74
     public static String[] reverse(String[] array) {
53
-        return null;
75
+        String[] answer = new String[array.length];
76
+        for(int i =0; i<array.length; i++)
77
+            answer[i] = array[array.length-1-i];
78
+        return answer;
54 79
     }
55 80
 
81
+    
82
+
56 83
     /**
57 84
      * @param array array of String objects
58 85
      * @return true if the order of the array is the same backwards and forwards
59 86
      */ // TODO
60 87
     public static boolean isPalindromic(String[] array) {
61
-        return false;
88
+       boolean answer = false;
89
+        if(Arrays.equals(array,reverse(array))){
90
+       answer=true;
91
+       }
92
+        return answer;
62 93
     }
94
+    
95
+
63 96
 
64 97
     /**
65 98
      * @param array array of String objects
66 99
      * @return true if each letter in the alphabet has been used in the array
67 100
      */ // TODO
68 101
     public static boolean isPangramic(String[] array) {
69
-        return false;
102
+        boolean isPangramic = false;
103
+        for(char i = 'A'; i <= 'Z'; i++) {
104
+        if(Arrays.toString(array).indexOf(i) < 0 && (Arrays.toString(array).indexOf((char) i + 32) < 0)) {
105
+        isPangramic = false;
106
+        }else{
107
+           isPangramic = true;
108
+         }
109
+         
110
+        }
111
+         return isPangramic; 
70 112
     }
113
+   
114
+
71 115
 
72 116
     /**
73 117
      * @param array array of String objects
@@ -75,8 +119,25 @@ public class StringArrayUtils {
75 119
      * @return number of occurrences the specified `value` has occurred
76 120
      */ // TODO
77 121
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
122
+        //int count = 0;
123
+        //for(String s : array){
124
+            //if(s.equals(value)){
125
+                //count +=1;
126
+                //}
127
+             //}
128
+             //return count;
129
+            //}
130
+        
131
+        int count=0;
132
+        for(int i =0;i<array.length; i++){
133
+        if(array[i].equals(value)){
134
+            count++;
135
+        }
79 136
     }
137
+    return count;
138
+}
139
+
140
+
80 141
 
81 142
     /**
82 143
      * @param array         array of String objects
@@ -84,24 +145,64 @@ 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;
88
-    }
148
+       //use method from above
149
+       int index = 0;
150
+        int count = getNumberOfOccurrences(array,valueToRemove);
151
+        String[] arr2 = new String[array.length -count];
152
+        
153
+        for(int i= 0; i<array.length;i++){
154
+            if(array[i] != valueToRemove){
155
+                arr2[index]=array[i];
156
+            index++;
157
+        
158
+     }
159
+    
160
+  }
161
+  return arr2;
162
+ }
163
+
164
+    
89 165
 
90 166
     /**
91 167
      * @param array array of chars
92 168
      * @return array of Strings with consecutive duplicates removes
93 169
      */ // TODO
94 170
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
171
+        String removeString = array[0] + " ";
172
+        for(int i =1; i<array.length; i++){
173
+         if(!array[i].equals(array[i-1])){
174
+             removeString+=array[i] + " ";
175
+            }
176
+        }
177
+        String [] removeStringA = removeString.split(" ");
178
+        return removeStringA;
96 179
     }
180
+    
181
+    
182
+    
97 183
 
98 184
     /**
99 185
      * @param array array of chars
100 186
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 187
      */ // TODO
102 188
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
189
+        String removeString = array[0];
190
+        for(int i =1; i<array.length; i++){
191
+            if(array[i].equals(array[i-1])){
192
+            removeString+=array[i];
193
+            }
194
+            else if(!array[i].equals(array[i-1])){
195
+                removeString += " " + array[i];
196
+            }   
197
+                
198
+                
199
+        }   
200
+            String[] removeStringA= removeString.split(" ");
201
+            
202
+            return removeStringA;
203
+            
204
+        
104 205
     }
105 206
 
106 207
 
107
-}
208
+ }

+ 2
- 2
StringArrayUtilsTest.java Wyświetl plik

@@ -1,5 +1,5 @@
1
- 
2
-
1
+import java.util.List; 
2
+import java.util.ArrayList;
3 3
 import org.junit.Assert;
4 4
 import org.junit.Test;
5 5
 

+ 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=722
6
+editor.fx.0.width=800
7
+editor.fx.0.x=239
8
+editor.fx.0.y=30
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=0
16
+package.editor.y=23
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