APL2.1 - Controle Machine & DEV 2.3

This commit is contained in:
2022-06-01 12:19:24 +02:00
parent 79791b395c
commit e7f9cf7b1f
15 changed files with 819 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,19 @@
import java.util.Objects;
public class Largest {
/**
* Return the largest element in a list.
*
* @param list A list of integers
* @return The largest number in the given list
*/
public static int largest(int[] list) {
Objects.requireNonNull(list);
int index, max=Integer.MIN_VALUE;
for (index = 0; index < list.length; index++) {
max = list[index] > max ? list[index] : max;
}
return max;
}
}

Binary file not shown.

View File

@@ -0,0 +1,36 @@
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
public class TestLargest {
@Test
public void testSimple() {
assertEquals(9, Largest.largest(new int[] {9,8,7}));
}
@Test
public void testBoundary() {
assertEquals(Integer.MAX_VALUE, Largest.largest(new int[] {1, 28825, Integer.MAX_VALUE}));
}
@Test
public void testInverse() {
assert(3 != Largest.largest(new int[] {1, 3, 7}));
}
@Test
public void testCrosscheck() {
int[] a = new int[] {3, 1, 7};
Arrays.sort(a);
assert(Largest.largest(new int[] {3, 1, 7}) == a[a.length-1]);
}
@Test
public void testError() {
Largest.largest(new int[] {});
Largest.largest(null);
}
}