Explorar el Código

all but last 2

Eric Foster hace 8 años
padre
commit
00f36a62c1
Se han modificado 2 ficheros con 111 adiciones y 13 borrados
  1. 69
    13
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 69
- 13
StringArrayUtils.java Ver fichero

1
- 
1
+import java.util.Arrays;
2
+import java.io.*;
3
+import java.util.*; 
2
 
4
 
3
 /**
5
 /**
4
  * Created by leon on 1/29/18.
6
  * Created by leon on 1/29/18.
9
      * @return first element of specified array
11
      * @return first element of specified array
10
      */ // TODO
12
      */ // TODO
11
     public static String getFirstElement(String[] array) {
13
     public static String getFirstElement(String[] array) {
12
-        return null;
14
+        return array[0];
13
     }
15
     }
14
 
16
 
15
     /**
17
     /**
17
      * @return second element in specified array
19
      * @return second element in specified array
18
      */
20
      */
19
     public static String getSecondElement(String[] array) {
21
     public static String getSecondElement(String[] array) {
20
-        return null;
22
+        return array[1];
21
     }
23
     }
22
 
24
 
23
     /**
25
     /**
25
      * @return last element in specified array
27
      * @return last element in specified array
26
      */ // TODO
28
      */ // TODO
27
     public static String getLastElement(String[] array) {
29
     public static String getLastElement(String[] array) {
28
-        return null;
30
+        return array[array.length - 1];
29
     }
31
     }
30
 
32
 
31
     /**
33
     /**
33
      * @return second to last element in specified array
35
      * @return second to last element in specified array
34
      */ // TODO
36
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
37
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
38
+        return array[array.length - 2];
37
     }
39
     }
38
 
40
 
39
     /**
41
     /**
42
      * @return true if the array contains the specified `value`
44
      * @return true if the array contains the specified `value`
43
      */ // TODO
45
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
46
     public static boolean contains(String[] array, String value) {
45
-        return false;
47
+        boolean contains = false;
48
+        for(String element : array){
49
+            if(element.equals(value)){
50
+                contains = true;
51
+            }
52
+        }
53
+        return contains;
46
     }
54
     }
47
 
55
 
48
     /**
56
     /**
50
      * @return an array with identical contents in reverse order
58
      * @return an array with identical contents in reverse order
51
      */ // TODO
59
      */ // TODO
52
     public static String[] reverse(String[] array) {
60
     public static String[] reverse(String[] array) {
53
-        return null;
61
+        String[] reverse = new String[array.length];
62
+        for (int j=array.length-1, i=0; j>=0; j--, i++){    
63
+            reverse[i] = array[j];
64
+        }
65
+        return reverse;
54
     }
66
     }
55
 
67
 
56
     /**
68
     /**
58
      * @return true if the order of the array is the same backwards and forwards
70
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
71
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
72
     public static boolean isPalindromic(String[] array) {
61
-        return false;
73
+        boolean isPalindromic = false;
74
+        String[] reverse = reverse(array);
75
+        if (Arrays.equals(array,reverse)){
76
+            isPalindromic = true;
77
+        }
78
+        return isPalindromic;
62
     }
79
     }
63
 
80
 
64
     /**
81
     /**
66
      * @return true if each letter in the alphabet has been used in the array
83
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
84
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
85
     public static boolean isPangramic(String[] array) {
69
-        return false;
86
+        String arrayString = Arrays.toString(array).toLowerCase();
87
+        
88
+        if(arrayString.length() < 26){
89
+            return false;
90
+        }
91
+
92
+        for (char c = 'a'; c <='z'; c++){
93
+            if(arrayString.indexOf(c) < 0){
94
+                return false;
95
+            }
96
+        }
97
+        
98
+        return true;
70
     }
99
     }
71
 
100
 
72
     /**
101
     /**
75
      * @return number of occurrences the specified `value` has occurred
104
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
105
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
106
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
107
+        int numberOfOccurrences = 0;
108
+
109
+        for (String element : array){
110
+            if(element.equals(value)){
111
+                numberOfOccurrences += 1;
112
+            }
113
+        }
114
+        
115
+        return numberOfOccurrences;
79
     }
116
     }
80
 
117
 
81
     /**
118
     /**
84
      * @return array with identical contents excluding values of `value`
121
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
122
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
123
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
124
+        int numberOfOccurrences = getNumberOfOccurrences(array, valueToRemove);
125
+        String[] newArray = new String[array.length - numberOfOccurrences];
126
+        
127
+        int i = 0;
128
+        for(String element : array){
129
+            if(!element.equals(valueToRemove)){
130
+                newArray[i] = element;
131
+                i++;
132
+            }
133
+        }
134
+        return newArray;
135
+    }
136
+    
137
+    public static int numberOfConsecutiveDuplicates(String[] array) {
138
+        int numberOfConsecDups = 0;
139
+        for(int i=1; i<=array.length-1; i++){
140
+            if(array[i].equals(array[i-1])){
141
+                numberOfConsecDups += 1;
142
+            }
143
+        }
144
+        return numberOfConsecDups;
88
     }
145
     }
89
 
146
 
90
     /**
147
     /**
92
      * @return array of Strings with consecutive duplicates removes
149
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
150
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
151
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
152
+        
96
     }
153
     }
97
 
154
 
98
     /**
155
     /**
103
         return null;
160
         return null;
104
     }
161
     }
105
 
162
 
106
-
107
 }
163
 }

+ 42
- 0
package.bluej Ver fichero

1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=713
6
+editor.fx.0.width=800
7
+editor.fx.0.x=466
8
+editor.fx.0.y=35
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=202
16
+package.editor.y=149
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