Nick Satinover 6 lat temu
rodzic
commit
7ac39742ab

+ 101
- 0
arraz/src/main/java/Arraz.java Wyświetl plik

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.stream.Stream;
6
+
1
 public class Arraz {
7
 public class Arraz {
2
 
8
 
3
     public int sumValuesOfArray(int[] intArr){
9
     public int sumValuesOfArray(int[] intArr){
48
 
54
 
49
         return retArr;
55
         return retArr;
50
     }
56
     }
57
+
58
+    public OddEven getOddEvensOfArray(int[] intsArr){
59
+        OddEven oddEven = new OddEven();
60
+        oddEven.evens = 0;
61
+        oddEven.odds = 0;
62
+
63
+        for (int i:intsArr) {
64
+            if(i % 2 == 0){
65
+                oddEven.evens++;
66
+            } else {
67
+                oddEven.odds++;
68
+            }
69
+        }
70
+        return oddEven;
71
+    }
72
+
73
+    public int findIndexOf(int[] intsArr, int index){
74
+        return intsArr[index];
75
+    }
76
+
77
+
78
+//    public Integer[] copyArrayByIterator(Integer[] intInput) {
79
+//        Integer[] retArr = new Integer[intInput.length];
80
+//
81
+//        Iterator<Integer> iterator = new Iterator<Integer>() {
82
+//            public boolean hasNext(int intValue) {
83
+//                return intValue == null ? true : false;
84
+//            }
85
+//
86
+//            public Integer next() {
87
+//                return null;
88
+//            }
89
+//
90
+//            public void remove() {
91
+//
92
+//            }
93
+//        }
94
+//        return retArr;
95
+//    }
96
+
97
+    public int[] copyArrayByLoop(int[] ints){
98
+        int[] retArr = new int[ints.length];
99
+        for (int i = 0; i < ints.length; i++) {
100
+            retArr[i] = ints[i];
101
+        }
102
+        return retArr;
103
+    }
104
+
105
+    public int[] removeElementFromArray(int[] ints, int removeInt){
106
+        int[] retArr = new int[ints.length - 1];
107
+
108
+        int index = 0;
109
+        for (int i:ints) {
110
+            if (i != removeInt){
111
+                retArr[index] = i;
112
+                index++;
113
+            }
114
+        }
115
+        return retArr;
116
+    }
117
+
118
+    public int[] insertIntoArrayAt(int[] ints, int index, int intToInsert){
119
+        int[] retArr = new int[ints.length + 1];
120
+
121
+        int intsIndex = 0;
122
+        for (int i = 0; i < retArr.length; i++){
123
+            if (i == index){
124
+                retArr[i] = intToInsert;
125
+            }
126
+            else {
127
+                retArr[i] = ints[intsIndex];
128
+                intsIndex++;
129
+            }
130
+        }
131
+        return retArr;
132
+    }
133
+
134
+    public MaxMin findMaxMinOfArray(int[] ints){
135
+        MaxMin maxMin = new MaxMin();
136
+        int minVal = Integer.MAX_VALUE;
137
+        int maxVal = Integer.MIN_VALUE;
138
+
139
+        for (int i:ints) {
140
+            if (i < minVal){
141
+                minVal = i;
142
+            }
143
+            if (i > maxVal){
144
+                maxVal = i;
145
+            }
146
+        }
147
+
148
+        maxMin.min = minVal;
149
+        maxMin.max = maxVal;
150
+        return maxMin;
151
+    }
51
 }
152
 }

+ 4
- 0
arraz/src/main/java/MaxMin.java Wyświetl plik

1
+public class MaxMin {
2
+    public int max;
3
+    public int min;
4
+}

+ 2
- 2
arraz/src/main/java/OddEven.java Wyświetl plik

1
 public class OddEven {
1
 public class OddEven {
2
-        private int odds;
3
-        private int evens;
2
+        public int odds;
3
+        public int evens;
4
 
4
 
5
     public OddEven() {
5
     public OddEven() {
6
     }
6
     }

+ 176
- 0
arraz/src/test/java/ArrazTest.java Wyświetl plik

169
         //THEN
169
         //THEN
170
         Assert.assertEquals(arr[0], actual[3]);
170
         Assert.assertEquals(arr[0], actual[3]);
171
     }
171
     }
172
+
173
+    @Test
174
+    public void test1getOddEvensOfArray(){
175
+        //GIVEN
176
+        int[] arr = {1, 2, 3, 4, 5};
177
+
178
+        //WHEN
179
+        int expected = 2;
180
+        int actual = arraz.getOddEvensOfArray(arr).evens;
181
+
182
+        //THEN
183
+        Assert.assertEquals(expected, actual);
184
+    }
185
+
186
+    @Test
187
+    public void test2getOddEvensOfArray(){
188
+        //GIVEN
189
+        int[] arr = {1, 2, 3, 4, 5};
190
+
191
+        //WHEN
192
+        int expected = 3;
193
+        int actual = arraz.getOddEvensOfArray(arr).odds;
194
+
195
+        //THEN
196
+        Assert.assertEquals(expected, actual);
197
+    }
198
+
199
+    @Test
200
+    public void test1findIndexOf() {
201
+        //GIVEN
202
+        int[] arr = {1, 2, 3, 4, 5};
203
+        int index = 4;
204
+        //WHEN
205
+        int expected = 5;
206
+        int actual = arraz.findIndexOf(arr, index);
207
+
208
+        //THEN
209
+        Assert.assertEquals(expected, actual);
210
+    }
211
+
212
+    @Test
213
+    public void test2findIndexOf() {
214
+        //GIVEN
215
+        int[] arr = {1, 2, 3, 4, 5};
216
+        int index = 1;
217
+        //WHEN
218
+        int expected = 2;
219
+        int actual = arraz.findIndexOf(arr, index);
220
+
221
+        //THEN
222
+        Assert.assertEquals(expected, actual);
223
+    }
224
+
225
+//    @Test
226
+//    public void test1copyArrayByIterator() {
227
+//        //GIVEN
228
+//        Object[] arr = {1, 2, 3, 4, 5};
229
+//        //WHEN
230
+//        Object[] retObject = arraz.copyArrayByIterator(arr);
231
+//        int expected = 3;
232
+//
233
+//        //THEN
234
+//        int actual = retObject.;
235
+//        Assert.assertEquals(expected, actual);
236
+//    }
237
+//
238
+//    @Test
239
+//    public void test2copyArrayByIterator() {
240
+//
241
+//    }
242
+
243
+    @Test
244
+    public void test1copyArrayByLoop() {
245
+        //GIVEN
246
+        int[] expected = {1, 2, 3, 4, 5};
247
+        //WHEN
248
+        int[] actual = arraz.copyArrayByLoop(expected);
249
+
250
+        //THEN
251
+        Assert.assertEquals(expected[0], actual[0]);
252
+    }
253
+
254
+    @Test
255
+    public void test2copyArrayByLoop() {
256
+        //GIVEN
257
+        int[] expected = {1, 2, 3, 4, 5};
258
+        //WHEN
259
+        int[] actual = arraz.copyArrayByLoop(expected);
260
+
261
+        //THEN
262
+        Assert.assertEquals(expected[expected.length - 1], actual[expected.length - 1]);
263
+    }
264
+
265
+    @Test
266
+    public void test1removeElementFromArray() {
267
+        //GIVEN
268
+        int[] arr = {1, 2, 3, 4, 5};
269
+        int removeInt = 5;
270
+        //WHEN
271
+        int expected = 4;
272
+        int actual = arraz.removeElementFromArray(arr, removeInt).length;
273
+        //THEN
274
+        Assert.assertEquals(expected, actual);
275
+    }
276
+
277
+    @Test
278
+    public void test2removeElementFromArray() {
279
+        //GIVEN
280
+        int[] arr = {1, 2, 3, 4, 5};
281
+        int removeInt = 1;
282
+        //WHEN
283
+        int expected = 2;
284
+        int[] actual = arraz.removeElementFromArray(arr, removeInt);
285
+        //THEN
286
+        Assert.assertEquals(expected, actual[0]);
287
+    }
288
+
289
+    @Test
290
+    public void test1insertIntoArrayAt() {
291
+        //GIVEN
292
+        int[] arr = {1, 2, 3, 4, 5};
293
+        int addInt = 6;
294
+        int atIndex = 3;
295
+        //WHEN
296
+        int expected = 6;
297
+        int[] retArr = arraz.insertIntoArrayAt(arr, atIndex, addInt);
298
+        int actual = retArr[atIndex];
299
+        //THEN
300
+        Assert.assertEquals(expected, actual);
301
+    }
302
+
303
+    @Test
304
+    public void test2insertIntoArrayAt() {
305
+        //GIVEN
306
+        int[] arr = {1, 2, 3, 4, 5};
307
+        int addInt = 6;
308
+        int atIndex = 5;
309
+        //WHEN
310
+        int expected = 6;
311
+        int[] retArr = arraz.insertIntoArrayAt(arr, atIndex, addInt);
312
+        int actual = retArr[atIndex];
313
+        //THEN
314
+        Assert.assertEquals(expected, actual);
315
+    }
316
+
317
+    @Test
318
+    public void test1findMaxMinOfArray() {
319
+        //GIVEN
320
+        int[] arr = {1, 2, 3, 4, 5};
321
+        //WHEN
322
+        int expectedMax = 5;
323
+        int expectedMin = 1;
324
+        MaxMin maxMin = arraz.findMaxMinOfArray(arr);
325
+
326
+        int actualMax = maxMin.max;
327
+        int actualMin = maxMin.min;
328
+        //THEN
329
+        Assert.assertEquals(expectedMax, actualMax);
330
+        Assert.assertEquals(expectedMin, actualMin);
331
+    }
332
+
333
+    @Test
334
+    public void test2findMaxMinOfArray() {
335
+        //GIVEN
336
+        int[] arr = {1, -2, 300, 4, 5};
337
+        //WHEN
338
+        int expectedMax = 300;
339
+        int expectedMin = -2;
340
+        MaxMin maxMin = arraz.findMaxMinOfArray(arr);
341
+
342
+        int actualMax = maxMin.max;
343
+        int actualMin = maxMin.min;
344
+        //THEN
345
+        Assert.assertEquals(expectedMax, actualMax);
346
+        Assert.assertEquals(expectedMin, actualMin);
347
+    }
172
 }
348
 }