Browse Source

Completed lab and passed all test.

William Brown 6 years ago
parent
commit
7630b7becb

+ 97
- 2
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java View File

1
 package com.zipcodewilmington.arrayutility;
1
 package com.zipcodewilmington.arrayutility;
2
 
2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+
3
 /**
6
 /**
4
- * Created by leon on 3/6/18.
7
+ * Completed by William on 11/13/2018.
5
  */
8
  */
6
-public class ArrayUtility {
9
+public class ArrayUtility<T>{
10
+
11
+    private T t;
12
+    private T[] inputArray;
13
+
14
+    public T getT() {
15
+        return t;
16
+    }
17
+
18
+    public void setT(T t1) {
19
+        this.t = t1;
20
+    }
21
+
22
+    public ArrayUtility(T[] inputArray) {
23
+        this.inputArray = inputArray;
24
+    }
25
+
26
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
27
+        T[] merged = merge(inputArray, arrayToMerge);
28
+        Integer dups = duplicates(merged, valueToEvaluate);
29
+        return dups;
30
+    }
31
+
32
+    public Integer duplicates(T[] merged, T valueToEvaluate) {
33
+        int counter = 0;
34
+        for (int i = 0; i < merged.length; i++) {
35
+            if(merged[i] == valueToEvaluate){
36
+                counter++;
37
+            }
38
+        }
39
+        return counter;
40
+    }
41
+
42
+    public T[] merge(T[] array1, T[] array2) {
43
+        T[] output = (T[])Array.newInstance(this.inputArray.getClass().getComponentType(), (inputArray.length)+(array2.length));
44
+        int counter = 0;
45
+        for (int i = 0; i < output.length; i++) {
46
+            if(i < array1.length){
47
+                output[i] = array1[i];
48
+            }else{
49
+                output[i] = array2[i - array1.length];
50
+            }
51
+        }
52
+        return output;
53
+    }
54
+
55
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
56
+        return mostCommon(merge(inputArray, arrayToMerge));
57
+    }
58
+
59
+    public T mostCommon(T[] array) {
60
+        int counter = 0;
61
+        int tempCounter = 0;
62
+        T top = array[0];
63
+        T current = array[0];
64
+        for(int i = 0; i < array.length; i++){
65
+            for(int j = 0; j < array.length; j++){
66
+                if(array[j].equals(current)){
67
+                    tempCounter++;
68
+                }
69
+            }
70
+            if(tempCounter > counter){
71
+                top = current;
72
+                counter = tempCounter;
73
+            }
74
+            tempCounter = 0;
75
+            current = array[i];
76
+        }
77
+        return top;
78
+    }
79
+
80
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
81
+        int counter = 0;
82
+        for(int i = 0; i < inputArray.length; i++){
83
+            if(inputArray[i] == valueToEvaluate){
84
+                counter++;
85
+            }
86
+        }
87
+        return counter;
88
+    }
89
+
90
+    public T[] removeValue(T valueToRemove) {
91
+        int number = getNumberOfOccurrences(valueToRemove);
92
+        T[] output = (T[])Array.newInstance(this.inputArray.getClass().getComponentType(), inputArray.length - number);
93
+        int counter = 0;
94
+        for(int i = 0; i < inputArray.length; i++){
95
+            if(inputArray[i] != valueToRemove){
96
+                output[counter] = inputArray[i];
97
+                counter++;
98
+            }
99
+        }
100
+        return output;
101
+    }
7
 }
102
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/GetMostCommonFromMergeTest.java View File

7
  * Created by leon on 3/1/18.
7
  * Created by leon on 3/1/18.
8
  * The purpose of this class is to thoroughly test the method getMostCommonFromMerge()
8
  * The purpose of this class is to thoroughly test the method getMostCommonFromMerge()
9
  */
9
  */
10
-public class GetMostCommonFromMergeTest {
10
+public class GetMostCommonFromMergeTest{
11
     @Test
11
     @Test
12
     public void integerTest() {
12
     public void integerTest() {
13
         // Given
13
         // Given

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java View File

1
 package com.zipcodewilmington.arrayutility;
1
 package com.zipcodewilmington.arrayutility;
2
 
2
 
3
-import com.zipcodewilmington.UnitTestingUtils;
3
+//import com.zipcodewilmington.UnitTestingUtils;
4
 import org.junit.Test;
4
 import org.junit.Test;
5
 
5
 
6
 /**
6
 /**