Question: /** * Selects the kth maximum value from the array a. This method * throws IllegalArgumentException if a is null, has zero length, * or

/** * Selects the kth maximum value from the array a. This method * throws IllegalArgumentException if a is null, has zero length, * or if there is no kth maximum value. Note that there is no kth * maximum value if k < 1, k > a.length, or if k is larger than * the number of distinct values in the array. The array a is not * changed by this method. */ public static int kmax(int[] a, int k) { if ((a == null) || (a.length == 0) || (k < 1) || (k > a.length)) { throw new IllegalArgumentException(); } int[] b = Arrays.copyOf(a,a.length); // creates copy of sort Arrays.sort(b); int count = 0; int total = 0; for(int i = 0; i < b.length - 1; i++){ if (b[i] == b[i + 1]){ count++; } } total = b.length - count; int[] c = Arrays.copyOf(b,b.length); int j = 0; int i = 1; while (i < c.length){ if (c[i] == c[j]){ i++; } else { j++; c[j] = c[i]; i++; } } int index = total - k; if(index < 0) index = -index; int kmax = c[index]; return kmax; }

eror:

testKMax_4(KmaxTests): Test case for Selector Test case testKMax_4: calling kmax() with a.length = 2, typical, boundary, special, and illegal subcases. java.lang.AssertionError: Program under test did not throw an IllegalArugmentException as required. a = [-4, -4] k = 2. testKMax_5(KmaxTests): Test case for Selector Test case testKMax_5: calling kmax() with a.length > 2, typical, boundary, special, and illegal subcases. java.lang.AssertionError: a = [-4, -4, -4, -4, -4, -4, -4] k = 2.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!