jasamuels vor 6 Jahren
Ursprung
Commit
afad44e561
3 geänderte Dateien mit 205 neuen und 11 gelöschten Zeilen
  1. 106
    10
      arraz/src/main/java/Arraz.java
  2. 12
    0
      arraz/src/main/java/OddEven.java
  3. 87
    1
      arraz/src/test/java/ArrazTest.java

+ 106
- 10
arraz/src/main/java/Arraz.java Datei anzeigen

@@ -1,4 +1,4 @@
1
-public class Arraz {
1
+public class Arraz <T> {
2 2
 
3 3
     public static int sumValuesOfArray (int[] array){
4 4
 
@@ -11,15 +11,111 @@ public class Arraz {
11 11
         return sum;
12 12
     }
13 13
 
14
-//            sumDoublesOfArray
15
-//    averageOfArray
16
-//            doubleAverageOfArray
17
-//    containsValue
18
-//            reverseArray
19
-//    getOddEvensOfArray
20
-//            findIndexOf
21
-//    copyArrayByIterator
22
-//            copyArrayByLoop
14
+    public static double sumDoublesOfArray (double[] array){
15
+
16
+        double sum = 0;
17
+
18
+        for (double i : array)
19
+        {
20
+            sum += i;
21
+        }
22
+        return sum;
23
+
24
+    }
25
+
26
+    public static int averageOfArray (int[] array){
27
+
28
+        int average = 0;
29
+
30
+        for (int i : array)
31
+        {
32
+            average += i;
33
+        }
34
+
35
+        return average / array.length;
36
+    }
37
+
38
+    public static double doubleAverageOfArray(double[] array) {
39
+
40
+        double average = 0;
41
+
42
+        for (double i : array)
43
+        {
44
+            average += i;
45
+        }
46
+
47
+        return average / array.length;
48
+    }
49
+
50
+    public static boolean containsValue (int[] array, int value) {
51
+
52
+        boolean answer = false;
53
+
54
+        for (int i : array) {
55
+
56
+            if (i == value) {
57
+                answer = true;
58
+            }
59
+        }
60
+        return answer;
61
+    }
62
+
63
+    public static int[] reverseArray (int[] array){
64
+
65
+        //initialize
66
+        int end = array.length - 1;
67
+        int counter = 0;
68
+        int[] newArr = new int[array.length];
69
+
70
+        //reverse
71
+        for (int i = end; i >= 0; i--){
72
+            newArr[counter] = array[i];
73
+            counter++;
74
+        }
75
+        return newArr;
76
+    }
77
+
78
+    public static OddEven getOddEvensOfArray(int[] array) {
79
+
80
+        //initialize
81
+        int evens = 0;
82
+        int odds = 0;
83
+
84
+        //evaluate
85
+        for (int i : array) {
86
+            if (i % 2 == 0) {
87
+                evens++;
88
+            } else {
89
+                odds++;
90
+            }
91
+        }
92
+
93
+        return new OddEven(odds, evens);
94
+    }
95
+
96
+
97
+    public int findIndexOf (T[] array, T val) {
98
+
99
+        int index = -1;
100
+
101
+        for (int i = 0; i < array.length; i++) {
102
+            if (array[i].equals(val)){
103
+                index = i;
104
+            }
105
+        }
106
+
107
+        return index;
108
+    }
109
+
110
+    public static void copyArrayByIterator (){
111
+
112
+    }
113
+
114
+    public static void copyArrayByLoop(){
115
+
116
+
117
+    }
118
+
23 119
 //    removeElementFromArray
24 120
 //            insertIntoArrayAt
25 121
 //    findMaxMinOfArray

+ 12
- 0
arraz/src/main/java/OddEven.java Datei anzeigen

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

+ 87
- 1
arraz/src/test/java/ArrazTest.java Datei anzeigen

@@ -1,11 +1,17 @@
1 1
 import junit.framework.Assert;
2 2
 import org.junit.Test;
3 3
 
4
+import java.util.Arrays;
5
+
4 6
 import static org.junit.Assert.*;
5 7
 
6 8
 public class ArrazTest {
7 9
 
8
-    int[] spiffyHandyIntArray = new int[] {4,5,102,6,-7,12,-32,92,8};
10
+    private int[] spiffyHandyIntArray = new int[] {4,5,102,6,-7,12,-32,92,8};
11
+
12
+    private double[] spiffyHandyDoubleArray = new double[] {1.0, 0.5, 3.6, 38.4, 17.3, 62.0, 9.0, 3.375, 0, 3.14159};
13
+
14
+    Arraz whySoDifficult = new Arraz();
9 15
 
10 16
     @Test
11 17
     public void sumValuesOfArray() {
@@ -15,4 +21,84 @@ public class ArrazTest {
15 21
         Assert.assertEquals(sum, Arraz.sumValuesOfArray(spiffyHandyIntArray));
16 22
 
17 23
     }
24
+
25
+    @Test
26
+    public void sumDoublesOfArray() {
27
+
28
+        double sum = 138.31659000000002;
29
+
30
+        Assert.assertEquals(sum, Arraz.sumDoublesOfArray(spiffyHandyDoubleArray));
31
+
32
+    }
33
+
34
+    @Test
35
+    public void averageOfArray() {
36
+
37
+        int average = 21;
38
+
39
+        Assert.assertEquals(average, Arraz.averageOfArray(spiffyHandyIntArray));
40
+
41
+    }
42
+
43
+    @Test
44
+    public void doubleAverageOfArray() {
45
+
46
+        double average = 13.831659000000002;
47
+
48
+        Assert.assertEquals(average, Arraz.doubleAverageOfArray(spiffyHandyDoubleArray));
49
+
50
+    }
51
+
52
+    @Test
53
+    public void containsValue() {
54
+
55
+        Assert.assertTrue(Arraz.containsValue(spiffyHandyIntArray, 102));
56
+        Assert.assertFalse(Arraz.containsValue(spiffyHandyIntArray, 103));
57
+
58
+    }
59
+
60
+
61
+    @Test
62
+    public void reverseArray() {
63
+
64
+        int[] expected = new int[] {8, 92, -32, 12, -7, 6, 102, 5, 4};
65
+
66
+        Assert.assertEquals(Arrays.toString(expected), Arrays.toString(Arraz.reverseArray(spiffyHandyIntArray)));
67
+
68
+    }
69
+
70
+    @Test
71
+    public void getOddEvensOfArray() {
72
+
73
+        OddEven result = new OddEven(2, 7);
74
+
75
+        Assert.assertEquals(result.odds, Arraz.getOddEvensOfArray(spiffyHandyIntArray).odds);
76
+        Assert.assertEquals(result.evens, Arraz.getOddEvensOfArray(spiffyHandyIntArray).evens);
77
+
78
+    }
79
+
80
+    @Test
81
+    @SuppressWarnings("unchecked")
82
+    public void findIndexOf() {
83
+
84
+        //Given
85
+        int expected = 4;
86
+        int expectedtwo = -1;
87
+        int val = -7;
88
+        int valtwo = 123456789;
89
+        Integer[] damnedObjects = new Integer[] {4,5,102,6,-7,12,-32,92,8};
90
+
91
+        //Assert
92
+        Assert.assertEquals(expected, whySoDifficult.findIndexOf(damnedObjects, val));
93
+        Assert.assertEquals(expectedtwo, whySoDifficult.findIndexOf(damnedObjects, valtwo));
94
+
95
+    }
96
+
97
+    @Test
98
+    public void copyArrayByIterator() {
99
+    }
100
+
101
+    @Test
102
+    public void copyArrayByLoop() {
103
+    }
18 104
 }