#29 alizalang

Öppen
alizalang vill sammanfoga 1 incheckningar från s[2]s in i master
2 ändrade filer med 134 tillägg och 20 borttagningar
  1. 92
    20
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 92
- 20
StringArrayUtils.java Visa fil

@@ -1,15 +1,12 @@
1
- 
1
+import java.util.Arrays;
2 2
 
3
-/**
4
- * Created by leon on 1/29/18.
5
- */
6 3
 public class StringArrayUtils {
7 4
     /**
8 5
      * @param array array of String objects
9 6
      * @return first element of specified array
10 7
      */ // TODO
11 8
     public static String getFirstElement(String[] array) {
12
-        return null;
9
+        return array[0];
13 10
     }
14 11
 
15 12
     /**
@@ -17,7 +14,7 @@ public class StringArrayUtils {
17 14
      * @return second element in specified array
18 15
      */
19 16
     public static String getSecondElement(String[] array) {
20
-        return null;
17
+        return array[1];
21 18
     }
22 19
 
23 20
     /**
@@ -25,7 +22,7 @@ public class StringArrayUtils {
25 22
      * @return last element in specified array
26 23
      */ // TODO
27 24
     public static String getLastElement(String[] array) {
28
-        return null;
25
+        return array[array.length-1];
29 26
     }
30 27
 
31 28
     /**
@@ -33,7 +30,7 @@ public class StringArrayUtils {
33 30
      * @return second to last element in specified array
34 31
      */ // TODO
35 32
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
33
+        return array[array.length-2];
37 34
     }
38 35
 
39 36
     /**
@@ -42,7 +39,12 @@ public class StringArrayUtils {
42 39
      * @return true if the array contains the specified `value`
43 40
      */ // TODO
44 41
     public static boolean contains(String[] array, String value) {
45
-        return false;
42
+        boolean answer = false;
43
+        for (String x: array){
44
+            if (x.equals(value))
45
+                answer = true;
46
+        }
47
+        return answer;
46 48
     }
47 49
 
48 50
     /**
@@ -50,7 +52,13 @@ public class StringArrayUtils {
50 52
      * @return an array with identical contents in reverse order
51 53
      */ // TODO
52 54
     public static String[] reverse(String[] array) {
53
-        return null;
55
+        String [] str = new String [array.length];
56
+        int count = 0;
57
+        for (int i = array.length-1; i>=0; i--){
58
+            str[count] = array[i];
59
+            count=count+1;
60
+        }
61
+        return str;
54 62
     }
55 63
 
56 64
     /**
@@ -58,15 +66,35 @@ 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
+        String [] newArray = reverse(array);
70
+        if (Arrays.equals(newArray, array)) 
71
+            return true;
72
+        else
73
+            return false;
62 74
     }
63 75
 
64 76
     /**
65 77
      * @param array array of String objects
66 78
      * @return true if each letter in the alphabet has been used in the array
67 79
      */ // TODO
68
-    public static boolean isPangramic(String[] array) {
69
-        return false;
80
+    public static boolean isPangramic(String[] array) {        
81
+
82
+        boolean flag = false;
83
+        String [] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
84
+
85
+        StringBuilder sb = new StringBuilder();
86
+        for (String element: array)
87
+            sb.append(element);
88
+        String str = sb.toString();
89
+
90
+        for (String letter : alphabet){
91
+            if (str.contains(letter))
92
+                flag = true;           
93
+            else
94
+                flag = false;
95
+        }
96
+        return flag;
97
+
70 98
     }
71 99
 
72 100
     /**
@@ -75,7 +103,12 @@ public class StringArrayUtils {
75 103
      * @return number of occurrences the specified `value` has occurred
76 104
      */ // TODO
77 105
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
106
+        int numberOfOccurences = 0;
107
+        for (int i = 0; i <=array.length-1; i++){
108
+            if (array[i]==value)
109
+                numberOfOccurences+=1;  
110
+        }
111
+        return numberOfOccurences;             
79 112
     }
80 113
 
81 114
     /**
@@ -84,15 +117,41 @@ public class StringArrayUtils {
84 117
      * @return array with identical contents excluding values of `value`
85 118
      */ // TODO
86 119
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
120
+        String [] strWithoutValue = new String [array.length-1];
121
+        int count = 0;
122
+        for (int i = 0; i < array.length; i++){
123
+            if (!array[i].equals(valueToRemove)){
124
+                strWithoutValue[count]=array[i];
125
+                count += 1;
126
+            }
127
+        }
128
+        return strWithoutValue;         
88 129
     }
89 130
 
90 131
     /**
91 132
      * @param array array of chars
92 133
      * @return array of Strings with consecutive duplicates removes
93 134
      */ // TODO
94
-    public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
135
+    public static String[] removeConsecutiveDuplicates(String[] array) {        
136
+        String [] newArray = new String[array.length];
137
+
138
+        int positionOfNewArray = 0;
139
+
140
+        for (int i = 0; i <= array.length-2; i++){
141
+            if (!array[i].equals(array[i+1])){
142
+                newArray[positionOfNewArray] = array[i];
143
+                positionOfNewArray+=1;
144
+            }
145
+        }
146
+
147
+        newArray[positionOfNewArray] = array[array.length-1];
148
+
149
+        String [] finalArray = new String [positionOfNewArray+1];
150
+
151
+        for (int i = 0; i< positionOfNewArray+1; i++){
152
+            finalArray[i] = newArray[i];
153
+        }
154
+        return finalArray;
96 155
     }
97 156
 
98 157
     /**
@@ -100,8 +159,21 @@ public class StringArrayUtils {
100 159
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 160
      */ // TODO
102 161
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105 162
 
163
+        StringBuilder sb = new StringBuilder();
164
+
165
+        for (int i = 0; i < array.length-1; i++){
166
+            if (array[i].equals(array[i+1]))
167
+                sb.append(array[i]);
168
+            else {
169
+                sb.append(array[i]);
170
+                sb.append(" ");
171
+            }
172
+        }
173
+        sb.append(array[array.length-1]);
106 174
 
107
-}
175
+        String str = sb.toString();
176
+        String [] newArray = str.split(" ");
177
+        return newArray;
178
+    }
179
+}

+ 42
- 0
package.bluej Visa fil

@@ -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=320
8
+editor.fx.0.y=51
9
+objectbench.height=102
10
+objectbench.width=461
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.7988929889298892
13
+package.editor.height=426
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