Jennifer Chao před 6 roky
rodič
revize
488343e38a
2 změnil soubory, kde provedl 494 přidání a 32 odebrání
  1. 167
    26
      arraz/src/main/java/Arraz.java
  2. 327
    6
      arraz/src/test/java/ArrazTest.java

+ 167
- 26
arraz/src/main/java/Arraz.java Zobrazit soubor

@@ -1,9 +1,5 @@
1 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;
2
+import java.util.*;
7 3
 
8 4
 public class Arraz<T> {
9 5
 
@@ -177,51 +173,196 @@ public class Arraz<T> {
177 173
         return maxMin;
178 174
     }
179 175
 
180
-    //////////    //////////    //////////      CONTINUE HERE     //////////    //////////    //////////
176
+    public Integer[] removeDupesFromArray(Integer[] array) {
177
+        List<Integer> noDupes = new ArrayList<>();
181 178
 
182
-    public int countDupes(Integer[] array) {
183
-        int count = 0;
179
+        for (Integer integer : array) {
180
+            if (!noDupes.contains(integer)) {
181
+                noDupes.add(integer);
182
+            }
183
+        }
184
+
185
+        Integer[] newArray = new Integer[noDupes.size()];
186
+        int index = 0;
187
+
188
+        for (Integer integer : noDupes) {
189
+            newArray[index] = integer;
190
+            index++;
191
+        }
184 192
 
185
-        return count;
193
+        return newArray;
186 194
     }
187 195
 
188
-    public Integer[] removeDupesFromArray(Integer[] array){
189
-        int dupeCount = 0;
196
+    public Double findLargest(Double[] array) {
197
+        Double largest = array[0];
190 198
 
191
-        Integer[] newArray = new Integer[array.length - dupeCount];
199
+        for (int i = 0; i < array.length; i++) {
200
+            for (int j = i + 1; j < array.length; j++) {
201
+                if (largest < array[j]) {
202
+                    largest = array[j];
203
+                }
204
+            }
205
+        }
192 206
 
193
-        return newArray;
207
+        return largest;
194 208
     }
195 209
 
196
-    public Double find2ndLargestValueFromArray(Double[] array){
197
-        Double secondLargest = 0.0;
210
+    public int findIndexOfLargest(Double[] array) {
211
+        Double largest = findLargest(array);
212
+        int indexOfLargest = 0;
198 213
 
199
-        return secondLargest;
214
+        for (int i = 0; i < array.length; i++) {
215
+            if (array[i] == largest) {
216
+                indexOfLargest = i;
217
+            }
218
+        }
219
+
220
+        return indexOfLargest;
200 221
     }
201 222
 
202
-    public ArrayList<T> makeMeAnArrayListFromArray(T[] array){
223
+    public Double find2ndLargestValueFromArray(Double[] array) {
224
+        int indexOfLargest = findIndexOfLargest(array);
225
+        array[indexOfLargest] = 0.0;
226
+
227
+        return findLargest(array);
228
+    }
229
+
230
+    public ArrayList<T> makeMeAnArrayListFromArray(T[] array) {
203 231
         ArrayList<T> arrayList = new ArrayList<T>();
204 232
 
233
+        for (T element : array) {
234
+            arrayList.add(element);
235
+        }
236
+
205 237
         return arrayList;
206 238
     }
207 239
 
208
-    public T[] makeMeAnArrayFromArrayList(ArrayList<T> arrayList) {
209
-        // ???? not really sure what i'm doing here
240
+    public T[] makeMeAnArrayFromArrayList(ArrayList<T> arrayList, T[] targetClass) {
241
+        T[] newArray = createEmptyGenericArray((targetClass), arrayList.size());
242
+        int index = 0;
243
+
244
+        for (Object object : arrayList) {
245
+            newArray[index] = (T) object;
246
+            index++;
247
+        }
248
+
249
+        return newArray;
250
+    }
251
+
252
+    public boolean check2ArraysForEquals(T[] array1, T[] array2){
253
+        if (Arrays.equals(array1, array2)) {
254
+            return true;
255
+        }
256
+        return false;
257
+    }
210 258
 
211
-        T[] array;
259
+    public double averageArrayWithoutMaxMin(int[] array){
260
+        double sum = 0;
261
+        int dupes = 0;
212 262
 
213
-        Class cl = arrayList.getClass();
214
-        if (!cl.isArray()) {
215
-            return null;
216
-        } else {
217
-            array = (T[]) Array.newInstance(cl.getComponentType(), arrayList.size());
263
+        for (int integer : array) {
264
+            if (integer != findMax(array) && integer != findMin(array)) {
265
+                sum += integer;
266
+            } else {
267
+                dupes++;
268
+            }
269
+        }
270
+
271
+        return sum / (array.length - dupes);
272
+    }
273
+
274
+    public boolean arrayHas65And77(int[] array){
275
+        if (containsValue(array, 65) && containsValue(array, 77)) {
276
+            return true;
277
+        }
278
+
279
+        return false;
280
+    }
281
+
282
+    public boolean theTotalofTensIs30(int[] array){
283
+        int sum = 0;
284
+
285
+        for (int integer : array) {
286
+            if (integer == 10) {
287
+                sum += integer;
288
+            }
289
+        }
290
+
291
+        if (sum == 30) {
292
+            return true;
293
+        }
294
+
295
+        return false;
296
+    }
297
+
298
+
299
+    public int findSmallestOfArray(int[] array){
300
+        return findMin(array);
301
+    }
302
+
303
+    public int findSecondSmallestOfArray(int[] array){
304
+        int indexOfSmallest = findIndexOf(array, findSmallestOfArray(array));
305
+        array[indexOfSmallest] = findMax(array);
306
+
307
+        return findMin(array);
308
+    }
309
+
310
+    public int[] makeMeACopyPlease(int[] array){
311
+       return reverseArray(array);
312
+    }
313
+
314
+    public T[] removeFromIndex(T[] array, int index) {
315
+        T[] newArray = createEmptyGenericArray(array, array.length - 1);
316
+        int j = 0;
317
+
318
+        for (int i = 0; i < array.length; i++) {
319
+            if (i != index) {
320
+                newArray[j] = array[i];
321
+                j++;
322
+            }
218 323
         }
324
+        return newArray;
325
+    }
219 326
 
220
-        return array;
327
+    public T[] removeLastItemAndCopy(T[] array) {
328
+        T[] newArray = removeFromIndex(array, array.length - 1);
329
+
330
+        return newArray;
221 331
     }
222 332
 
333
+    public T[] removeFirstItemAndCopy(T[] array) {
334
+        T[] newArray = removeFromIndex(array, 0);
223 335
 
336
+        return newArray;
337
+    }
224 338
 
339
+    public T[] insertAtStartAndCopy(T[] array, T element) {
340
+        T[] newArray = insertIntoArrayAt(array, element, 0);
225 341
 
342
+        return newArray;
343
+    }
344
+
345
+    public T[] insertAtEndAndCopy(T[] array, T element) {
346
+        T[] newArray = insertIntoArrayAt(array, element, array.length);
347
+
348
+        return newArray;
349
+    }
350
+
351
+    public List[] sortArrayIntoEvensThenOdds(int[] array){
352
+        List[] evensThenOdds = new ArrayList[2];
353
+        List<Integer> evens = new ArrayList<>();
354
+        List<Integer> odds = new ArrayList<>();
355
+
356
+        for (int integer : array) {
357
+            if (integer % 2 == 0) {
358
+                evens.add(integer);
359
+            } else {
360
+                odds.add(integer);
361
+            }
362
+        }
363
+
364
+        evensThenOdds[0] = evens; evensThenOdds[1] = odds;
365
+        return evensThenOdds;
366
+    }
226 367
 
227 368
 }

+ 327
- 6
arraz/src/test/java/ArrazTest.java Zobrazit soubor

@@ -2,7 +2,8 @@ import org.junit.Assert;
2 2
 import org.junit.Before;
3 3
 import org.junit.Test;
4 4
 
5
-import static org.junit.Assert.*;
5
+import java.util.ArrayList;
6
+import java.util.List;
6 7
 
7 8
 public class ArrazTest {
8 9
 
@@ -277,16 +278,20 @@ public class ArrazTest {
277 278
 
278 279
     @Test
279 280
     public void findMax() {
280
-        int expected = 5;
281
-        int actual = arraz.findMax(intArray);
281
+        int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
282
+
283
+        int expected = 102;
284
+        int actual = arraz.findMax(spiffyHandyIntArray);
282 285
 
283 286
         Assert.assertEquals(expected, actual);
284 287
     }
285 288
 
286 289
     @Test
287 290
     public void findMin() {
288
-        int expected = 1;
289
-        int actual = arraz.findMin(intArray);
291
+        int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
292
+
293
+        int expected = -32;
294
+        int actual = arraz.findMin(spiffyHandyIntArray);
290 295
 
291 296
         Assert.assertEquals(expected, actual);
292 297
     }
@@ -313,45 +318,361 @@ public class ArrazTest {
313 318
 
314 319
     //////////    //////////    //////////
315 320
 
321
+
316 322
     @Test
317 323
     public void removeDupesFromArray1() {
324
+        Integer[] original = new Integer[]{2, 2, 1, 3, 4, 5, 5, 6, 2, 1, 8};
318 325
 
326
+        Integer[] expected = new Integer[]{2, 1, 3, 4, 5, 6, 8};
327
+        Integer[] actual = arraz.removeDupesFromArray(original);
319 328
 
320
-
329
+        Assert.assertEquals(expected, actual);
321 330
     }
322 331
 
323 332
     @Test
324 333
     public void removeDupesFromArray2() {
334
+        Integer[] original = new Integer[]{10, 6, 20, 22, 28, 5, 999, 5};
335
+
336
+        Integer[] expected = new Integer[]{10, 6, 20, 22, 28, 5, 999};
337
+        Integer[] actual = arraz.removeDupesFromArray(original);
338
+
339
+        Assert.assertEquals(expected, actual);
325 340
     }
326 341
 
327 342
     //////////    //////////    //////////
328 343
 
329 344
     @Test
330 345
     public void find2ndLargestValueFromArray1() {
346
+        Double[] original = new Double[]{2.4, 3.0, 6.5, 7.1, 2.8, 1.1, 0.5};
347
+
348
+        Double expected = 6.5;
349
+        Double actual = arraz.find2ndLargestValueFromArray(original);
350
+
351
+        Assert.assertEquals(expected, actual, 1e-15);
331 352
     }
332 353
 
333 354
 
334 355
     @Test
335 356
     public void find2ndLargestValueFromArray2() {
357
+        Double[] original = new Double[]{1.5, 1.3, 1.4, 1.2, 1.1};
358
+
359
+        Double expected = 1.4;
360
+        Double actual = arraz.find2ndLargestValueFromArray(original);
361
+
362
+        Assert.assertEquals(expected, actual, 1e-15);
336 363
     }
337 364
 
338 365
     //////////    //////////    //////////
339 366
 
340 367
     @Test
341 368
     public void makeMeAnArrayListFromArray1() {
369
+        Integer[] original = new Integer[]{1, 2, 3};
370
+
371
+        ArrayList<Integer> expected = new ArrayList<>();
372
+        expected.add(1);
373
+        expected.add(2);
374
+        expected.add(3);
375
+
376
+        ArrayList<Integer> actual = arraz.makeMeAnArrayListFromArray(original);
377
+
378
+        Assert.assertEquals(expected, actual);
342 379
     }
343 380
 
344 381
     @Test
345 382
     public void makeMeAnArrayListFromArray2() {
383
+        String[] original = new String[]{"I", "am", "very", "hungry", "!"};
384
+
385
+        ArrayList<String> expected = new ArrayList<>();
386
+        expected.add("I");
387
+        expected.add("am");
388
+        expected.add("very");
389
+        expected.add("hungry");
390
+        expected.add("!");
391
+
392
+        ArrayList<String> actual = arraz.makeMeAnArrayListFromArray(original);
393
+
394
+        Assert.assertEquals(expected, actual);
346 395
     }
347 396
 
348 397
     //////////    //////////    //////////
349 398
 
350 399
     @Test
351 400
     public void makeMeAnArrayFromArrayList1() {
401
+        ArrayList<Integer> original = new ArrayList<>();
402
+        original.add(6);
403
+        original.add(20);
404
+        original.add(92);
405
+
406
+        Integer[] expected = new Integer[]{6, 20, 92};
407
+        Integer[] actual = (Integer[]) arraz.makeMeAnArrayFromArrayList(original, new Integer[0]);
408
+
409
+        Assert.assertEquals(expected, actual);
352 410
     }
353 411
 
354 412
     @Test
355 413
     public void makeMeAnArrayFromArrayList2() {
414
+        ArrayList<String> original = new ArrayList<>();
415
+        original.add("hello");
416
+        original.add("goodbye");
417
+
418
+        String[] expected = new String[]{"hello", "goodbye"};
419
+        String[] actual = (String[]) arraz.makeMeAnArrayFromArrayList(original, new String[0]);
420
+
421
+        Assert.assertEquals(expected, actual);
422
+    }
423
+
424
+    //////////    //////////    //////////
425
+
426
+    @Test
427
+    public void check2ArraysForEquals1() {
428
+        String[] string1 = "I am starting to hate arrays".split(" ");
429
+        String[] string2 = new String[]{"I", "am", "starting", "to", "hate", "arrays"};
430
+
431
+        Assert.assertTrue(arraz.check2ArraysForEquals(string1, string2));
432
+    }
433
+
434
+    @Test
435
+    public void check2ArraysForEquals2() {
436
+        Double[] double1 = new Double[]{1.0, 0.5, 3.6, 38.4, 17.3, 62.0, 9.0, 3.375, 0.0, 3.14159};
437
+        Double[] double2 = new Double[]{1.0, 0.5, 3.6, 38.4, 17.3, 62.1, 9.0, 3.375, 0.0, 3.14159};
438
+
439
+        Assert.assertFalse(arraz.check2ArraysForEquals(double1, double2));
440
+    }
441
+
442
+    //////////    //////////    //////////
443
+
444
+    @Test
445
+    public void averageArrayWithoutMaxMin1() {
446
+        String expected = "3.00";
447
+        String actual = String.format("%.2f", arraz.averageArrayWithoutMaxMin(intArray));
448
+
449
+        Assert.assertEquals(expected, actual);
450
+    }
451
+
452
+    @Test
453
+    public void averageArrayWithoutMaxMin2() {
454
+        int[] original = new int[]{9, 9, 2, 5, -18, 5, 2, 35, 291, 291, 291, -18};
455
+
456
+        String expected = "9.57";
457
+        String actual = String.format("%.2f", arraz.averageArrayWithoutMaxMin(original));
458
+
459
+        Assert.assertEquals(expected, actual);
460
+    }
461
+
462
+    //////////    //////////    //////////
463
+
464
+    @Test
465
+    public void arrayHas65And77_1() {
466
+        int[] original = new int[]{1, 1, 2, 65, 64, 98, 78};
467
+
468
+        Assert.assertFalse(arraz.arrayHas65And77(original));
469
+    }
470
+
471
+    @Test
472
+    public void arrayHas65And77_2() {
473
+        int[] original = new int[]{1, 1, 2, 65, 64, 98, 77};
474
+
475
+        Assert.assertTrue(arraz.arrayHas65And77(original));
476
+    }
477
+
478
+    //////////    //////////    //////////
479
+
480
+    @Test
481
+    public void theTotalofTensIs30_1() {
482
+        int[] original = new int[]{10, 1, 2, 65, 10, 64, 10, 98, 77};
483
+
484
+        Assert.assertTrue(arraz.theTotalofTensIs30(original));
485
+    }
486
+
487
+    @Test
488
+    public void theTotalofTensIs30_2() {
489
+        int[] original = new int[]{10, 1, 2, 10, 65, 10, 64, 10, 98, 77};
490
+
491
+        Assert.assertFalse(arraz.theTotalofTensIs30(original));
492
+    }
493
+
494
+    //////////    //////////    //////////
495
+
496
+    @Test
497
+    public void findSmallestOfArray1() {
498
+        int expected = 1;
499
+        int actual = arraz.findSmallestOfArray(intArray);
500
+
501
+        Assert.assertEquals(expected, actual);
502
+    }
503
+
504
+    @Test
505
+    public void findSmallestOfArray2() {
506
+        int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
507
+
508
+        int expected = -32;
509
+        int actual = arraz.findSmallestOfArray(spiffyHandyIntArray);
510
+
511
+        Assert.assertEquals(expected, actual);
512
+    }
513
+
514
+    //////////    //////////    //////////
515
+
516
+    @Test
517
+    public void findSecondSmallestOfArray1() {
518
+        int expected = 2;
519
+        int actual = arraz.findSecondSmallestOfArray(intArray);
520
+
521
+        Assert.assertEquals(expected, actual);
522
+    }
523
+
524
+    @Test
525
+    public void findSecondSmallestOfArray2() {
526
+        int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
527
+
528
+        int expected = -7;
529
+        int actual = arraz.findSecondSmallestOfArray(spiffyHandyIntArray);
530
+
531
+        Assert.assertEquals(expected, actual);
532
+    }
533
+
534
+
535
+    //////////    //////////    //////////
536
+
537
+    @Test
538
+    public void makeMeACopyPlease1() {
539
+        int[] expected = new int[]{5, 4, 3, 2, 1};
540
+        int[] actual = arraz.makeMeACopyPlease(intArray);
541
+
542
+        for (int i = 0; i < expected.length; i++) {
543
+            Assert.assertEquals(expected[i], actual[i]);
544
+        }
545
+    }
546
+
547
+    @Test
548
+    public void makeMeACopyPlease2() {
549
+        int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
550
+
551
+        int[] expected = new int[]{8, 92, -32, 12, -7, 6, 102, 5, 4};
552
+        int[] actual = arraz.makeMeACopyPlease(spiffyHandyIntArray);
553
+
554
+        for (int i = 0; i < expected.length; i++) {
555
+            Assert.assertEquals(expected[i], actual[i]);
556
+        }
557
+    }
558
+
559
+    //////////    //////////    //////////
560
+
561
+    @Test
562
+    public void removeLastItemAndCopy1() {
563
+        Integer[] original = new Integer[]{10, 6, 20, 22, 28, 5, 999, 5};
564
+
565
+        Integer[] expected = new Integer[]{10, 6, 20, 22, 28, 5, 999};
566
+        Integer[] actual = (Integer[]) arraz.removeLastItemAndCopy(original);
567
+
568
+        Assert.assertEquals(expected, actual);
569
+    }
570
+
571
+    @Test
572
+    public void removeLastItemAndCopy2() {
573
+        String[] original = new String[]{"My", "name", "is", "Jenn", "Chao"};
574
+
575
+        String[] expected = new String[]{"My", "name", "is", "Jenn"};
576
+        String[] actual = (String[]) arraz.removeLastItemAndCopy(original);
577
+
578
+        Assert.assertEquals(expected, actual);
579
+    }
580
+
581
+    //////////    //////////    //////////
582
+
583
+    @Test
584
+    public void removeFirstItemAndCopy1() {
585
+        Integer[] original = new Integer[]{10, 6, 20, 22, 28, 5, 999, 5};
586
+
587
+        Integer[] expected = new Integer[]{6, 20, 22, 28, 5, 999, 5};
588
+        Integer[] actual = (Integer[]) arraz.removeFirstItemAndCopy(original);
589
+
590
+        Assert.assertEquals(expected, actual);
591
+    }
592
+
593
+    @Test
594
+    public void removeFirstItemAndCopy2() {
595
+        String[] original = new String[]{"My", "name", "is", "Jenn", "Chao"};
596
+
597
+        String[] expected = new String[]{"name", "is", "Jenn", "Chao"};
598
+        String[] actual = (String[]) arraz.removeFirstItemAndCopy(original);
599
+
600
+        Assert.assertEquals(expected, actual);
601
+    }
602
+
603
+    //////////    //////////    //////////
604
+
605
+    @Test
606
+    public void insertAtStartAndCopy1() {
607
+        Integer[] original = new Integer[]{1, 2, 3, 4, 5};
608
+
609
+        Integer[] expected = new Integer[]{99, 1, 2, 3, 4, 5};
610
+        Integer[] actual = (Integer[]) arraz.insertAtStartAndCopy(original, 99);
611
+
612
+        Assert.assertEquals(expected, actual);
613
+    }
614
+
615
+    @Test
616
+    public void insertAtStartAndCopy2() {
617
+        String[] original = new String[]{"I", "want", "fruit", "snacks"};
618
+
619
+        String[] expected = new String[]{"Seriously", "I", "want", "fruit", "snacks"};
620
+        String[] actual = (String[]) arraz.insertAtStartAndCopy(original, "Seriously");
621
+
622
+        Assert.assertEquals(expected, actual);
623
+
624
+    }
625
+
626
+    //////////    //////////    //////////
627
+
628
+    @Test
629
+    public void insertAtEndAndCopy1() {
630
+        Integer[] original = new Integer[]{1, 2, 3, 4, 5};
631
+
632
+        Integer[] expected = new Integer[]{1, 2, 3, 4, 5, 99};
633
+        Integer[] actual = (Integer[]) arraz.insertAtEndAndCopy(original, 99);
634
+
635
+        Assert.assertEquals(expected, actual);
636
+
637
+    }
638
+
639
+    @Test
640
+    public void insertAtEndAndCopy2() {
641
+        String[] original = new String[]{"I", "want", "fruit", "snacks"};
642
+
643
+        String[] expected = new String[]{"I", "want", "fruit", "snacks", "!"};
644
+        String[] actual = (String[]) arraz.insertAtEndAndCopy(original, "!");
645
+
646
+        Assert.assertEquals(expected, actual);
647
+    }
648
+
649
+    //////////    //////////    //////////
650
+
651
+    @Test
652
+    public void sortArrayIntoEvensThenOdds1() {
653
+        int[] spiffyHandyIntArray = new int[] {4, 5, 102, 6, -7, 12, -32, 92, 8};
654
+
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);
662
+
663
+        Assert.assertEquals(expected, actual);
664
+    }
665
+
666
+    @Test
667
+    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);
675
+
676
+        Assert.assertEquals(expected, actual);
356 677
     }
357 678
 }