BlackJack 6 år sedan
förälder
incheckning
84f5643a81
3 ändrade filer med 251 tillägg och 144 borttagningar
  1. 51
    2
      IntegerDuplicateDeleter.java
  2. 144
    140
      IntegerDuplicateDeleterTest.java
  3. 56
    2
      StringDuplicateDeleter.java

+ 51
- 2
IntegerDuplicateDeleter.java Visa fil

@@ -1,6 +1,55 @@
1
- 
1
+import org.omg.PortableInterceptor.INACTIVE;
2 2
 
3
+import java.util.Arrays;
3 4
 
4 5
 public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
5 6
 
6
-}
7
+
8
+    public IntegerDuplicateDeleter(Integer[] array) {
9
+         super(array);
10
+     }
11
+
12
+
13
+     public Integer[] removeDuplicates(int maxNumberOfDuplications) {
14
+         Integer[] newArr = Arrays.copyOf(this.array, this.array.length);
15
+         int counter = 0;
16
+
17
+         for (int x = 0; x < array.length; x++) {
18
+             if (countOccurence(array[x]) < maxNumberOfDuplications) {
19
+                 newArr[counter] = array[x];
20
+                 counter++;
21
+                 }
22
+         }
23
+
24
+         Integer[] answer = Arrays.copyOf(newArr, counter);
25
+         return  answer;
26
+     }
27
+
28
+
29
+     public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
30
+         Integer[] newArr = Arrays.copyOf(this.array, this.array.length);
31
+         int counter = 0;
32
+
33
+         for (int x = 0; x < array.length; x++) {
34
+             if (countOccurence(array[x]) < exactNumberOfDuplications || countOccurence(array[x]) > exactNumberOfDuplications) {
35
+                 newArr[counter] = array[x];
36
+                 counter++;
37
+             }
38
+         }
39
+
40
+         Integer[] answer = Arrays.copyOf(newArr, counter);
41
+         return answer;
42
+     }
43
+
44
+
45
+     public int countOccurence(Integer value) {
46
+        int count = 0;
47
+         for (int x = 0; x < array.length; x++) {
48
+
49
+             if (array[x] == value) {
50
+                 count++;
51
+             }
52
+         }
53
+         return count;
54
+     }
55
+ }

+ 144
- 140
IntegerDuplicateDeleterTest.java Visa fil

@@ -1,78 +1,77 @@
1
- 
2 1
 
3
-import org.junit.Assert;
4
-import org.junit.Before;
5
-import org.junit.Test;
6 2
 
7
-import java.util.Arrays;
3
+ import org.junit.Assert;
4
+ import org.junit.Before;
5
+ import org.junit.Test;
8 6
 
9
-/**
10
- * Created by leon on 1/25/18.
11
- * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
12
- */
13
-public class IntegerDuplicateDeleterTest {
7
+ import java.util.Arrays;
14 8
 
15
-    @Test
16
-    public void testRemoveDuplicatesExactly_whenThereIsOnlyOneElement() {
17
-        Integer[] intArray = new Integer[]{0};
18
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
9
+ /**
10
+  * Created by leon on 1/25/18.
11
+  * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
12
+  */
13
+ public class IntegerDuplicateDeleterTest {
19 14
 
20
-        Integer[] expected = new Integer[]{0};
21
-        Integer[] actual = deleter.removeDuplicatesExactly(2);
22
-        Assert.assertArrayEquals(expected, actual);
23
-    }
15
+     @Test
16
+     public void testRemoveDuplicatesExactly_whenThereIsOnlyOneElement() {
17
+         Integer[] intArray = new Integer[]{0};
18
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
24 19
 
25
-    @Test
26
-    public void testRemoveDuplicatesExactly_whenThereIsOnlyOneElementAndRemoveDumplicationIs1() {
27
-        Integer[] intArray = new Integer[]{9};
28
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
20
+         Integer[] expected = new Integer[]{0};
21
+         Integer[] actual = deleter.removeDuplicatesExactly(2);
22
+         Assert.assertArrayEquals(expected, actual);
23
+     }
29 24
 
30
-        Integer[] expected = new Integer[]{};
31
-        Integer[] actual = deleter.removeDuplicatesExactly(1);
32
-        Assert.assertArrayEquals(expected, actual);
33
-    }
25
+     @Test
26
+     public void testRemoveDuplicatesExactly_whenThereIsOnlyOneElementAndRemoveDumplicationIs1() {
27
+         Integer[] intArray = new Integer[]{9};
28
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
34 29
 
35
-    @Test
36
-    public void testRemoveDuplicatesExactly_withNoElementsWithExactDumplication() {
37
-        Integer[] intArray = new Integer[]{51, 4, 4, 4, 28, 38, 38, 38, 38, 38};
38
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
30
+         Integer[] expected = new Integer[]{};
31
+         Integer[] actual = deleter.removeDuplicatesExactly(1);
32
+         Assert.assertArrayEquals(expected, actual);
33
+     }
39 34
 
40
-        Integer[] expected = new Integer[]{51, 4, 4, 4, 28, 38, 38, 38, 38, 38};
41
-        Integer[] actual = deleter.removeDuplicatesExactly(2);
42
-        Assert.assertArrayEquals(expected, actual);
43
-    }
35
+     @Test
36
+     public void testRemoveDuplicatesExactly_withNoElementsWithExactDumplication() {
37
+         Integer[] intArray = new Integer[]{51, 4, 4, 4, 28, 38, 38, 38, 38, 38};
38
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
44 39
 
45
-    @Test
46
-    public void testRemoveDuplicatesExactly_withMoreThanOneElement() {
47
-        Integer[] intArray = new Integer[]{5, 5, 1, 2, 2, 3, 3, 3};
48
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
40
+         Integer[] expected = new Integer[]{51, 4, 4, 4, 28, 38, 38, 38, 38, 38};
41
+         Integer[] actual = deleter.removeDuplicatesExactly(2);
42
+         Assert.assertArrayEquals(expected, actual);
43
+     }
49 44
 
50
-        Integer[] expected = new Integer[]{1, 3, 3, 3};
51
-        Integer[] actual = deleter.removeDuplicatesExactly(2);
52
-        Assert.assertArrayEquals(expected, actual);
53
-    }
45
+     @Test
46
+     public void testRemoveDuplicatesExactly_withMoreThanOneElement() {
47
+         Integer[] intArray = new Integer[]{5, 5, 1, 2, 2, 3, 3, 3};
48
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
54 49
 
55
-    @Test
56
-    public void testRemoveDuplicatesExactly_withOccuranceAtTheBeginningAndEnd() {
57
-        Integer[] intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
58
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
50
+         Integer[] expected = new Integer[]{1, 3, 3, 3};
51
+         Integer[] actual = deleter.removeDuplicatesExactly(2);
52
+         Assert.assertArrayEquals(expected, actual);
53
+     }
59 54
 
60
-        Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};
61
-        Integer[] actual = deleter.removeDuplicatesExactly(3);
62
-        Assert.assertArrayEquals(expected, actual);
63
-    }
55
+     @Test
56
+     public void testRemoveDuplicatesExactly_withOccuranceAtTheBeginningAndEnd() {
57
+         Integer[] intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
58
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
64 59
 
60
+         Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};
61
+         Integer[] actual = deleter.removeDuplicatesExactly(3);
62
+         Assert.assertArrayEquals(expected, actual);
63
+     }
65 64
 
66
-    @Test
67
-    public void testRemoveDuplicatesExactly_withOccuranceInTheMiddle() {
68
-        Integer[] intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
69
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
70 65
 
71
-        Integer[] expected = new Integer[]{0, 0, 0, 1, 5, 5, 5, 6, 9, 9, 9};
72
-        Integer[] actual = deleter.removeDuplicatesExactly(2);
73
-        Assert.assertArrayEquals(expected, actual);
74
-    }
66
+     @Test
67
+     public void testRemoveDuplicatesExactly_withOccuranceInTheMiddle() {
68
+         Integer[] intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
69
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
75 70
 
71
+         Integer[] expected = new Integer[]{0, 0, 0, 1, 5, 5, 5, 6, 9, 9, 9};
72
+         Integer[] actual = deleter.removeDuplicatesExactly(2);
73
+         Assert.assertArrayEquals(expected, actual);
74
+     }
76 75
 
77 76
 
78 77
 
@@ -82,108 +81,113 @@ public class IntegerDuplicateDeleterTest {
82 81
 
83 82
 
84 83
 
85
-    @Test
86
-    public void testRemoveDuplicates_whenThereIsOnlyOneElement() {
87
-        Integer[] intArray = new Integer[]{5};
88
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
89 84
 
90
-        Integer[] expected = new Integer[]{5};
91
-        Integer[] actual = deleter.removeDuplicates(2);
92
-        Assert.assertArrayEquals(expected, actual);
93
-    }
85
+     @Test
86
+     public void testRemoveDuplicates_whenThereIsOnlyOneElement() {
87
+         Integer[] intArray = new Integer[]{5};
88
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
94 89
 
95
-    @Test
96
-    public void testRemoveDuplicates_whenThereIsOnlyOneElementAndRemoveDumplicationIs1() {
97
-        Integer[] intArray = new Integer[]{3};
98
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
90
+         Integer[] expected = new Integer[]{5};
91
+         Integer[] actual = deleter.removeDuplicates(2);
92
+         Assert.assertArrayEquals(expected, actual);
93
+     }
99 94
 
100
-        Integer[] expected = new Integer[]{};
101
-        Integer[] actual = deleter.removeDuplicatesExactly(1);
102
-        Assert.assertArrayEquals(expected, actual);
103
-    }
95
+     @Test
96
+     public void testRemoveDuplicates_whenThereIsOnlyOneElementAndRemoveDumplicationIs1() {
97
+         Integer[] intArray = new Integer[]{3};
98
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
104 99
 
105
-    @Test
106
-    public void testRemoveDuplicates_whenThereIsMoreThanOneElement() {
107
-        Integer[] intArray = new Integer[]{4, 1, 2};
108
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
100
+         Integer[] expected = new Integer[]{};
101
+         Integer[] actual = deleter.removeDuplicatesExactly(1);
102
+         Assert.assertArrayEquals(expected, actual);
103
+     }
109 104
 
110
-        Integer[] expected = new Integer[]{4, 1, 2};
111
-        Integer[] actual = deleter.removeDuplicates(2);
112
-        Assert.assertArrayEquals(expected, actual);
113
-    }
105
+     @Test
106
+     public void testRemoveDuplicates_whenThereIsMoreThanOneElement() {
107
+         Integer[] intArray = new Integer[]{4, 1, 2};
108
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
114 109
 
115
-    @Test
116
-    public void testRemoveDuplicates_whenThereIsMoreThanOneElementWithOccurance() {
117
-        Integer[] intArray = new Integer[]{4, 4, 1, 2, 2, 2};
118
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
110
+         Integer[] expected = new Integer[]{4, 1, 2};
111
+         Integer[] actual = deleter.removeDuplicates(2);
112
+         Assert.assertArrayEquals(expected, actual);
113
+     }
119 114
 
120
-        Integer[] expected = new Integer[]{1};
121
-        Integer[] actual = deleter.removeDuplicates(2);
122
-        Assert.assertArrayEquals(expected, actual);
123
-    }
115
+     @Test
116
+     public void testRemoveDuplicates_whenThereIsMoreThanOneElementWithOccurance() {
117
+         Integer[] intArray = new Integer[]{4, 4, 1, 2, 2, 2};
118
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
124 119
 
125
-    @Test
126
-    public void testRemoveDuplicates_removesNothing() {
127
-        Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
128
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
129
-        Integer[] expected = new Integer[]{};
130
-        Integer[] actual = deleter.removeDuplicates(0);
131
-        Assert.assertArrayEquals(expected, actual);
132
-    }
120
+         Integer[] expected = new Integer[]{1};
121
+         Integer[] actual = deleter.removeDuplicates(2);
122
+         Assert.assertArrayEquals(expected, actual);
123
+     }
133 124
 
134
-    @Test
135
-    public void testRemoveDuplicates_withOccuranceInTheMiddle() {
136
-        Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
137
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
138
-        Integer[] expected = new Integer[]{2};
139
-        Integer[] actual = deleter.removeDuplicates(2);
140
-        Assert.assertArrayEquals(expected, actual);
141
-    }
125
+     @Test
126
+     public void testRemoveDuplicates_removesNothing() {
127
+         Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
128
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
129
+         Integer[] expected = new Integer[]{};
130
+         Integer[] actual = deleter.removeDuplicates(0);
131
+         Assert.assertArrayEquals(expected, actual);
132
+     }
142 133
 
134
+     @Test
135
+     public void testRemoveDuplicates_withOccuranceInTheMiddle() {
136
+         Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
137
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
138
+         Integer[] expected = new Integer[]{2};
139
+         Integer[] actual = deleter.removeDuplicates(2);
140
+         Assert.assertArrayEquals(expected, actual);
141
+     }
143 142
 
144
-    @Test
145
-    public void testRemoveDuplicates_withOccuranceAtTheBeginningAndEnd() {
146
-        Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
147
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
148
-        Integer[] expected = new Integer[]{1, 1, 2, 4, 4};
149
-        Integer[] actual = deleter.removeDuplicates(3);
150
-        Assert.assertArrayEquals(expected, actual);
151
-    }
152 143
 
144
+     @Test
145
+     public void testRemoveDuplicates_withOccuranceAtTheBeginningAndEnd() {
146
+         Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
147
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
148
+         Integer[] expected = new Integer[]{1, 1, 2, 4, 4};
149
+         Integer[] actual = deleter.removeDuplicates(3);
150
+         Assert.assertArrayEquals(expected, actual);
151
+     }
153 152
 
154 153
 
155 154
 
156 155
 
157
-    @Test
158
-    public void testRemoveDuplicatesExactlyIdempotence() {
159
-        Integer[] input = {11, 3, 3, 3, 4, 4, 4, 4};
160
-        Integer[] expected = {11, 4, 4, 4, 4};
161 156
 
162
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
157
+     @Test
158
+     public void testRemoveDuplicatesExactlyIdempotence() {
159
+         Integer[] input = {11, 3, 3, 3, 4, 4, 4, 4};
160
+         Integer[] expected = {11, 4, 4, 4, 4};
163 161
 
164
-        Integer[] actual1 = deleter.removeDuplicatesExactly(3);
165
-        Integer[] actual2 = deleter.removeDuplicatesExactly(3);
166
-        Integer[] actual3 = deleter.removeDuplicatesExactly(3);
162
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
167 163
 
168
-        Assert.assertArrayEquals(expected, actual1);
169
-        Assert.assertArrayEquals(expected, actual2);
170
-        Assert.assertArrayEquals(expected, actual3);
171
-    }
164
+         Integer[] actual1 = deleter.removeDuplicatesExactly(3);
165
+         Integer[] actual2 = deleter.removeDuplicatesExactly(3);
166
+         Integer[] actual3 = deleter.removeDuplicatesExactly(3);
172 167
 
168
+         Assert.assertArrayEquals(expected, actual1);
169
+         Assert.assertArrayEquals(expected, actual2);
170
+         Assert.assertArrayEquals(expected, actual3);
171
+     }
173 172
 
174
-    @Test
175
-    public void testRemoveDuplicatesIdempotence() {
176
-        Integer[] input = {1, 2, 2, 2, 3, 4, 4};
177
-        Integer[] expected = {1, 3};
178 173
 
179
-        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
174
+     @Test
175
+     public void testRemoveDuplicatesIdempotence() {
176
+         Integer[] input = {1, 2, 2, 2, 3, 4, 4};
177
+         Integer[] expected = {1, 3};
178
+
179
+         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
180
+
181
+         Integer[] actual1 = deleter.removeDuplicates(2);
182
+         Integer[] actual2 = deleter.removeDuplicates(2);
183
+         Integer[] actual3 = deleter.removeDuplicates(2);
184
+
185
+         Assert.assertArrayEquals(expected, actual1);
186
+         Assert.assertArrayEquals(expected, actual2);
187
+         Assert.assertArrayEquals(expected, actual3);
188
+     }
189
+
190
+
191
+
192
+     }
180 193
 
181
-        Integer[] actual1 = deleter.removeDuplicates(2);
182
-        Integer[] actual2 = deleter.removeDuplicates(2);
183
-        Integer[] actual3 = deleter.removeDuplicates(2);
184
-
185
-        Assert.assertArrayEquals(expected, actual1);
186
-        Assert.assertArrayEquals(expected, actual2);
187
-        Assert.assertArrayEquals(expected, actual3);
188
-    }
189
-}

+ 56
- 2
StringDuplicateDeleter.java Visa fil

@@ -1,6 +1,60 @@
1
- 
1
+ import java.util.*;
2 2
 
3 3
 public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
4 4
 
5
+    public StringDuplicateDeleter(String[] array) {
6
+        super(array);
7
+
8
+    }
9
+
10
+     @Override
11
+    public String[] removeDuplicates(int maxNumberOfDuplications) {
12
+         String[] newArr = Arrays.copyOf(array, array.length);
13
+         int counter = 0;
14
+
15
+         for (int x = 0; x < array.length; x++) {
16
+             if (countOccurence(array[x]) < maxNumberOfDuplications) {
17
+                 newArr[counter] = array[x];
18
+                 counter++;
19
+             }
20
+         }
21
+
22
+         String[] answer = Arrays.copyOf(newArr, counter);
23
+         return  answer;
24
+     }
25
+
26
+    //@Override
27
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
28
+        String[] newArr = Arrays.copyOf(array, array.length);
29
+        int counter = 0;
30
+
31
+        for (int x = 0; x < array.length; x++) {
32
+            if (countOccurence(array[x]) < exactNumberOfDuplications || countOccurence(array[x]) > exactNumberOfDuplications) {
33
+                newArr[counter] = array[x];
34
+                counter++;
35
+            }
36
+        }
37
+
38
+        String[] answer = Arrays.copyOf(newArr, counter);
39
+        return answer;
40
+    }
41
+
42
+
43
+
44
+    public Integer countOccurence(String value) {
45
+        int count = 0;
46
+        for (int x = 0; x < array.length; x++) {
47
+
48
+            if (array[x] == value) {
49
+                count++;
50
+            }
51
+        }
52
+        return count;
53
+    }
54
+    }
55
+
56
+
57
+
58
+
59
+
5 60
 
6
-}