import java.util.stream.IntStream; public class Area { public static long[] workOutArea(long[] values){ if (values.length % 2 != 0) { return null; } return IntStream.range(0, values.length / 2).mapToLong(i -> values[i * 2] * values[i * 2 + 1]).toArray(); } }
- import java.util.stream.IntStream;
- public class Area {
- public static long[] workOutArea(long[] values){
- if (values.length % 2 != 0) {
- return null;
- }
- return IntStream.range(0, values.length / 2).mapToLong(i -> values[i * 2] * values[i * 2 + 1]).toArray();
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertArrayEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void testNothing() { assertArrayEquals(new long[0], Area.workOutArea(new long[0])); } @Test public void testSomething() { long[] val1 = {4l,4l,5l,5l,6l,6l,7l,7l}; assertArrayEquals(Area.workOutArea(val1), new long[] {16l, 25l, 36l, 49l}); long[] val2 = {4l,4l,5l,5l,6l,6l,7l}; assertEquals(null, Area.workOutArea(val2)); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertArrayEquals;
- import org.junit.runners.JUnit4;
- public class SolutionTest {
- @Test
- public void testNothing() {
- assertArrayEquals(new long[0], Area.workOutArea(new long[0]));
- }
- @Test
- public void testSomething() {
- long[] val1 = {4l,4l,5l,5l,6l,6l,7l,7l};
Assert.assertArrayEquals(area.workOutArea(val1), new long[] {16l, 25l, 36l, 49l});- assertArrayEquals(Area.workOutArea(val1), new long[] {16l, 25l, 36l, 49l});
- long[] val2 = {4l,4l,5l,5l,6l,6l,7l};
assertEquals(null, area.workOutArea(val2));- assertEquals(null, Area.workOutArea(val2));
- }
- }