ソースを参照

passed all test cases

Aleena Rose-Mathew 6 年 前
コミット
69a18a9370

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

@@ -21,6 +21,12 @@
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>
24 30
     </dependencies>
25 31
 
26 32
 

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

@@ -1,5 +1,6 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
3 4
 import java.util.ArrayList;
4 5
 import java.util.Arrays;
5 6
 
@@ -8,13 +9,13 @@ import java.util.Arrays;
8 9
  */
9 10
 public class ArrayUtility<S> {
10 11
     ArrayList<S> newlist=new ArrayList<S>();
11
-
12
+    S[] inputArray;
12 13
     public ArrayUtility(S[] inputArray) {
13
-
14
+        this.inputArray=inputArray;
14 15
         this.newlist.addAll(Arrays.asList(inputArray));
16
+        //S[] temp = (S[]) new Object[length];(creating a new generic array)
15 17
     }
16 18
     public Integer countDuplicatesInMerge(S[] inputArray, S valueToEvaluate ) {
17
-        // Display array elements
18 19
         Integer count=0;
19 20
         newlist.addAll(Arrays.asList(inputArray));
20 21
         for (S element : newlist) {
@@ -43,7 +44,7 @@ public class ArrayUtility<S> {
43 44
     public int getNumberOfOccurrences(S valueToEvaluate)
44 45
     {
45 46
         int countOccurence=0;
46
-        for(S element :newlist)
47
+        for(S element :inputArray)
47 48
         {
48 49
             if(element.equals(valueToEvaluate))
49 50
             {
@@ -52,21 +53,22 @@ public class ArrayUtility<S> {
52 53
         }
53 54
 
54 55
         return countOccurence;
56
+
55 57
     }
56 58
 
57
-   /* public S[] removeValue(S valueToRemove)
59
+    public S[] removeValue(S valueToRemove)
58 60
     {
59
-        ArrayList<S> resultList=new ArrayList<S>();
60
-
61
-        for(S element : newlist)
62
-        {
63
-            if(!element.equals(valueToRemove))
64
-            {
65
-                resultList.add(valueToRemove);
61
+        Integer removed = getNumberOfOccurrences(valueToRemove);
62
+        S[] array = (S[])Array.newInstance(inputArray.getClass().getComponentType(), inputArray.length-removed);
63
+        int count = 0;
64
+        for (int i = 0; i < inputArray.length; i++) {
65
+            if (!inputArray[i].equals(valueToRemove)) {
66
+                array[count] = inputArray[i];
67
+                count++;
66 68
             }
67 69
         }
68
-        S[] newArray=new (S[])[resultList.size()];
70
+        return array;
69 71
 
72
+    }
70 73
 
71
-    }*/
72 74
 }

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

@@ -1,19 +1,19 @@
1
-//package com.zipcodewilmington.arrayutility;
2
-//
3
-//import org.junit.runner.RunWith;
4
-//import org.junit.runners.Suite;
5
-//
6
-///**
7
-// * Created by leon on 3/1/18.
8
-// * The purpose of this class is to test all classes within this package
9
-// */
10
-//@RunWith(Suite.class)
11
-//
12
-//@Suite.SuiteClasses({
13
-//        CountDuplicatesInMergeTest.class,
14
-//        GetMostCommonFromMergeTest.class,
15
-//        GetNumberOfOccurrencesTest.class,
16
-//        RemoveValueTest.class
17
-//})
18
-//public class ArrayUtilityTestSuite {
19
-//}
1
+package com.zipcodewilmington.arrayutility;
2
+
3
+import org.junit.runner.RunWith;
4
+import org.junit.runners.Suite;
5
+
6
+/**
7
+ * Created by leon on 3/1/18.
8
+ * The purpose of this class is to test all classes within this package
9
+ */
10
+@RunWith(Suite.class)
11
+
12
+@Suite.SuiteClasses({
13
+        CountDuplicatesInMergeTest.class,
14
+        GetMostCommonFromMergeTest.class,
15
+        GetNumberOfOccurrencesTest.class,
16
+        RemoveValueTest.class
17
+})
18
+public class ArrayUtilityTestSuite {
19
+}

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

@@ -1,78 +1,78 @@
1
-//package com.zipcodewilmington.arrayutility;
2
-//
1
+package com.zipcodewilmington.arrayutility;
2
+
3 3
 //import com.zipcodewilmington.UnitTestingUtils;
4
-//import org.junit.Test;
5
-//
6
-///**
7
-// * Created by leon on 3/1/18.
8
-// * The purpose of this class is to thoroughly test the method removeValue()
9
-// */
10
-//public class RemoveValueTest {
11
-//    @Test
12
-//    public void integerTest() {
13
-//        // Given
14
-//        Integer valueToRemove = 7;
15
-//        Integer[] expected = {11, 2, 8, 4, 5, 0, 9, 8};
16
-//        Integer[] inputArray = {11, 2, valueToRemove, 8, 4, 5, valueToRemove, 0, 9, 8, valueToRemove};
17
-//        ArrayUtility<Integer> arrayUtility = new ArrayUtility<Integer>(inputArray);
18
-//
19
-//
20
-//        // When
21
-//        Integer[] actual = arrayUtility.removeValue(valueToRemove);
22
-//
23
-//        // Then
24
-//        UnitTestingUtils.assertArrayEquality(expected, actual);
25
-//    }
26
-//
27
-//    @Test
28
-//    public void longTest() {
29
-//        // Given
30
-//        Long valueToRemove = 7L;
31
-//        Long[] expected = {12L, 2L, 8L, 4L, 5L, 0L, 9L, 8L};
32
-//        Long[] inputArray = {12L, 2L, valueToRemove, 8L, 4L, 5L, valueToRemove, 0L, 9L, 8L, valueToRemove};
33
-//        ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
34
-//
35
-//
36
-//        // When
37
-//        Long[] actual = arrayUtility.removeValue(valueToRemove);
38
-//
39
-//        // Then
40
-//        UnitTestingUtils.assertArrayEquality(expected, actual);
41
-//    }
42
-//
43
-//
44
-//    @Test
45
-//    public void stringTest() {
46
-//        // Given
47
-//        String valueToRemove = "7";
48
-//        String[] expected = {"13", "2", "8", "4", "5", "0", "9", "8"};
49
-//        String[] inputArray = {"13", "2", valueToRemove, "8", "4", "5", valueToRemove, "0", "9", "8", valueToRemove};
50
-//        ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);
51
-//
52
-//
53
-//        // When
54
-//        String[] actual = arrayUtility.removeValue(valueToRemove);
55
-//
56
-//        // Then
57
-//        UnitTestingUtils.assertArrayEquality(expected, actual);
58
-//    }
59
-//
60
-//
61
-//
62
-//    @Test
63
-//    public void objectTest() {
64
-//        // Given
65
-//        Object valueToRemove = "7";
66
-//        Object[] expected = {"41", "2", "8", "4", "5", "0", "9", "8"};
67
-//        Object[] inputArray = {"41", "2", valueToRemove, "8", "4", "5", valueToRemove, "0", "9", "8", valueToRemove};
68
-//        ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(inputArray);
69
-//
70
-//
71
-//        // When
72
-//        Object[] actual = arrayUtility.removeValue(valueToRemove);
73
-//
74
-//        // Then
75
-//        UnitTestingUtils.assertArrayEquality(expected, actual);
76
-//    }
77
-//
78
-//}
4
+import org.junit.Test;
5
+
6
+/**
7
+ * Created by leon on 3/1/18.
8
+ * The purpose of this class is to thoroughly test the method removeValue()
9
+ */
10
+public class RemoveValueTest {
11
+    @Test
12
+    public void integerTest() {
13
+        // Given
14
+        Integer valueToRemove = 7;
15
+        Integer[] expected = {11, 2, 8, 4, 5, 0, 9, 8};
16
+        Integer[] inputArray = {11, 2, valueToRemove, 8, 4, 5, valueToRemove, 0, 9, 8, valueToRemove};
17
+        ArrayUtility<Integer> arrayUtility = new ArrayUtility<Integer>(inputArray);
18
+
19
+
20
+        // When
21
+        Integer[] actual = arrayUtility.removeValue(valueToRemove);
22
+
23
+        // Then
24
+        UnitTestingUtils.assertArrayEquality(expected, actual);
25
+    }
26
+
27
+    @Test
28
+    public void longTest() {
29
+        // Given
30
+        Long valueToRemove = 7L;
31
+        Long[] expected = {12L, 2L, 8L, 4L, 5L, 0L, 9L, 8L};
32
+        Long[] inputArray = {12L, 2L, valueToRemove, 8L, 4L, 5L, valueToRemove, 0L, 9L, 8L, valueToRemove};
33
+        ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
34
+
35
+
36
+        // When
37
+        Long[] actual = arrayUtility.removeValue(valueToRemove);
38
+
39
+        // Then
40
+        UnitTestingUtils.assertArrayEquality(expected, actual);
41
+    }
42
+
43
+
44
+    @Test
45
+    public void stringTest() {
46
+        // Given
47
+        String valueToRemove = "7";
48
+        String[] expected = {"13", "2", "8", "4", "5", "0", "9", "8"};
49
+        String[] inputArray = {"13", "2", valueToRemove, "8", "4", "5", valueToRemove, "0", "9", "8", valueToRemove};
50
+        ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);
51
+
52
+
53
+        // When
54
+        String[] actual = arrayUtility.removeValue(valueToRemove);
55
+
56
+        // Then
57
+        UnitTestingUtils.assertArrayEquality(expected, actual);
58
+    }
59
+
60
+
61
+
62
+    @Test
63
+    public void objectTest() {
64
+        // Given
65
+        Object valueToRemove = "7";
66
+        Object[] expected = {"41", "2", "8", "4", "5", "0", "9", "8"};
67
+        Object[] inputArray = {"41", "2", valueToRemove, "8", "4", "5", valueToRemove, "0", "9", "8", valueToRemove};
68
+        ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(inputArray);
69
+
70
+
71
+        // When
72
+        Object[] actual = arrayUtility.removeValue(valueToRemove);
73
+
74
+        // Then
75
+        UnitTestingUtils.assertArrayEquality(expected, actual);
76
+    }
77
+
78
+}