Aleena Rose-Mathew 8 lat temu
rodzic
commit
ea56b1080a
2 zmienionych plików z 182 dodań i 14 usunięć
  1. 140
    14
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 140
- 14
StringArrayUtils.java Wyświetl plik

@@ -1,15 +1,15 @@
1
- 
2 1
 
3 2
 /**
4 3
  * Created by leon on 1/29/18.
5 4
  */
5
+import java.util.*;
6 6
 public class StringArrayUtils {
7 7
     /**
8 8
      * @param array array of String objects
9 9
      * @return first element of specified array
10 10
      */ // TODO
11 11
     public static String getFirstElement(String[] array) {
12
-        return null;
12
+        return array[0];
13 13
     }
14 14
 
15 15
     /**
@@ -17,7 +17,7 @@ public class StringArrayUtils {
17 17
      * @return second element in specified array
18 18
      */
19 19
     public static String getSecondElement(String[] array) {
20
-        return null;
20
+        return array[1];
21 21
     }
22 22
 
23 23
     /**
@@ -25,7 +25,8 @@ public class StringArrayUtils {
25 25
      * @return last element in specified array
26 26
      */ // TODO
27 27
     public static String getLastElement(String[] array) {
28
-        return null;
28
+        int last_index=array.length-1;
29
+        return array[last_index];
29 30
     }
30 31
 
31 32
     /**
@@ -33,7 +34,9 @@ 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
+
38
+        int last_index=array.length-2;
39
+        return array[last_index];
37 40
     }
38 41
 
39 42
     /**
@@ -42,7 +45,15 @@ public class StringArrayUtils {
42 45
      * @return true if the array contains the specified `value`
43 46
      */ // TODO
44 47
     public static boolean contains(String[] array, String value) {
45
-        return false;
48
+        Boolean b=false;
49
+        for(int i=0;i<array.length;i++)
50
+        {
51
+            if(array[i].equals(value))
52
+            {
53
+                b=true;
54
+            }
55
+        }
56
+        return b;
46 57
     }
47 58
 
48 59
     /**
@@ -50,7 +61,17 @@ public class StringArrayUtils {
50 61
      * @return an array with identical contents in reverse order
51 62
      */ // TODO
52 63
     public static String[] reverse(String[] array) {
53
-        return null;
64
+        String[] str=new String[array.length];
65
+        int k=0;
66
+        for(int i=array.length-1;i>=0;i--)
67
+        {
68
+
69
+            str[k]=array[i];
70
+            k++;
71
+
72
+        }
73
+        //System.out.println((str));
74
+        return str;
54 75
     }
55 76
 
56 77
     /**
@@ -58,7 +79,20 @@ public class StringArrayUtils {
58 79
      * @return true if the order of the array is the same backwards and forwards
59 80
      */ // TODO
60 81
     public static boolean isPalindromic(String[] array) {
61
-        return false;
82
+
83
+        String[] reversed=new String[array.length];
84
+        reversed=reverse(array);
85
+        boolean flag=true;
86
+        for( int i=0;i<reversed.length;i++)
87
+        {
88
+            if(!array[i].equals(reversed[i]))
89
+            {
90
+                flag=false;
91
+                break;
92
+
93
+            }
94
+        }
95
+        return flag;
62 96
     }
63 97
 
64 98
     /**
@@ -66,7 +100,44 @@ public class StringArrayUtils {
66 100
      * @return true if each letter in the alphabet has been used in the array
67 101
      */ // TODO
68 102
     public static boolean isPangramic(String[] array) {
69
-        return false;
103
+        ArrayList<Character> albha = new ArrayList<Character>();
104
+        String str="";
105
+        int count =0;
106
+        boolean result=false;
107
+        for(int i=0;i<array.length;i++)
108
+        {
109
+            str=str+array[i];
110
+        }
111
+        System.out.println(str);
112
+        str=str.toLowerCase();
113
+        for(int i=0;i<str.length();i++)
114
+        {
115
+            int k=str.charAt(i);
116
+            if(k>=65&&k<=90 || k>=97&&k<=122)
117
+            {
118
+               
119
+                if(!albha.contains(str.charAt(i)))
120
+                {
121
+
122
+                    albha.add(str.charAt(i));
123
+
124
+                    count ++;
125
+                }
126
+            }
127
+        }
128
+        
129
+        System.out.println(count);
130
+        System.out.println(albha);
131
+        
132
+        if(count==26)
133
+        {
134
+            result=true;
135
+        }
136
+        else
137
+        {
138
+            result=false;
139
+        }
140
+        return result;
70 141
     }
71 142
 
72 143
     /**
@@ -75,7 +146,15 @@ public class StringArrayUtils {
75 146
      * @return number of occurrences the specified `value` has occurred
76 147
      */ // TODO
77 148
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
149
+        int k=0;
150
+        for(int i=0;i<array.length;i++)
151
+        {
152
+            if(value.equals(array[i].toString()))
153
+            {
154
+                k++;
155
+            }
156
+        }
157
+        return k;
79 158
     }
80 159
 
81 160
     /**
@@ -84,7 +163,21 @@ public class StringArrayUtils {
84 163
      * @return array with identical contents excluding values of `value`
85 164
      */ // TODO
86 165
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
166
+        int k=0;
167
+        int n=getNumberOfOccurrences(array,valueToRemove);
168
+        String[] newArray= new String[array.length-n];
169
+        for(int i=0;i<array.length;i++)
170
+        {
171
+            if(!array[i].equals(valueToRemove))
172
+            {
173
+                newArray[k]=array[i];
174
+                k++;
175
+
176
+            }
177
+
178
+        }
179
+        System.out.println(Arrays.toString(array));
180
+        return newArray;
88 181
     }
89 182
 
90 183
     /**
@@ -92,7 +185,20 @@ public class StringArrayUtils {
92 185
      * @return array of Strings with consecutive duplicates removes
93 186
      */ // TODO
94 187
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
188
+        int k=0;
189
+        
190
+        ArrayList<String> resultList = new ArrayList<String>();
191
+        resultList.add(array[0]);
192
+        for(int i = 1; i < array.length; i++) {
193
+
194
+            if(!array[i-1].equals(array[i])) {
195
+                resultList.add(array[i]);
196
+            }
197
+
198
+        }
199
+        String[] resultArray = resultList.toArray(new String[resultList.size()]);
200
+
201
+        return resultArray;
96 202
     }
97 203
 
98 204
     /**
@@ -100,8 +206,28 @@ public class StringArrayUtils {
100 206
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 207
      */ // TODO
102 208
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
209
+        ArrayList<String> list = new ArrayList<>();
210
+        list.add(array[0]);
211
+        int k=0;
212
+
213
+        for(int i = 1 ; i < array.length ; i++) {
214
+
215
+            if(array[i-1].equalsIgnoreCase(array[i])){
105 216
 
217
+                String lastElement = list.get(list.size()-1);
218
+
219
+                lastElement = lastElement.concat(array[i]);
220
+
221
+                list.set(list.size()-1,lastElement);
222
+            }
223
+            else{
224
+                list.add(array[i]);
225
+            }
226
+        }
227
+
228
+        String[] newArray = list.toArray(new String[list.size()]);
229
+        return newArray;
230
+
231
+    }
106 232
 
107 233
 }

+ 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=729
6
+editor.fx.0.width=1280
7
+editor.fx.0.x=0
8
+editor.fx.0.y=23
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=353
16
+package.editor.y=162
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