Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/lesson6/ArrayMethods.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
35 changes: 35 additions & 0 deletions src/lesson6/IsThereOneOrFourTest.java
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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));
}
}
34 changes: 34 additions & 0 deletions src/lesson6/SplitBy4Test.java
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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));
}
}