class Playground
class Playground {
/**
* Implement method that takes array of ints and returns the
* smallest element of the array.
*
* Example:
* findSmallestElement([0]) => 0
* findSmallestElement([1, 1, 1]) => 1
* findSmallestElement([-1, -4, -3, -2]) => -4
*/
private static int findSmallestElement(int[] nums) {
// WRITE YOUR CODE BELOW THIS LINE
// WRITE YOUR CODE ABOVE THIS LINE
}
public static void main(String[ ] args) {
test(findSmallestElement(new int[]{0}), 0, "arrayWithOneElement");
test(findSmallestElement(new int[0]), 0, "emptyArray");
test(findSmallestElement(new int[]{1, 1, 1, 1}), 1, "arrayWithSameElements");
test(findSmallestElement(new int[]{1, 2, 3, 4}), 1, "arrayWithLargestElementInTheEnd");
test(findSmallestElement(new int[]{1, 4, 3, 2}), 1, "arrayWithLargestElementInTheMiddle");
test(findSmallestElement(new int[]{-1, -4, -3, -2}), -4, "arrayWithNegativeElements");
}
private static void test(int actual, int expected, String testName) {
if (actual != expected) {
String errorMessage = String.format(
"Test %s failed: %s is not equal to expected %s",
testName,
actual,
expected);
System.out.println("ERROR: " + errorMessage);
} else {
System.out.println("SUCCESS: " + testName + " passed");
}
}
}
Are there any code examples left?
New code examples in category Java
-
Java 2022-03-27 21:35:04 Sort string array in case insensitive order and case sensitive order java
-
Java 2022-03-27 21:25:10 java -jar -l resources\es.porperties -i ejemplo.txt -o inject.bin
-
Java 2022-03-27 21:20:21 Debug & Fix a 2-Dimensional Array Java Console Application
-
Java 2022-03-27 20:40:19 TreeSet headSet(E toElement) method in java
-
Java 2022-03-27 19:30:06 close keyboard android
-
Java 2022-03-27 19:20:40 java measure execution time
-
Java 2022-03-27 19:10:06 how to add cardview support in android studio
-
Java 2022-03-27 18:55:08 android studio lower case letters on a button