#29 alizalang

Отворено
alizalang жели да споји 1 комит(е) из alizalang/ZCW-Arrays-StringArrayUtilities-BlueJ:master у master
2 измењених фајлова са 134 додато и 20 уклоњено
  1. 92
    20
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 92
- 20
StringArrayUtils.java Прегледај датотеку

1
- 
1
+import java.util.Arrays;
2
 
2
 
3
-/**
4
- * Created by leon on 1/29/18.
5
- */
6
 public class StringArrayUtils {
3
 public class StringArrayUtils {
7
     /**
4
     /**
8
      * @param array array of String objects
5
      * @param array array of String objects
9
      * @return first element of specified array
6
      * @return first element of specified array
10
      */ // TODO
7
      */ // TODO
11
     public static String getFirstElement(String[] array) {
8
     public static String getFirstElement(String[] array) {
12
-        return null;
9
+        return array[0];
13
     }
10
     }
14
 
11
 
15
     /**
12
     /**
17
      * @return second element in specified array
14
      * @return second element in specified array
18
      */
15
      */
19
     public static String getSecondElement(String[] array) {
16
     public static String getSecondElement(String[] array) {
20
-        return null;
17
+        return array[1];
21
     }
18
     }
22
 
19
 
23
     /**
20
     /**
25
      * @return last element in specified array
22
      * @return last element in specified array
26
      */ // TODO
23
      */ // TODO
27
     public static String getLastElement(String[] array) {
24
     public static String getLastElement(String[] array) {
28
-        return null;
25
+        return array[array.length-1];
29
     }
26
     }
30
 
27
 
31
     /**
28
     /**
33
      * @return second to last element in specified array
30
      * @return second to last element in specified array
34
      */ // TODO
31
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
32
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
33
+        return array[array.length-2];
37
     }
34
     }
38
 
35
 
39
     /**
36
     /**
42
      * @return true if the array contains the specified `value`
39
      * @return true if the array contains the specified `value`
43
      */ // TODO
40
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
      * @return an array with identical contents in reverse order
52
      * @return an array with identical contents in reverse order
51
      */ // TODO
53
      */ // TODO
52
     public static String[] reverse(String[] array) {
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
      * @return true if the order of the array is the same backwards and forwards
66
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
67
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
      * @param array array of String objects
77
      * @param array array of String objects
66
      * @return true if each letter in the alphabet has been used in the array
78
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
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
      * @return number of occurrences the specified `value` has occurred
103
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
104
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
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
      * @return array with identical contents excluding values of `value`
117
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
118
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
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
      * @param array array of chars
132
      * @param array array of chars
92
      * @return array of Strings with consecutive duplicates removes
133
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
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
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
159
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
160
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
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 Прегледај датотеку

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