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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started