rjsmall90 6 年 前
コミット
9e3f67e5c1

+ 12
- 0
pom.xml ファイルの表示

@@ -21,6 +21,18 @@
21 21
             <artifactId>junit</artifactId>
22 22
             <version>4.12</version>
23 23
         </dependency>
24
+        <dependency>
25
+            <groupId>com.zipcodewilmington</groupId>
26
+            <artifactId>arrayutility</artifactId>
27
+            <version>1.0-SNAPSHOT</version>
28
+            <scope>test</scope>
29
+        </dependency>
30
+        <dependency>
31
+            <groupId>com.zipcodewilmington</groupId>
32
+            <artifactId>arrayutility</artifactId>
33
+            <version>1.0-SNAPSHOT</version>
34
+            <scope>test</scope>
35
+        </dependency>
24 36
     </dependencies>
25 37
 
26 38
 

+ 70
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java ファイルの表示

@@ -1,7 +1,76 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+
3 6
 /**
4 7
  * Created by leon on 3/6/18.
5 8
  */
6
-public class ArrayUtility {
9
+public class ArrayUtility<I> {
10
+
11
+  private I[] inputArray;
12
+
13
+    public ArrayUtility(I[] inputArray) {
14
+        this.inputArray= inputArray;
15
+    }
16
+
17
+
18
+
19
+
20
+    public Integer countDuplicatesInMerge(I[] arrayToMerge, I valueToEvaluate) {
21
+        Integer count = 0;
22
+        Object[] arr =  new Object[inputArray.length + arrayToMerge.length];
23
+
24
+        System.arraycopy(inputArray, 0, arr, 0 , inputArray.length);
25
+        System.arraycopy(arrayToMerge, 0, arr, inputArray.length , arrayToMerge.length);
26
+
27
+        for(Object i : arr) if(i == valueToEvaluate) count+=1;
28
+
29
+        return count;
30
+    }
31
+
32
+
33
+    public I getMostCommonFromMerge(I[] arrayToMerge) {
34
+        Object[] arr = new Object[inputArray.length + arrayToMerge.length];
35
+        System.arraycopy(inputArray, 0, arr, 0 , inputArray.length);
36
+        System.arraycopy(arrayToMerge, 0, arr, inputArray.length , arrayToMerge.length);
37
+
38
+        Integer count = 1, tempCount;
39
+        Object common =  arr[0];
40
+        Object temp;
41
+
42
+        for (int i = 0; i < (arr.length - 1); i++)
43
+        {
44
+            temp = arr[i];
45
+            tempCount = 0;
46
+            for (int j = 1; j < arr.length; j++)
47
+            {
48
+                if (temp == arr[j])
49
+                    tempCount++;
50
+            }
51
+            if (tempCount > count)
52
+            {
53
+                common = temp;
54
+                count = tempCount;
55
+            }
56
+        }
57
+        return (I) common;
58
+    }
59
+
60
+    public I[] removeValue(I valueToRemove) {
61
+        ArrayList<I> arr = new ArrayList<>();
62
+            for(I i: inputArray) if(i != valueToRemove) arr.add(i);
63
+
64
+            I[] arr2 = (I[])Array.newInstance(inputArray.getClass().getComponentType(), arr.size());
65
+
66
+        return arr.toArray(arr2);
67
+    }
68
+
69
+
70
+    public Integer getNumberOfOccurrences(I valueToEvaluate) {
71
+        Integer count = 0;
72
+        for(I i : inputArray) if(i == valueToEvaluate) count++;
73
+
74
+        return count;
75
+    }
7 76
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/CountDuplicatesInMergeTest.java ファイルの表示

@@ -15,7 +15,7 @@ public class CountDuplicatesInMergeTest {
15 15
         Integer expected = 5;
16 16
         Integer[] inputArray = {1, 2, valueToEvaluate, 8, 4, 5, valueToEvaluate, 0, 9, 8, valueToEvaluate};
17 17
         Integer[] arrayToMerge = {1, 2, valueToEvaluate, 8, 4, 5, valueToEvaluate, 0, 9, 8};
18
-        ArrayUtility<Integer> arrayUtility = new ArrayUtility<Integer>(inputArray);
18
+        ArrayUtility<Integer> arrayUtility = new ArrayUtility<>(inputArray);
19 19
 
20 20
         // When
21 21
         Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);

+ 2
- 2
src/test/java/com/zipcodewilmington/arrayutility/GetMostCommonFromMergeTest.java ファイルの表示

@@ -32,7 +32,7 @@ public class GetMostCommonFromMergeTest {
32 32
         ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
33 33
 
34 34
         // When
35
-        Long actual = arrayUtility.getMostCommonFromMerge(arrayToMerge);
35
+        Long actual = Long.valueOf(arrayUtility.getMostCommonFromMerge(arrayToMerge));
36 36
 
37 37
         // Then
38 38
         Assert.assertEquals(expected, actual);
@@ -48,7 +48,7 @@ public class GetMostCommonFromMergeTest {
48 48
         ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);
49 49
 
50 50
         // When
51
-        String actual = arrayUtility.getMostCommonFromMerge(arrayToMerge);
51
+        String actual = String.valueOf(arrayUtility.getMostCommonFromMerge(arrayToMerge));
52 52
 
53 53
         // Then
54 54
         Assert.assertEquals(expected, actual);

+ 4
- 4
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java ファイルの表示

@@ -1,6 +1,6 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
-import com.zipcodewilmington.UnitTestingUtils;
3
+//import com.zipcodewilmington.UnitTestingUtils;
4 4
 import org.junit.Test;
5 5
 
6 6
 /**
@@ -14,7 +14,7 @@ public class RemoveValueTest {
14 14
         Integer valueToRemove = 7;
15 15
         Integer[] expected = {11, 2, 8, 4, 5, 0, 9, 8};
16 16
         Integer[] inputArray = {11, 2, valueToRemove, 8, 4, 5, valueToRemove, 0, 9, 8, valueToRemove};
17
-        ArrayUtility<Integer> arrayUtility = new ArrayUtility<Integer>(inputArray);
17
+        ArrayUtility<Integer> arrayUtility = new ArrayUtility<>(inputArray);
18 18
 
19 19
 
20 20
         // When
@@ -30,7 +30,7 @@ public class RemoveValueTest {
30 30
         Long valueToRemove = 7L;
31 31
         Long[] expected = {12L, 2L, 8L, 4L, 5L, 0L, 9L, 8L};
32 32
         Long[] inputArray = {12L, 2L, valueToRemove, 8L, 4L, 5L, valueToRemove, 0L, 9L, 8L, valueToRemove};
33
-        ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
33
+        ArrayUtility<Long> arrayUtility = new ArrayUtility<>(inputArray);
34 34
 
35 35
 
36 36
         // When
@@ -65,7 +65,7 @@ public class RemoveValueTest {
65 65
         Object valueToRemove = "7";
66 66
         Object[] expected = {"41", "2", "8", "4", "5", "0", "9", "8"};
67 67
         Object[] inputArray = {"41", "2", valueToRemove, "8", "4", "5", valueToRemove, "0", "9", "8", valueToRemove};
68
-        ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(inputArray);
68
+        ArrayUtility<Object> arrayUtility = new ArrayUtility<>(inputArray);
69 69
 
70 70
 
71 71
         // When

+ 0
- 26
src/test/java/com/zipcodewilmington/arrayutility/UnitTestingUtils.java ファイルの表示

@@ -1,26 +0,0 @@
1
-package com.zipcodewilmington.arrayutility;
2
-
3
-import org.junit.Assert;
4
-
5
-import java.util.Arrays;
6
-
7
-/**
8
- * Created by leon on 1/28/18.
9
- *
10
- * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
11
- */
12
-public class UnitTestingUtils {
13
-    public synchronized static <T, E> void assertArrayEquality(T[] expected, E[] actual) {
14
-        Arrays.sort(expected);
15
-        Arrays.sort(actual);
16
-        String expectedString = Arrays.toString(expected);
17
-        String actualString = Arrays.toString(actual);
18
-        boolean equality = expectedString.equals(actualString);
19
-
20
-        System.out.println("\n\nExpected:\n\t" + expectedString);
21
-        System.out.println("\nActual:\n\t" + actualString);
22
-        System.out.println("\nEquivalence:\n\t" + equality);
23
-
24
-        Assert.assertArrayEquals(expected, actual);
25
-    }
26
-}