Bladeren bron

finishing up

Ryan Small 8 jaren geleden
bovenliggende
commit
ef0c6f8532
3 gewijzigde bestanden met toevoegingen van 257 en 12 verwijderingen
  1. 60
    12
      StringArrayUtils.java
  2. 155
    0
      StringArrayUtils.java#
  3. 42
    0
      package.bluej

+ 60
- 12
StringArrayUtils.java Bestand weergeven

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

+ 155
- 0
StringArrayUtils.java# Bestand weergeven

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

+ 42
- 0
package.bluej Bestand weergeven

@@ -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=40
8
+editor.fx.0.y=120
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=31
16
+package.editor.y=75
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=70
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