BAEL-1586 Sum and Average in Java Array (#3718)

* Evaluation Article

* CR comments resolved

* BAEL-1586 Find Sum and Average in Java Array

* Revert "CR comments resolved"

This reverts commit c9bf88fcd58773d3df34e2a6cb4db44c3b54777d.

* Revert "Evaluation Article"

This reverts commit 41ad30313ab2c454350b5accdee531dba80e7ea9.

* BAEL-1586 Removed Author Description

* BAEL-1586 Find Sum And Avergae In Array

* remove excess whitespace
This commit is contained in:
raghav-jha 2018-03-07 21:29:50 +05:30 committed by pauljervis
parent 52d257a545
commit 4526bb6add
6 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package com.baeldung.array;
public class Find2ndLargestInArray {
public static int find2ndLargestElement(int[] array) {
int maxElement = array[0];
int secondLargestElement = -1;
for (int index = 0; index < array.length; index++) {
if (maxElement <= array[index]) {
secondLargestElement = maxElement;
maxElement = array[index];
} else if (secondLargestElement < array[index]) {
secondLargestElement = array[index];
}
}
return secondLargestElement;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.array;
import java.util.Arrays;
public class FindElementInArray {
public static boolean findGivenElementInArrayWithoutUsingStream(int[] array, int element) {
boolean actualResult = false;
for (int index = 0; index < array.length; index++) {
if (element == array[index]) {
actualResult = true;
break;
}
}
return actualResult;
}
public static boolean findGivenElementInArrayUsingStream(int[] array, int element) {
return Arrays.stream(array).filter(x -> element == x).findFirst().isPresent();
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.array;
import java.util.Arrays;
public class SumAndAverageInArray {
public static int findSumWithoutUsingStream(int[] array) {
int sum = 0;
for (int index = 0; index < array.length; index++) {
sum += array[index];
}
return sum;
}
public static int findSumUsingStream(int[] array) {
return Arrays.stream(array).sum();
}
public static double findAverageWithoutUsingStream(int[] array) {
int sum = findSumWithoutUsingStream(array);
return (double) sum / array.length;
}
public static double findAverageUsingStream(int[] array) {
return Arrays.stream(array).average().getAsDouble();
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.array;
import org.junit.Assert;
import org.junit.Test;
public class Find2ndLargestInArrayTest {
@Test
public void givenAnIntArray_thenFind2ndLargestElement() {
int[] array = { 1, 3, 24, 16, 87, 20 };
int expected2ndLargest = 24;
int actualSecondLargestElement = Find2ndLargestInArray.find2ndLargestElement(array);
Assert.assertEquals(expected2ndLargest, actualSecondLargestElement);
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.array;
import org.junit.Assert;
import org.junit.Test;
public class FindElementInArrayTest {
@Test
public void givenAnIntArray_whenNotUsingStream_thenFindAnElement() {
int[] array = { 1, 3, 4, 8, 19, 20 };
int element = 19;
boolean expectedResult = true;
boolean actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element);
Assert.assertEquals(expectedResult, actualResult);
element = 78;
expectedResult = false;
actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element);
Assert.assertEquals(expectedResult, actualResult);
}
@Test
public void givenAnIntArray_whenUsingStream_thenFindAnElement() {
int[] array = { 15, 16, 12, 18 };
int element = 16;
boolean expectedResult = true;
boolean actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element);
Assert.assertEquals(expectedResult, actualResult);
element = 20;
expectedResult = false;
actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element);
Assert.assertEquals(expectedResult, actualResult);
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.array;
import org.junit.Assert;
import org.junit.Test;
public class SumAndAverageInArrayTest {
@Test
public void givenAnIntArray_whenNotUsingStream_thenFindSum() {
int[] array = { 1, 3, 4, 8, 19, 20 };
int expectedSumOfArray = 55;
int actualSumOfArray = SumAndAverageInArray.findSumWithoutUsingStream(array);
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
}
@Test
public void givenAnIntArray_whenUsingStream_thenFindSum() {
int[] array = { 1, 3, 4, 8, 19, 20 };
int expectedSumOfArray = 55;
int actualSumOfArray = SumAndAverageInArray.findSumUsingStream(array);
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
}
@Test
public void givenAnIntArray_whenNotUsingStream_thenFindAverage() {
int[] array = { 1, 3, 4, 8, 19, 20 };
double expectedAvgOfArray = 9.17;
double actualAvgOfArray = SumAndAverageInArray.findAverageWithoutUsingStream(array);
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
}
@Test
public void givenAnIntArray_whenUsingStream_thenFindAverage() {
int[] array = { 1, 3, 4, 8, 19, 20 };
double expectedAvgOfArray = 9.17;
double actualAvgOfArray = SumAndAverageInArray.findAverageUsingStream(array);
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
}
}