-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinTester_1ReddyPerrott.java
57 lines (52 loc) · 1.7 KB
/
MinTester_1ReddyPerrott.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Name: Nikhil Reddy and Samuel Perrott
// Assignment: AList-A6: Find2ndMin-PP Copy
import java.util.ArrayList;
import java.util.List;
public class MinTester_1ReddyPerrott {
public static void main( String[] args) {
List<Integer> stuff = new ArrayList<Integer>();
stuff.add(2);
stuff.add(12);
stuff.add(22);
stuff.add(17);
stuff.add(19);
stuff.add(-10);
stuff.add(-2);
System.out.println("Test 1: 2nd smallest is: " + find2ndMin(stuff));
stuff = new ArrayList<Integer>();
stuff.add(-2);
stuff.add(12);
stuff.add(22);
stuff.add(17);
stuff.add(19);
stuff.add(-1);
stuff.add(-2);
System.out.println("Test 2: 2nd smallest is: " + find2ndMin(stuff));
stuff = new ArrayList<Integer>();
stuff.add(-1);
stuff.add(-2);
stuff.add(22);
stuff.add(17);
stuff.add(19);
stuff.add(-1);
stuff.add(999999);
System.out.println("Test 3: 2nd smallest is: " + find2ndMin(stuff));
}
/**
@param ints An arrayList with at least 2 elements (precondition).
Postcondition: The arrayList ints is not mutilated or disturbed in any way.
@return the 2nd smallest value in the array (or the smallest if the min value occurs more than once)
*/
public static int find2ndMin(List<Integer> ints) {
int min = ints.get(0), secondMin = ints.get(1);
for (int i = 1; i < ints.size(); i++) {
if (ints.get(i) < min) {
secondMin = min;
min = ints.get(i);
} else if (ints.get(i) < secondMin) {
secondMin = ints.get(i);
}
}
return secondMin;
}
}