shakila před 8 roky
rodič
revize
95764b6b2e
2 změnil soubory, kde provedl 134 přidání a 22 odebrání
  1. 92
    22
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 92
- 22
StringArrayUtils.java Zobrazit soubor

@@ -1,5 +1,8 @@
1
- 
2
-
1
+import java.util.ArrayList;
2
+import java.util.Arrays;
3
+import java.util.Collections;
4
+import java.util.List;
5
+import java.util.*;
3 6
 /**
4 7
  * Created by leon on 1/29/18.
5 8
  */
@@ -7,58 +10,83 @@ public class StringArrayUtils {
7 10
     /**
8 11
      * @param array array of String objects
9 12
      * @return first element of specified array
10
-     */ // TODO
13
+     */ // DONE
11 14
     public static String getFirstElement(String[] array) {
12
-        return null;
15
+
16
+        return array[0];
13 17
     }
14 18
 
15 19
     /**
16 20
      * @param array array of String objects
17 21
      * @return second element in specified array
22
+     * DONE
18 23
      */
19 24
     public static String getSecondElement(String[] array) {
20
-        return null;
25
+        return array[1];
21 26
     }
22 27
 
23 28
     /**
24 29
      * @param array array of String objects
25 30
      * @return last element in specified array
26
-     */ // TODO
31
+     */ // DONE
27 32
     public static String getLastElement(String[] array) {
28
-        return null;
33
+        return array[array.length-1];
29 34
     }
30 35
 
31 36
     /**
32 37
      * @param array array of String objects
33 38
      * @return second to last element in specified array
34
-     */ // TODO
39
+     */ // DONE
35 40
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
41
+        return array[array.length-2];
37 42
     }
38 43
 
39 44
     /**
40 45
      * @param array array of String objects
41 46
      * @param value value to check array for
42 47
      * @return true if the array contains the specified `value`
43
-     */ // TODO
48
+     */ // DONE
44 49
     public static boolean contains(String[] array, String value) {
45
-        return false;
50
+        boolean check = false;
51
+
52
+        for (int i = 0; i<array.length; i++){
53
+            if (array[i] == value){
54
+                check = true;}
55
+        }
56
+        return check;
46 57
     }
47 58
 
48 59
     /**
49 60
      * @param array of String objects
50 61
      * @return an array with identical contents in reverse order
51
-     */ // TODO
62
+     */ // DONE
52 63
     public static String[] reverse(String[] array) {
53
-        return null;
64
+
65
+        for (int i = 0; i<array.length/2; i++){
66
+            String temp = array[i];
67
+            array[i] = array[array.length-1-i];
68
+            array[array.length-1-i] = temp;
69
+
70
+        }   
71
+        return array;
54 72
     }
55 73
 
56 74
     /**
57 75
      * @param array array of String objects
58 76
      * @return true if the order of the array is the same backwards and forwards
59
-     */ // TODO
77
+     */ // DONE
60 78
     public static boolean isPalindromic(String[] array) {
61
-        return false;
79
+        boolean check = false;
80
+        for (int i=0; i<array.length/2; i++){
81
+            String temp = array[i];
82
+            array[i] = array[array.length-1-i];
83
+            array[array.length-1-i] = temp;
84
+
85
+            if ( temp == array[i]){
86
+                check = true;
87
+            }
88
+        }
89
+        return check;
62 90
     }
63 91
 
64 92
     /**
@@ -66,25 +94,60 @@ public class StringArrayUtils {
66 94
      * @return true if each letter in the alphabet has been used in the array
67 95
      */ // TODO
68 96
     public static boolean isPangramic(String[] array) {
69
-        return false;
97
+        boolean check = false;
98
+        //Scanner reader = new Scanner();
99
+        String string = new String();//reader.nextLine();
100
+        Set character = new HashSet();
101
+        Set set = new HashSet();
102
+        for (char c:string.toUpperCase().toCharArray()){
103
+            
104
+            if (character.equals(c)){
105
+                set.add(c);}
106
+        }
107
+        if (set.size() == 26){
108
+            check = true;}
109
+            else {
110
+                check = false;}
111
+        return check;
112
+        
70 113
     }
71 114
 
72 115
     /**
73 116
      * @param array array of String objects
74 117
      * @param value value to check array for
75 118
      * @return number of occurrences the specified `value` has occurred
76
-     */ // TODO
119
+     */ // Done
77 120
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
121
+        int counter = 0;
122
+        for (int i = 0; i<array.length; i++){
123
+            if (array[i] == array[i+1]){
124
+                counter=counter+1;}
125
+                
126
+            }
127
+        return counter;
79 128
     }
80 129
 
81 130
     /**
82 131
      * @param array         array of String objects
83 132
      * @param valueToRemove value to remove from array
84 133
      * @return array with identical contents excluding values of `value`
85
-     */ // TODO
134
+     */ // Done
86 135
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
136
+        //sort array, create new array, compare i, add new i to new array.
137
+        Arrays.sort(array);
138
+        ArrayList fresh = new ArrayList();
139
+        for (int i=0;i<array.length;i++){
140
+            if (array[i] != valueToRemove){
141
+                fresh.add(array[i]);
142
+            }
143
+            
144
+        }
145
+        String[] list = (String[])(fresh.toArray()); //needed to convert to a string array, not a string
146
+        return list;
147
+        
148
+        //String delete = value.replaceAll (Character.toString(charToRemove), "");
149
+       //return delete;  
150
+        //return list.toString();
88 151
     }
89 152
 
90 153
     /**
@@ -92,7 +155,15 @@ public class StringArrayUtils {
92 155
      * @return array of Strings with consecutive duplicates removes
93 156
      */ // TODO
94 157
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
158
+        Arrays.sort(array);
159
+        ArrayList fresh = new ArrayList();
160
+        for (int i=0; i<array.length; i++){
161
+         if (array[i] != array[i-1] && array[i] != array[i+1]) {
162
+            fresh.add(array[i]);
163
+            }
164
+        }
165
+        String[] list = (String[])(fresh.toArray());
166
+        return list;
96 167
     }
97 168
 
98 169
     /**
@@ -103,5 +174,4 @@ public class StringArrayUtils {
103 174
         return null;
104 175
     }
105 176
 
106
-
107 177
 }

+ 42
- 0
package.bluej Zobrazit soubor

@@ -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=714
6
+editor.fx.0.width=930
7
+editor.fx.0.x=310
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=255
16
+package.editor.y=130
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