Array may be empty.
import java.util.*; public class Average { public static int averageFinder(int[] arr) { return (int)Arrays.stream(arr).average().orElse(0d); }; }
- import java.util.*;
- public class Average {
- public static int averageFinder(int[] arr) {
return (int)Arrays.stream(arr).average().getAsDouble();- return (int)Arrays.stream(arr).average().orElse(0d);
- };
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; // TODO: Replace examples and use TDD development by writing your own tests public class SolutionTest { @Test public void testSomething() { Average av = new Average(); int arr1[] = {96, 98}; assertEquals(97, av.averageFinder(arr1)); int arr2[] = {94, 96, 98}; assertEquals(96, av.averageFinder(arr2)); int arr3[] = {}; assertEquals(0, av.averageFinder(arr3)); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- // TODO: Replace examples and use TDD development by writing your own tests
- public class SolutionTest {
- @Test
- public void testSomething() {
- Average av = new Average();
- int arr1[] = {96, 98};
- assertEquals(97, av.averageFinder(arr1));
- int arr2[] = {94, 96, 98};
- assertEquals(96, av.averageFinder(arr2));
- int arr3[] = {};
- assertEquals(0, av.averageFinder(arr3));
- }
- }