Browse Source

completed lab

Michelle DiMarino 6 years ago
parent
commit
16b782f77e
1 changed files with 101 additions and 12 deletions
  1. 101
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 101
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java View File

@@ -3,13 +3,14 @@ package com.zipcodewilmington;
3 3
 /**
4 4
  * Created by leon on 1/29/18.
5 5
  */
6
+import java.util.ArrayList;
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
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,7 +43,14 @@ 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
+        boolean containsValue = true;
47
+        for (int i = 0; i < array.length; i++){
48
+            if (array[i].equals(value)) {
49
+                containsValue = true;
50
+
51
+            }
52
+        }
53
+        return containsValue;
46 54
     }
47 55
 
48 56
     /**
@@ -50,7 +58,14 @@ public class StringArrayUtils {
50 58
      * @return an array with identical contents in reverse order
51 59
      */ // TODO
52 60
     public static String[] reverse(String[] array) {
53
-        return null;
61
+        String reverseArray[] = new String[array.length];
62
+        int j = 0;
63
+        for (int i = array.length-1; i >=0; i--) {
64
+            String arrayElement = array[i];
65
+            reverseArray[j] = arrayElement;
66
+            j++;
67
+        }
68
+        return reverseArray;
54 69
     }
55 70
 
56 71
     /**
@@ -58,7 +73,15 @@ public class StringArrayUtils {
58 73
      * @return true if the order of the array is the same backwards and forwards
59 74
      */ // TODO
60 75
     public static boolean isPalindromic(String[] array) {
61
-        return false;
76
+       String[] checkReverse = reverse(array);
77
+       boolean isPalindromic = false;
78
+       for (int i = 0; i < array.length; i++) {
79
+           if (checkReverse[i] == array[i]) {
80
+               isPalindromic =true;
81
+           } else {
82
+               isPalindromic = false;
83
+           }
84
+       }return isPalindromic;
62 85
     }
63 86
 
64 87
     /**
@@ -66,7 +89,21 @@ public class StringArrayUtils {
66 89
      * @return true if each letter in the alphabet has been used in the array
67 90
      */ // TODO
68 91
     public static boolean isPangramic(String[] array) {
69
-        return false;
92
+       StringBuilder arrayString = new StringBuilder();
93
+       boolean isPangramic = true;
94
+        for (int i = 0; i < array.length; i++) {
95
+            arrayString.append(array[i]);
96
+            String newString = arrayString.toString().toLowerCase();
97
+
98
+            for (char c = 'a'; c <= 'z'; c++) {
99
+                if (newString.indexOf(c) == -1) {
100
+                    isPangramic = false;
101
+                }else {
102
+                    isPangramic = true;
103
+                }
104
+            }
105
+        } return isPangramic;
106
+
70 107
     }
71 108
 
72 109
     /**
@@ -75,7 +112,13 @@ public class StringArrayUtils {
75 112
      * @return number of occurrences the specified `value` has occurred
76 113
      */ // TODO
77 114
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
115
+        int counter = 0;
116
+        for (int i = 0; i < array.length; i++){
117
+            if (array[i].equals(value)) {
118
+                counter++;
119
+            }
120
+        }
121
+        return counter;
79 122
     }
80 123
 
81 124
     /**
@@ -84,7 +127,21 @@ public class StringArrayUtils {
84 127
      * @return array with identical contents excluding values of `value`
85 128
      */ // TODO
86 129
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
130
+        ArrayList <String> newArray = new ArrayList();
131
+        int i =0;
132
+        while (i<array.length){
133
+            if (array[i].equals(valueToRemove)){
134
+                i++;
135
+            }else{
136
+                newArray.add(array[i]);
137
+                i++;
138
+            }
139
+
140
+        }
141
+        int size = newArray.size();
142
+        String newArray2[] = new String[size];
143
+        newArray.toArray(newArray2);
144
+        return newArray2;
88 145
     }
89 146
 
90 147
     /**
@@ -92,7 +149,20 @@ public class StringArrayUtils {
92 149
      * @return array of Strings with consecutive duplicates removes
93 150
      */ // TODO
94 151
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
152
+        ArrayList <String> allDups = new ArrayList();
153
+        int i = 0;
154
+        while (i < array.length){
155
+            if (i+1 < array.length && array[i].equals(array[i+1])){
156
+              i++;
157
+            }else{
158
+                allDups.add(array[i]);
159
+                i++;
160
+            }
161
+        }
162
+        int size = allDups.size();
163
+        String newArray2[] = new String[size];
164
+        allDups.toArray(newArray2);
165
+        return newArray2;
96 166
     }
97 167
 
98 168
     /**
@@ -100,7 +170,26 @@ public class StringArrayUtils {
100 170
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 171
      */ // TODO
102 172
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
173
+        ArrayList <String> allDups = new ArrayList();
174
+        int i = 0;
175
+        while (i < array.length) {
176
+            if (i>0 && array[i].equals(array[i - 1])) {
177
+                String addString = allDups.get(allDups.size() - 1);
178
+                addString += array[i];
179
+                allDups.set(allDups.size() -1 , addString);
180
+                i++;
181
+            } else{
182
+                allDups.add(array[i]);
183
+                i++;
184
+            }
185
+
186
+        }
187
+
188
+        int size = allDups.size();
189
+        String newArray2[] = new String[size];
190
+        allDups.toArray(newArray2);
191
+        return newArray2;
192
+
104 193
     }
105 194
 
106 195