Bladeren bron

All but two

rayskeez21 8 jaren geleden
bovenliggende
commit
dfb2cda5df
2 gewijzigde bestanden met toevoegingen van 78 en 8 verwijderingen
  1. 36
    8
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 36
- 8
StringArrayUtils.java Bestand weergeven

1
- 
2
 
1
 
2
+import java.util.Arrays;
3
 /**
3
 /**
4
  * Created by leon on 1/29/18.
4
  * Created by leon on 1/29/18.
5
  */
5
  */
9
      * @return first element of specified array
9
      * @return first element of specified array
10
      */ // TODO
10
      */ // TODO
11
     public static String getFirstElement(String[] array) {
11
     public static String getFirstElement(String[] array) {
12
-        return null;
12
+        return array[0];
13
     }
13
     }
14
 
14
 
15
     /**
15
     /**
17
      * @return second element in specified array
17
      * @return second element in specified array
18
      */
18
      */
19
     public static String getSecondElement(String[] array) {
19
     public static String getSecondElement(String[] array) {
20
-        return null;
20
+        return array[1];
21
     }
21
     }
22
 
22
 
23
     /**
23
     /**
25
      * @return last element in specified array
25
      * @return last element in specified array
26
      */ // TODO
26
      */ // TODO
27
     public static String getLastElement(String[] array) {
27
     public static String getLastElement(String[] array) {
28
-        return null;
28
+        return array[array.length - 1];
29
     }
29
     }
30
 
30
 
31
     /**
31
     /**
33
      * @return second to last element in specified array
33
      * @return second to last element in specified array
34
      */ // TODO
34
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
35
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
36
+        return array[array.length - 2];
37
     }
37
     }
38
 
38
 
39
     /**
39
     /**
42
      * @return true if the array contains the specified `value`
42
      * @return true if the array contains the specified `value`
43
      */ // TODO
43
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
44
     public static boolean contains(String[] array, String value) {
45
+        for (String n : array){
46
+            if(value == n){
47
+                return true;
48
+            }
49
+        }
45
         return false;
50
         return false;
46
     }
51
     }
47
 
52
 
50
      * @return an array with identical contents in reverse order
55
      * @return an array with identical contents in reverse order
51
      */ // TODO
56
      */ // TODO
52
     public static String[] reverse(String[] array) {
57
     public static String[] reverse(String[] array) {
53
-        return null;
58
+
59
+        for(int i = 0; i < array.length/2; i++){
60
+            String cheese = array[i]; 
61
+            array[i]= array[array.length - i - 1];
62
+            array[array.length - i - 1] = cheese;
63
+        }
64
+        return array;
54
     }
65
     }
55
 
66
 
56
     /**
67
     /**
58
      * @return true if the order of the array is the same backwards and forwards
69
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
70
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
71
     public static boolean isPalindromic(String[] array) {
72
+
73
+        for(int i = 0; i < array.length; i++){
74
+            if (array[i] == array[array.length - i - 1]){
75
+                return true;
76
+            }
77
+        }
61
         return false;
78
         return false;
62
     }
79
     }
63
 
80
 
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) {
86
+
87
+        for(char alphabet = 'a'; alphabet <= 'z'; alphabet++){
88
+            if(alphabet >= 'a' && alphabet <= 'z'){
89
+                return true;
90
+            }
91
+        }
69
         return false;
92
         return false;
70
     }
93
     }
71
 
94
 
75
      * @return number of occurrences the specified `value` has occurred
98
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
99
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
100
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
101
+        int count = 0;
102
+        for(String word: array){
103
+            if(value.equals(word)){
104
+                count++;
105
+            }
106
+        }
107
+        return count;
79
     }
108
     }
80
 
109
 
81
     /**
110
     /**
103
         return null;
132
         return null;
104
     }
133
     }
105
 
134
 
106
-
107
 }
135
 }

+ 42
- 0
package.bluej Bestand weergeven

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=240
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=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