Explorar el Código

100% coverage

Jennifer Chao hace 6 años
padre
commit
0959bc25c4
Se han modificado 2 ficheros con 30 adiciones y 41 borrados
  1. 20
    24
      arraz/src/main/java/Arraz.java
  2. 10
    17
      arraz/src/test/java/ArrazTest.java

+ 20
- 24
arraz/src/main/java/Arraz.java Ver fichero

78
 
78
 
79
     public T[] createEmptyGenericArray(T[] array, int length) {
79
     public T[] createEmptyGenericArray(T[] array, int length) {
80
         Class cl = array.getClass();
80
         Class cl = array.getClass();
81
-        if (!cl.isArray()) {
82
-            return null;
83
-        } else {
84
-            T[] copy = (T[]) Array.newInstance(cl.getComponentType(), length);
85
-            return copy;
86
-        }
81
+        T[] copy = (T[]) Array.newInstance(cl.getComponentType(), length);
82
+
83
+        return copy;
87
     }
84
     }
88
 
85
 
89
     public T[] copyArrayByIterator(T[] array) {
86
     public T[] copyArrayByIterator(T[] array) {
249
         return newArray;
246
         return newArray;
250
     }
247
     }
251
 
248
 
252
-    public boolean check2ArraysForEquals(T[] array1, T[] array2){
249
+    public boolean check2ArraysForEquals(T[] array1, T[] array2) {
253
         if (Arrays.equals(array1, array2)) {
250
         if (Arrays.equals(array1, array2)) {
254
             return true;
251
             return true;
255
         }
252
         }
256
         return false;
253
         return false;
257
     }
254
     }
258
 
255
 
259
-    public double averageArrayWithoutMaxMin(int[] array){
256
+    public double averageArrayWithoutMaxMin(int[] array) {
260
         double sum = 0;
257
         double sum = 0;
261
         int dupes = 0;
258
         int dupes = 0;
262
 
259
 
271
         return sum / (array.length - dupes);
268
         return sum / (array.length - dupes);
272
     }
269
     }
273
 
270
 
274
-    public boolean arrayHas65And77(int[] array){
271
+    public boolean arrayHas65And77(int[] array) {
275
         if (containsValue(array, 65) && containsValue(array, 77)) {
272
         if (containsValue(array, 65) && containsValue(array, 77)) {
276
             return true;
273
             return true;
277
         }
274
         }
279
         return false;
276
         return false;
280
     }
277
     }
281
 
278
 
282
-    public boolean theTotalofTensIs30(int[] array){
279
+    public boolean theTotalofTensIs30(int[] array) {
283
         int sum = 0;
280
         int sum = 0;
284
 
281
 
285
         for (int integer : array) {
282
         for (int integer : array) {
296
     }
293
     }
297
 
294
 
298
 
295
 
299
-    public int findSmallestOfArray(int[] array){
296
+    public int findSmallestOfArray(int[] array) {
300
         return findMin(array);
297
         return findMin(array);
301
     }
298
     }
302
 
299
 
303
-    public int findSecondSmallestOfArray(int[] array){
300
+    public int findSecondSmallestOfArray(int[] array) {
304
         int indexOfSmallest = findIndexOf(array, findSmallestOfArray(array));
301
         int indexOfSmallest = findIndexOf(array, findSmallestOfArray(array));
305
         array[indexOfSmallest] = findMax(array);
302
         array[indexOfSmallest] = findMax(array);
306
 
303
 
307
         return findMin(array);
304
         return findMin(array);
308
     }
305
     }
309
 
306
 
310
-    public int[] makeMeACopyPlease(int[] array){
311
-       return reverseArray(array);
307
+    public int[] makeMeACopyPlease(int[] array) {
308
+        return reverseArray(array);
312
     }
309
     }
313
 
310
 
314
     public T[] removeFromIndex(T[] array, int index) {
311
     public T[] removeFromIndex(T[] array, int index) {
348
         return newArray;
345
         return newArray;
349
     }
346
     }
350
 
347
 
351
-    public List[] sortArrayIntoEvensThenOdds(int[] array){
352
-        List[] evensThenOdds = new ArrayList[2];
353
-        List<Integer> evens = new ArrayList<>();
354
-        List<Integer> odds = new ArrayList<>();
348
+    public ArrayList<Integer> sortArrayIntoEvensThenOdds(int[] array) {
349
+        ArrayList<Integer> arrayList = new ArrayList<>();
350
+        int indexOfEvens = 0;
355
 
351
 
356
-        for (int integer : array) {
357
-            if (integer % 2 == 0) {
358
-                evens.add(integer);
352
+        for (int integer: array) {
353
+            if (integer % 2 ==0) {
354
+                arrayList.add(indexOfEvens, integer);
355
+                indexOfEvens++;
359
             } else {
356
             } else {
360
-                odds.add(integer);
357
+                arrayList.add(integer);
361
             }
358
             }
362
         }
359
         }
363
 
360
 
364
-        evensThenOdds[0] = evens; evensThenOdds[1] = odds;
365
-        return evensThenOdds;
361
+        return arrayList;
366
     }
362
     }
367
 
363
 
368
 }
364
 }

+ 10
- 17
arraz/src/test/java/ArrazTest.java Ver fichero

3
 import org.junit.Test;
3
 import org.junit.Test;
4
 
4
 
5
 import java.util.ArrayList;
5
 import java.util.ArrayList;
6
-import java.util.List;
7
 
6
 
8
 public class ArrazTest {
7
 public class ArrazTest {
9
 
8
 
404
         original.add(92);
403
         original.add(92);
405
 
404
 
406
         Integer[] expected = new Integer[]{6, 20, 92};
405
         Integer[] expected = new Integer[]{6, 20, 92};
407
-        Integer[] actual = (Integer[]) arraz.makeMeAnArrayFromArrayList(original, new Integer[0]);
406
+        Integer[] actual = new Integer[original.size()];
407
+        actual = (Integer[]) arraz.makeMeAnArrayFromArrayList(original, actual);
408
 
408
 
409
         Assert.assertEquals(expected, actual);
409
         Assert.assertEquals(expected, actual);
410
     }
410
     }
416
         original.add("goodbye");
416
         original.add("goodbye");
417
 
417
 
418
         String[] expected = new String[]{"hello", "goodbye"};
418
         String[] expected = new String[]{"hello", "goodbye"};
419
-        String[] actual = (String[]) arraz.makeMeAnArrayFromArrayList(original, new String[0]);
419
+        String[] actual = new String[original.size()];
420
+        actual = (String[]) arraz.makeMeAnArrayFromArrayList(original, new String[0]);
420
 
421
 
421
         Assert.assertEquals(expected, actual);
422
         Assert.assertEquals(expected, actual);
422
     }
423
     }
652
     public void sortArrayIntoEvensThenOdds1() {
653
     public void sortArrayIntoEvensThenOdds1() {
653
         int[] spiffyHandyIntArray = new int[] {4, 5, 102, 6, -7, 12, -32, 92, 8};
654
         int[] spiffyHandyIntArray = new int[] {4, 5, 102, 6, -7, 12, -32, 92, 8};
654
 
655
 
655
-        List<Integer> evens = new ArrayList<>();
656
-        evens.add(4); evens.add(102); evens.add(6); evens.add(12); evens.add(-32); evens.add(92); evens.add(8);
657
-        List<Integer> odds = new ArrayList<>();
658
-        odds.add(5); odds.add(-7);
659
-
660
-        List[] expected = new List[] {evens, odds};
661
-        List[] actual = arraz.sortArrayIntoEvensThenOdds(spiffyHandyIntArray);
656
+        ArrayList<Integer> expected = new ArrayList<>();
657
+        expected.add(4); expected.add(102); expected.add(6); expected.add(12); expected.add(-32); expected.add(92); expected.add(8); expected.add(5); expected.add(-7);
658
+        ArrayList<Integer> actual = arraz.sortArrayIntoEvensThenOdds(spiffyHandyIntArray);
662
 
659
 
663
         Assert.assertEquals(expected, actual);
660
         Assert.assertEquals(expected, actual);
664
     }
661
     }
665
 
662
 
666
     @Test
663
     @Test
667
     public void sortArrayIntoEvensThenOdds2() {
664
     public void sortArrayIntoEvensThenOdds2() {
668
-        List<Integer> evens = new ArrayList<>();
669
-        evens.add(2); evens.add(4);
670
-        List<Integer> odds = new ArrayList<>();
671
-        odds.add(1); odds.add(3); odds.add(5);
672
-
673
-        List[] expected = new List[] {evens, odds};
674
-        List[] actual = arraz.sortArrayIntoEvensThenOdds(intArray);
665
+        ArrayList<Integer> expected = new ArrayList<>();
666
+        expected.add(2); expected.add(4); expected.add(1); expected.add(3); expected.add(5);
667
+        ArrayList<Integer> actual = arraz.sortArrayIntoEvensThenOdds(intArray);
675
 
668
 
676
         Assert.assertEquals(expected, actual);
669
         Assert.assertEquals(expected, actual);
677
     }
670
     }