Ver código fonte

half of the tests, working through the methods

Jennifer Chao 6 anos atrás
pai
commit
5b91529277

+ 226
- 1
arraz/src/main/java/Arraz.java Ver arquivo

@@ -1,2 +1,227 @@
1
-public class Arraz {
1
+import java.lang.reflect.Array;
2
+import java.util.ArrayList;
3
+import java.util.Arrays;
4
+import java.util.Iterator;
5
+import java.util.List;
6
+import java.util.stream.Stream;
7
+
8
+public class Arraz<T> {
9
+
10
+    public int sumValuesOfArray(int[] array) {
11
+        int sum = 0;
12
+
13
+        for (int intValue : array) {
14
+            sum += intValue;
15
+        }
16
+
17
+        return sum;
18
+    }
19
+
20
+    public double sumDoubleOfArray(double[] array) {
21
+        double sum = 0;
22
+
23
+        for (double doubleValue : array) {
24
+            sum += doubleValue;
25
+        }
26
+
27
+        return sum;
28
+    }
29
+
30
+    public int averageOfArray(int[] array) {
31
+        return sumValuesOfArray(array) / array.length;
32
+    }
33
+
34
+    public double doubleAverageOfArray(double[] array) {
35
+        return sumDoubleOfArray(array) / array.length;
36
+    }
37
+
38
+    public boolean containsValue(int[] array, int value) {
39
+        for (int intValue : array) {
40
+            if (intValue == value) {
41
+                return true;
42
+            }
43
+        }
44
+        return false;
45
+    }
46
+
47
+    public int[] reverseArray(int[] array) {
48
+        int[] reverse = new int[array.length];
49
+        int reverseIndex = array.length - 1;
50
+
51
+        for (int intValue : array) {
52
+            reverse[reverseIndex] = intValue;
53
+            reverseIndex -= 1;
54
+        }
55
+
56
+        return reverse;
57
+    }
58
+
59
+    public OddEven getOddEvensOfArray(int[] array) {
60
+        OddEven oddeven = new OddEven();
61
+
62
+        for (int intValue : array) {
63
+            if (intValue % 2 == 0) {
64
+                oddeven.evens++;
65
+            } else {
66
+                oddeven.odds++;
67
+            }
68
+        }
69
+
70
+        return oddeven;
71
+    }
72
+
73
+    public int findIndexOf(int[] array, int value) {
74
+        for (int i = 0; i < array.length; i++) {
75
+            if (array[i] == value) {
76
+                return i;
77
+            }
78
+        }
79
+
80
+        return -1;
81
+    }
82
+
83
+    public T[] createEmptyGenericArray(T[] array, int length) {
84
+        Class cl = array.getClass();
85
+        if (!cl.isArray()) {
86
+            return null;
87
+        } else {
88
+            T[] copy = (T[]) Array.newInstance(cl.getComponentType(), length);
89
+            return copy;
90
+        }
91
+    }
92
+
93
+    public T[] copyArrayByIterator(T[] array) {
94
+        T[] copy = createEmptyGenericArray(array, array.length);
95
+        int index = 0;
96
+
97
+        List<T> copyList = Arrays.asList(array);
98
+        Iterator iterator = copyList.iterator();
99
+
100
+        while (iterator.hasNext()) {
101
+            copy[index] = (T) iterator.next();
102
+            index++;
103
+        }
104
+
105
+        return copy;
106
+    }
107
+
108
+    public T[] copyArrayByLoop(T[] array) {
109
+        T[] copy = createEmptyGenericArray(array, array.length);
110
+
111
+        for (int i = 0; i < array.length; i++) {
112
+            copy[i] = array[i];
113
+        }
114
+
115
+        return copy;
116
+    }
117
+
118
+    public T[] removeElementFromArray(T[] array, T element) {
119
+        T[] newArray = createEmptyGenericArray(array, array.length - 1);
120
+        int j = 0;
121
+
122
+        for (int i = 0; i < array.length; i++) {
123
+            if (array[i] != element) {
124
+                newArray[j] = array[i];
125
+                j++;
126
+            }
127
+        }
128
+
129
+        return newArray;
130
+    }
131
+
132
+    public T[] insertIntoArrayAt(T[] array, T element, int index) {
133
+        T[] newArray = createEmptyGenericArray(array, array.length + 1);
134
+        int j = 0;
135
+
136
+        for (int i = 0; i < newArray.length; i++) {
137
+            if (i != index) {
138
+                newArray[i] = array[j];
139
+                j++;
140
+            } else if (i == index) {
141
+                newArray[index] = element;
142
+            }
143
+        }
144
+
145
+        return newArray;
146
+    }
147
+
148
+    public int findMax(int[] array) {
149
+        int max = array[0];
150
+
151
+        for (int intValue : array) {
152
+            if (intValue > max) {
153
+                max = intValue;
154
+            }
155
+        }
156
+
157
+        return max;
158
+    }
159
+
160
+    public int findMin(int[] array) {
161
+        int min = array[0];
162
+
163
+        for (int intValue : array) {
164
+            if (intValue < min) {
165
+                min = intValue;
166
+            }
167
+        }
168
+
169
+        return min;
170
+    }
171
+
172
+    public MaxMin findMaxMinOfArray(int[] array) {
173
+        MaxMin maxMin = new MaxMin();
174
+        maxMin.max = findMax(array);
175
+        maxMin.min = findMin(array);
176
+
177
+        return maxMin;
178
+    }
179
+
180
+    //////////    //////////    //////////      CONTINUE HERE     //////////    //////////    //////////
181
+
182
+    public int countDupes(Integer[] array) {
183
+        int count = 0;
184
+
185
+        return count;
186
+    }
187
+
188
+    public Integer[] removeDupesFromArray(Integer[] array){
189
+        int dupeCount = 0;
190
+
191
+        Integer[] newArray = new Integer[array.length - dupeCount];
192
+
193
+        return newArray;
194
+    }
195
+
196
+    public Double find2ndLargestValueFromArray(Double[] array){
197
+        Double secondLargest = 0.0;
198
+
199
+        return secondLargest;
200
+    }
201
+
202
+    public ArrayList<T> makeMeAnArrayListFromArray(T[] array){
203
+        ArrayList<T> arrayList = new ArrayList<T>();
204
+
205
+        return arrayList;
206
+    }
207
+
208
+    public T[] makeMeAnArrayFromArrayList(ArrayList<T> arrayList) {
209
+        // ???? not really sure what i'm doing here
210
+
211
+        T[] array;
212
+
213
+        Class cl = arrayList.getClass();
214
+        if (!cl.isArray()) {
215
+            return null;
216
+        } else {
217
+            array = (T[]) Array.newInstance(cl.getComponentType(), arrayList.size());
218
+        }
219
+
220
+        return array;
221
+    }
222
+
223
+
224
+
225
+
226
+
2 227
 }

+ 15
- 0
arraz/src/main/java/MaxMin.java Ver arquivo

@@ -0,0 +1,15 @@
1
+public class MaxMin {
2
+
3
+    public int max;
4
+    public int min;
5
+
6
+    public MaxMin(){
7
+        this.max = 0;
8
+        this.min = 0;
9
+    }
10
+
11
+    public MaxMin(int max, int min){
12
+        this.max = max;
13
+        this.min = min;
14
+    }
15
+}

+ 16
- 0
arraz/src/main/java/OddEven.java Ver arquivo

@@ -0,0 +1,16 @@
1
+public class OddEven {
2
+
3
+    public int odds;
4
+    public int evens;
5
+
6
+    public OddEven() {
7
+        this.odds = 0;
8
+        this.evens = 0;
9
+    }
10
+
11
+    public OddEven(int odds, int evens) {
12
+        this.odds = odds;
13
+        this.evens = evens;
14
+    }
15
+
16
+}

+ 352
- 0
arraz/src/test/java/ArrazTest.java Ver arquivo

@@ -1,5 +1,357 @@
1
+import org.junit.Assert;
2
+import org.junit.Before;
3
+import org.junit.Test;
4
+
1 5
 import static org.junit.Assert.*;
2 6
 
3 7
 public class ArrazTest {
4 8
 
9
+    private Arraz arraz;
10
+    private int[] intArray;
11
+    private int[] intArray2;
12
+    private double[] doubleArray;
13
+    private double[] doubleArray2;
14
+
15
+    @Before
16
+    public void setUp() {
17
+        arraz = new Arraz();
18
+        intArray = new int[]{1, 2, 3, 4, 5};
19
+        intArray2 = new int[]{5, 6, 7, 8, 9, 10};
20
+        doubleArray = new double[]{11, 12, 13, 14, 15};
21
+        doubleArray2 = new double[]{15, 16, 17, 18, 19, 20};
22
+    }
23
+
24
+    @Test
25
+    public void sumValuesOfArray1() {
26
+        int expected = 15;
27
+        int actual = arraz.sumValuesOfArray(intArray);
28
+
29
+        Assert.assertEquals(expected, actual);
30
+    }
31
+
32
+    @Test
33
+    public void sumValuesOfArray2() {
34
+        int expected = 45;
35
+        int actual = arraz.sumValuesOfArray(intArray2);
36
+
37
+        Assert.assertEquals(expected, actual);
38
+    }
39
+
40
+    //////////    //////////    //////////
41
+
42
+    @Test
43
+    public void sumDoubleOfArray1() {
44
+        double expected = 65;
45
+        double actual = arraz.sumDoubleOfArray(doubleArray);
46
+
47
+        Assert.assertEquals(expected, actual, 1e-15);
48
+    }
49
+
50
+    @Test
51
+    public void sumDoubleOfArray2() {
52
+        double expected = 105;
53
+        double actual = arraz.sumDoubleOfArray(doubleArray2);
54
+
55
+        Assert.assertEquals(expected, actual, 1e-15);
56
+    }
57
+
58
+    //////////    //////////    //////////
59
+
60
+    @Test
61
+    public void averageOfArray1() {
62
+        int expected = 3;
63
+        int actual = arraz.averageOfArray(intArray);
64
+
65
+        Assert.assertEquals(expected, actual);
66
+    }
67
+
68
+    @Test
69
+    public void averageOfArray2() {
70
+        int expected = 7;
71
+        int actual = arraz.averageOfArray(intArray2);
72
+
73
+        Assert.assertEquals(expected, actual);
74
+    }
75
+
76
+    //////////    //////////    //////////
77
+
78
+    @Test
79
+    public void doubleAverageOfArray1() {
80
+        double expected = 13;
81
+        double actual = arraz.doubleAverageOfArray(doubleArray);
82
+
83
+        Assert.assertEquals(expected, actual, 1e-15);
84
+    }
85
+
86
+    @Test
87
+    public void doubleAverageOfArray2() {
88
+        double expected = 17.5;
89
+        double actual = arraz.doubleAverageOfArray(doubleArray2);
90
+
91
+        Assert.assertEquals(expected, actual, 1e-15);
92
+    }
93
+
94
+    //////////    //////////    //////////
95
+
96
+    @Test
97
+    public void containsValue1() {
98
+        Assert.assertTrue(arraz.containsValue(intArray, 5));
99
+        Assert.assertTrue(arraz.containsValue(intArray2, 5));
100
+    }
101
+
102
+    @Test
103
+    public void containsValue2() {
104
+        Assert.assertFalse(arraz.containsValue(intArray, 6));
105
+        Assert.assertFalse(arraz.containsValue(intArray2, 4));
106
+    }
107
+
108
+    //////////    //////////    //////////
109
+
110
+    @Test
111
+    public void reverseArray1() {
112
+        int[] expected = {5, 4, 3, 2, 1};
113
+        int[] actual = arraz.reverseArray(intArray);
114
+
115
+        for (int i = 0; i < expected.length; i++) {
116
+            Assert.assertEquals(expected[i], actual[i]);
117
+        }
118
+    }
119
+
120
+    @Test
121
+    public void reverseArray2() {
122
+        int[] expected = {10, 9, 8, 7, 6, 5};
123
+        int[] actual = arraz.reverseArray(intArray2);
124
+
125
+        for (int i = 0; i < expected.length; i++) {
126
+            Assert.assertEquals(expected[i], actual[i]);
127
+        }
128
+    }
129
+
130
+    //////////    //////////    //////////
131
+
132
+    @Test
133
+    public void getOddEvensOfArray1() {
134
+        OddEven expected = new OddEven(3, 2);
135
+        OddEven actual = arraz.getOddEvensOfArray(intArray);
136
+
137
+        Assert.assertEquals(expected.odds, actual.odds);
138
+        Assert.assertEquals(expected.evens, actual.evens);
139
+    }
140
+
141
+    @Test
142
+    public void getOddEvensOfArray2() {
143
+        OddEven expected = new OddEven(3, 3);
144
+        OddEven actual = arraz.getOddEvensOfArray(intArray2);
145
+
146
+        Assert.assertEquals(expected.odds, actual.odds);
147
+        Assert.assertEquals(expected.evens, actual.evens);
148
+    }
149
+
150
+    //////////    //////////    //////////
151
+
152
+    @Test
153
+    public void findIndexOf1() {
154
+        int expected = 3;
155
+        int actual = arraz.findIndexOf(intArray, 4);
156
+
157
+        Assert.assertEquals(expected, actual);
158
+    }
159
+
160
+    @Test
161
+    public void findIndexOf2() {
162
+        int expected = -1;
163
+        int actual = arraz.findIndexOf(intArray2, 1);
164
+
165
+        Assert.assertEquals(expected, actual);
166
+    }
167
+
168
+    //////////    //////////    //////////
169
+
170
+    @Test
171
+    public void createEmptyGenericArray() {
172
+        String[] expected = new String[]{"this", "is", "a", "good", "test"};
173
+        String[] actual = (String[]) arraz.createEmptyGenericArray(expected, expected.length);
174
+        actual[0] = expected[0];
175
+
176
+        Assert.assertEquals(expected.getClass(), actual.getClass());
177
+        Assert.assertEquals(expected[0].getClass(), actual[0].getClass());
178
+    }
179
+
180
+    //////////    //////////    //////////
181
+
182
+    @Test
183
+    public void copyArrayByIterator1() {
184
+        String[] expected = new String[]{"this", "is", "a", "good", "test"};
185
+        String[] actual = (String[]) arraz.copyArrayByIterator(expected);
186
+
187
+        for (int i = 0; i < expected.length; i++) {
188
+            Assert.assertEquals(expected[i], actual[i]);
189
+        }
190
+
191
+        Assert.assertEquals(expected, actual);
192
+    }
193
+
194
+    @Test
195
+    public void copyArrayByIterator2() {
196
+        Integer[] expected = new Integer[]{1, 2, 3, 4, 5};
197
+        Integer[] actual = (Integer[]) arraz.copyArrayByIterator(expected);
198
+
199
+        for (int i = 0; i < expected.length; i++) {
200
+            Assert.assertEquals(expected[i], actual[i]);
201
+        }
202
+
203
+        Assert.assertEquals(expected, actual);
204
+    }
205
+
206
+    //////////    //////////    //////////
207
+
208
+    @Test
209
+    public void copyArrayByLoop1() {
210
+        String[] expected = new String[]{"this", "is", "a", "good", "test"};
211
+        String[] actual = (String[]) arraz.copyArrayByLoop(expected);
212
+
213
+        for (int i = 0; i < expected.length; i++) {
214
+            Assert.assertEquals(expected[i], actual[i]);
215
+        }
216
+
217
+        Assert.assertEquals(expected, actual);
218
+    }
219
+
220
+    @Test
221
+    public void copyArrayByLoop2() {
222
+        Integer[] expected = new Integer[]{1, 2, 3, 4, 5};
223
+        Integer[] actual = (Integer[]) arraz.copyArrayByLoop(expected);
224
+
225
+        for (int i = 0; i < expected.length; i++) {
226
+            Assert.assertEquals(expected[i], actual[i]);
227
+        }
228
+
229
+        Assert.assertEquals(expected, actual);
230
+    }
231
+
232
+    //////////    //////////    //////////
233
+
234
+    @Test
235
+    public void removeElementFromArray1() {
236
+        String[] original = new String[]{"this", "is", "a", "good", "test"};
237
+
238
+        String[] expected = new String[]{"this", "is", "a", "test"};
239
+        String[] actual = (String[]) arraz.removeElementFromArray(original, "good");
240
+
241
+        Assert.assertEquals(expected, actual);
242
+    }
243
+
244
+    @Test
245
+    public void removeElementFromArray2() {
246
+        Integer[] original = new Integer[]{1, 2, 3, 4, 5};
247
+
248
+        Integer[] expected = new Integer[]{1, 2, 4, 5};
249
+        Integer[] actual = (Integer[]) arraz.removeElementFromArray(original, 3);
250
+
251
+        Assert.assertEquals(expected, actual);
252
+    }
253
+
254
+    //////////    //////////    //////////
255
+
256
+    @Test
257
+    public void insertIntoArrayAt1() {
258
+        String[] original = new String[]{"this", "is", "a", "good", "test"};
259
+
260
+        String[] expected = new String[]{"this", "is", "a", "good", "test", "!"};
261
+        String[] actual = (String[]) arraz.insertIntoArrayAt(original, "!", 5);
262
+
263
+        Assert.assertEquals(expected, actual);
264
+    }
265
+
266
+    @Test
267
+    public void insertIntoArrayAt2() {
268
+        Integer[] original = new Integer[]{1, 2, 3, 4, 5};
269
+
270
+        Integer[] expected = new Integer[]{1, 2, 3, 100, 4, 5};
271
+        Integer[] actual = (Integer[]) arraz.insertIntoArrayAt(original, 100, 3);
272
+
273
+        Assert.assertEquals(expected, actual);
274
+    }
275
+
276
+    //////////    //////////    //////////
277
+
278
+    @Test
279
+    public void findMax() {
280
+        int expected = 5;
281
+        int actual = arraz.findMax(intArray);
282
+
283
+        Assert.assertEquals(expected, actual);
284
+    }
285
+
286
+    @Test
287
+    public void findMin() {
288
+        int expected = 1;
289
+        int actual = arraz.findMin(intArray);
290
+
291
+        Assert.assertEquals(expected, actual);
292
+    }
293
+
294
+    //////////    //////////    //////////
295
+
296
+    @Test
297
+    public void findMaxMinOfArray1() {
298
+        MaxMin expected = new MaxMin(5, 1);
299
+        MaxMin actual = arraz.findMaxMinOfArray(intArray);
300
+
301
+        Assert.assertEquals(expected.max, actual.max);
302
+        Assert.assertEquals(expected.min, actual.min);
303
+    }
304
+
305
+    @Test
306
+    public void findMaxMinOfArray2() {
307
+        MaxMin expected = new MaxMin(10, 5);
308
+        MaxMin actual = arraz.findMaxMinOfArray(intArray2);
309
+
310
+        Assert.assertEquals(expected.max, actual.max);
311
+        Assert.assertEquals(expected.min, actual.min);
312
+    }
313
+
314
+    //////////    //////////    //////////
315
+
316
+    @Test
317
+    public void removeDupesFromArray1() {
318
+
319
+
320
+
321
+    }
322
+
323
+    @Test
324
+    public void removeDupesFromArray2() {
325
+    }
326
+
327
+    //////////    //////////    //////////
328
+
329
+    @Test
330
+    public void find2ndLargestValueFromArray1() {
331
+    }
332
+
333
+
334
+    @Test
335
+    public void find2ndLargestValueFromArray2() {
336
+    }
337
+
338
+    //////////    //////////    //////////
339
+
340
+    @Test
341
+    public void makeMeAnArrayListFromArray1() {
342
+    }
343
+
344
+    @Test
345
+    public void makeMeAnArrayListFromArray2() {
346
+    }
347
+
348
+    //////////    //////////    //////////
349
+
350
+    @Test
351
+    public void makeMeAnArrayFromArrayList1() {
352
+    }
353
+
354
+    @Test
355
+    public void makeMeAnArrayFromArrayList2() {
356
+    }
5 357
 }