Nick Satinover пре 6 година
родитељ
комит
6073970bbb
2 измењених фајлова са 167 додато и 0 уклоњено
  1. 53
    0
      arraz/src/main/java/Arraz.java
  2. 114
    0
      arraz/src/test/java/ArrazTest.java

+ 53
- 0
arraz/src/main/java/Arraz.java Прегледај датотеку

@@ -1,4 +1,5 @@
1 1
 import java.util.ArrayList;
2
+import java.util.Arrays;
2 3
 
3 4
 public class Arraz {
4 5
 
@@ -165,4 +166,56 @@ public class Arraz {
165 166
         return intsToRet;
166 167
     }
167 168
 
169
+    public double find2ndLargestValueFromArray(double[] dArr){
170
+        double max = Double.MIN_VALUE;
171
+        double secondMax = Double.MIN_VALUE;
172
+
173
+        for (double d:dArr) {
174
+            if (d > max){
175
+                max = d;
176
+            }
177
+        }
178
+
179
+        for (double d:dArr) {
180
+            if (d < max && d > secondMax){
181
+                secondMax = d;
182
+            }
183
+        }
184
+        return secondMax;
185
+    }
186
+
187
+    public ArrayList<Integer> makeMeAnArrayListFromArray(int[] intsArr){
188
+        ArrayList<Integer> retArr = new ArrayList<Integer>();
189
+        for (int i: intsArr) {
190
+            retArr.add(i);
191
+        }
192
+        return retArr;
193
+    }
194
+
195
+    public int[] makeMeAnArrayFromArrayList(ArrayList<Integer> intArr){
196
+        int[] retArr = new int[intArr.size()];
197
+
198
+        int index = 0;
199
+        for (int i:intArr) {
200
+            retArr[index] = i;
201
+            index++;
202
+        }
203
+
204
+        return retArr;
205
+    }
206
+
207
+    public boolean check2ArraysForEqual(int[] a, int[] b){
208
+        if (a.length != b.length){
209
+            return false;
210
+        } else {
211
+            for (int i = 0; i < a.length; i++) {
212
+                if (a[i] != b[i]){
213
+                    return false;
214
+                }
215
+            }
216
+        }
217
+        return true;
218
+    }
219
+
220
+
168 221
 }

+ 114
- 0
arraz/src/test/java/ArrazTest.java Прегледај датотеку

@@ -2,6 +2,8 @@ import org.junit.Assert;
2 2
 import org.junit.Before;
3 3
 import org.junit.Test;
4 4
 
5
+import java.util.ArrayList;
6
+
5 7
 import static org.junit.Assert.*;
6 8
 
7 9
 public class ArrazTest {
@@ -382,4 +384,116 @@ public class ArrazTest {
382 384
         //THEN
383 385
         Assert.assertEquals(expected, actual);
384 386
     }
387
+
388
+    @Test
389
+    public void test1find2ndLargestValueFromArray() {
390
+        //GIVEN
391
+        double[] arr = {10, 20.2, 30.3, 40.4, 50};
392
+        //WHEN
393
+        double expected = 40.4;
394
+
395
+        double actual = arraz.find2ndLargestValueFromArray(arr);
396
+        //THEN
397
+        Assert.assertEquals(expected, actual);
398
+    }
399
+
400
+    @Test
401
+    public void test2find2ndLargestValueFromArray() {
402
+        //GIVEN
403
+        double[] arr = {40.5, 500, 10, 20.2, 30.3, 40.4};
404
+        //WHEN
405
+        double expected = 40.5;
406
+
407
+        double actual = arraz.find2ndLargestValueFromArray(arr);
408
+        //THEN
409
+        Assert.assertEquals(expected, actual);
410
+    }
411
+
412
+    @Test
413
+    public void test1makeMeAnArrayListFromArray() {
414
+        //GIVEN
415
+        int[] arr = {1, 2, 3, 4, 5, 3};
416
+        //WHEN
417
+        int expected = 5;
418
+
419
+        int actual = arraz.makeMeAnArrayListFromArray(arr).get(4);
420
+        //THEN
421
+        Assert.assertEquals(expected, actual);
422
+    }
423
+
424
+    @Test
425
+    public void test2makeMeAnArrayListFromArray() {
426
+        //GIVEN
427
+        int[] arr = {1, 2, 3, 4, 5, 3, 1, 1, 1, 2, 3, 4, 5};
428
+        //WHEN
429
+        int expected = 13;
430
+
431
+        int actual = arraz.makeMeAnArrayListFromArray(arr).size();
432
+        //THEN
433
+        Assert.assertEquals(expected, actual);
434
+    }
435
+
436
+    @Test
437
+    public void test1makeMeAnArrayFromArrayList() {
438
+        //GIVEN
439
+        ArrayList<Integer> arr = new ArrayList<Integer>();
440
+        arr.add(1); arr.add(2); arr.add(3);
441
+        //WHEN
442
+        int expected = 1;
443
+
444
+        int[] actualArr = arraz.makeMeAnArrayFromArrayList(arr);
445
+        int actual = actualArr[0];
446
+        //THEN
447
+        Assert.assertEquals(expected, actual);
448
+    }
449
+
450
+    @Test
451
+    public void test2makeMeAnArrayFromArrayList() {
452
+        //GIVEN
453
+        ArrayList<Integer> arr = new ArrayList<Integer>();
454
+                arr.add(1); arr.add(2); arr.add(3);
455
+        //WHEN
456
+        Class expected = Integer.TYPE;
457
+
458
+        Class actual = arraz.makeMeAnArrayFromArrayList(arr).getClass().getComponentType();
459
+        //THEN
460
+        Assert.assertEquals(expected, actual);
461
+    }
462
+
463
+    @Test
464
+    public void test1check2ArraysForEqual() {
465
+        //GIVEN
466
+        int[] a = {1, 2, 3, 4, 5, 3};
467
+        int[] b = {1, 2, 3, 4, 5, 3};
468
+        //WHEN
469
+        boolean expected = true;
470
+
471
+        boolean actual = arraz.check2ArraysForEqual(a, b);
472
+        //THEN
473
+        Assert.assertEquals(expected, actual);
474
+    }
475
+
476
+    @Test
477
+    public void test2check2ArraysForEqual() {
478
+        int[] a = {1, 2, 3, 4, 5, 3};
479
+        int[] b = {1, 2, 3, 4, 5, 3, 1};
480
+        //WHEN
481
+        boolean expected = false;
482
+
483
+        boolean actual = arraz.check2ArraysForEqual(a, b);
484
+        //THEN
485
+        Assert.assertEquals(expected, actual);
486
+    }
487
+
488
+    @Test
489
+    public void test3check2ArraysForEqual() {
490
+        int[] a = {1, 2, 3, 4, 5, 3};
491
+        int[] b = {1, 2, 2, 4, 5, 3};
492
+        //WHEN
493
+        boolean expected = false;
494
+
495
+        boolean actual = arraz.check2ArraysForEqual(a, b);
496
+        //THEN
497
+        Assert.assertEquals(expected, actual);
498
+    }
385 499
 }