diff --git a/src/lesson6/ArrayMethods.java b/src/lesson6/ArrayMethods.java new file mode 100644 index 0000000..632117e --- /dev/null +++ b/src/lesson6/ArrayMethods.java @@ -0,0 +1,37 @@ +package lesson6; + +public class ArrayMethods { + + public static int[] splitBy4(int [] array){ + for(int i = array.length - 1; i>0; i--){ + if(array[i] == 4) { + int[] part = new int[array.length - i - 1]; + System.arraycopy(array, (i + 1), part, 0, part.length); + return part; + } + } + throw new RuntimeException("В массиве не обнаружена цифра 4"); + } + + public static boolean isThereOneOrFour(int[] array){ + + boolean isThereOne = false; + boolean isThereFour = false; + + if(array.length == 0) return false; + else{ + for(int i = 0; i < array.length; i++){ + if(array[i] == 1) { + isThereOne = true; + }else{ + if(array[i] == 4){ + isThereFour = true; + } else { + return false; + } + } + } + return isThereFour && isThereOne; + } + } +} diff --git a/src/lesson6/IsThereOneOrFourTest.java b/src/lesson6/IsThereOneOrFourTest.java new file mode 100644 index 0000000..f1499b6 --- /dev/null +++ b/src/lesson6/IsThereOneOrFourTest.java @@ -0,0 +1,35 @@ +package lesson6; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; + +@RunWith(Parameterized.class) +public class IsThereOneOrFourTest{ + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{ + {new int[]{1, 1, 4}, true}, + {new int[]{1, 1, 4, 2, 8}, false}, + {new int[]{1, 1, 1}, false}, + {new int[]{4, 4, 4}, false}, + }); + } + + private int[] array; + private boolean result; + + public IsThereOneOrFourTest(int[] array, boolean result) { + this.array = array; + this.result = result; + } + + @Test + public void massTest() { + Assert.assertEquals(result, ArrayMethods.isThereOneOrFour(array)); + } +} diff --git a/src/lesson6/SplitBy4Test.java b/src/lesson6/SplitBy4Test.java new file mode 100644 index 0000000..78f42d4 --- /dev/null +++ b/src/lesson6/SplitBy4Test.java @@ -0,0 +1,34 @@ +package lesson6; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; + +@RunWith(Parameterized.class) +public class SplitBy4Test { + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{ + {new int[]{1, 2, 3, 4}, new int[]{}}, + {new int[]{1, 4, 3, 2, 1}, new int[]{3, 2, 1}}, + {new int[]{1, 2, 3, 4, 1, 2, 3, 4, 5, 6}, new int[]{5, 6}}, + }); + } + + private int[] array; + private int[] result; + + public SplitBy4Test(int[] array, int[] result){ + this.array = array; + this.result = result; + } + + @Test + public void test(){ + Assert.assertArrayEquals(result, ArrayMethods.splitBy4(array)); + } +}