Kaynağa Gözat

working on generics

jacob andersen 5 yıl önce
ebeveyn
işleme
18337b2c84

+ 24
- 6
src/main/java/rocks/zipcode/io/quiz3/generics/ArrayUtility.java Dosyayı Görüntüle

@@ -1,5 +1,6 @@
1 1
 package rocks.zipcode.io.quiz3.generics;
2 2
 
3
+import java.util.Collections;
3 4
 import java.util.function.Function;
4 5
 
5 6
 /**
@@ -8,23 +9,40 @@ import java.util.function.Function;
8 9
 public class ArrayUtility<SomeType> {
9 10
     private final SomeType[] array;
10 11
 
11
-    public ArrayUtility(SomeType[] array) {
12
+    public ArrayUtility(SomeType[] array)
13
+    {
12 14
         this.array = array;
13 15
     }
14 16
 
15 17
     public SomeType findOddOccurringValue()
18
+
16 19
     {
17
-     return null;
20
+        Integer i;
21
+        Integer result = 0;
22
+        for (i = 0; i < array.length; i++)
23
+        {
24
+            result = result ^ (Integer)array[i];
25
+        }
26
+        return (SomeType) result;
18 27
     }
19 28
 
20 29
 
21
-    public SomeType findEvenOccurringValue() {
30
+    public SomeType findEvenOccurringValue()
31
+    {
22 32
         return null;
23 33
     }
24 34
 
25
-    public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
26
-
27
-        return null;
35
+    public Integer getNumberOfOccurrences(SomeType valueToEvaluate)
36
+    {
37
+        Integer count=0;
38
+        for (int i=0;i<this.array.length;i++)
39
+        {
40
+         if(valueToEvaluate.equals(array[i]))
41
+         {
42
+             count++;
43
+         }
44
+        }
45
+        return count;
28 46
     }
29 47
 
30 48
     public SomeType[] filter(Function<SomeType, Boolean> predicate) {

+ 1
- 1
src/test/java/rocks/zipcode/io/quiz3/generics/arrayutility/string/StringFilterTest.java Dosyayı Görüntüle

@@ -30,7 +30,7 @@ public class StringFilterTest {
30 30
         ArrayUtility<String> utility = new ArrayUtility<>(input);
31 31
 
32 32
         // when
33
-        String[] actual = utility.filter(string -> string.length() < 3);
33
+        String[] actual = utility.filter(string -> string.length() <= 3);
34 34
 
35 35
         // then
36 36
         TestUtils.assertArrayEquals(expected, actual);