Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/** * 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 kmax = c[total - k]; return kmax; }

bug: testKMax_4(KmaxTests): Test case for Selector Test case testKMax_4: calling kmax() with a.length = 2, typical, boundary, special, and illegal subcases. Program under test threw an exception. a = [-4, -4] k = 2. java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 2 testKMax_5(KmaxTests): Test case for Selector Test case testKMax_5: calling kmax() with a.length > 2, typical, boundary, special, and illegal subcases. Program under test threw an exception. Program under test returned an incorrect result for a = [-4, -4, -4, -4, -4, -4, -4] k = 2. java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 7

2

/** * Returns an array containing all the values in a in the * range [low..high]; that is, all the values that are greater * than or equal to low and less than or equal to high, * including duplicate values. The length of the returned array * is the same as the number of values in the range [low..high]. * If there are no qualifying values, this method returns a * zero-length array. Note that low and high do not have * to be actual values in a. This method throws an * IllegalArgumentException if a is null or has zero length. * The array a is not changed by this method. */ public static int[] range(int[] a, int low, int high) { if ((a.length == 0) || (a == null)){ throw new IllegalArgumentException(); } else { int count = 0; int point = 0; for(int i = 0; i< a.length; i++){ if(a[i] <= high && a [i] >= low){ count++; } } int[] range = new int[count]; for(int c = 0; c < a.length; c++){ if(a[c] <= high && a[c] >= low){ range[point] = a[c]; point++; } } return range; } }

testRange_1(RangeTests): Test case for Selector Test case testRange_1: calling range() with a == null. Program under test threw the wrong exception. java.lang.NullPointerException

please help me to fix the bug

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions