#31 lewis lab

Ouvert
LewisDominguez veut fusionner 1 révision(s) depuis LewisDominguez/ZCW-Arrays-StringArrayUtilities-BlueJ:master vers master
2 fichiers modifiés avec 149 ajouts et 16 suppressions
  1. 107
    16
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 107
- 16
StringArrayUtils.java Voir le fichier

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

+ 42
- 0
package.bluej Voir le fichier

1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=717
6
+editor.fx.0.width=645
7
+editor.fx.0.x=635
8
+editor.fx.0.y=23
9
+objectbench.height=101
10
+objectbench.width=636
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.8361153262518968
13
+package.editor.height=544
14
+package.editor.width=534
15
+package.editor.x=183
16
+package.editor.y=23
17
+package.frame.height=717
18
+package.frame.width=660
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=160
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=130
41
+target2.x=70
42
+target2.y=70