Question: public final class Selector { private Selector ( ) { } public static int min ( int [ ] a ) { if ( a

public final class Selector {
private Selector(){}
public static int min(int[] a){
if(a == null || a.length ==0){
throw new IllegalArgumentException();
}
//if the first element in the array a[0] is the minimum value
int min = a[0];
// example array 1051220304
//why i max){
max = a[i];
}
}
return max;
}
public static int kmin(int[] a, int k){
if(a == null || a.length ==0|| k > a.length || k <1){
throw new IllegalArgumentException();
}
if(a.length ==1){
return a[0];
}
int[] sorted = new int[a.length];
sorted = createCopy(sorted, a);
sorted = sortIncreasing(sorted);
return searchForK(sorted, k);
}
public static int kmax(int[] a, int k){
if(a == null || a.length ==0|| k > a.length || k <1){
throw new IllegalArgumentException();
}
if(a.length ==1){
return a[0];
}
int[] sorted = new int[a.length];
sorted = createCopy(sorted, a);
sorted = sortDecreasing(sorted);
return searchForK(sorted, k);
}
//Helper method for kmax only
private static int[] sortDecreasing(int[]a){
for(int x =0; x < a.length; x++){
for(int y = x +1; y < a.length; y++){
if(a[x]< a[y]){
int val = a[x];
a[x]= a[y];
a[y]= val;
}
}
}
return a;
}
//Helper method for kmin only
private static int[] sortIncreasing(int[]a){
for(int x =0; x < a.length; x++){
for(int y = x +1; y < a.length; y++){
if(a[x]> a[y]){
int val = a[x];
a[x]= a[y];
a[y]= val;
}
}
}
return a;
}
//Helper methods for kmax and kmin
private static int searchForK(int[] arr, int k){
if(k ==1){
return arr[0];
}
int index =0;
for(int n =0; n < arr.length -1; n++){
if(arr[n]!= arr[n +1]){
index++;
}
if(k -1== index){
return arr[n+1];
}
}
throw new IllegalArgumentException();
}
//Helper method for kmin and kmax
private static int[] createCopy(int[] arr, int[] copy){
for(int n =0; n < copy.length; n++){
arr[n]= copy[n];
}
return arr;
}
public static int[] range(int[] a, int low, int high){
if(a == null || a.length ==0){
throw new IllegalArgumentException();
}
int count =0;
for(int x =0; x < a.length; x++){
if(a[x]>= low && a[x]<= high){
count++;
}
}
int[] newArr = new int[count];
int arrCount =0;
for(int x =0; x < a.length; x++){
if(a[x]>= low && a[x]<= high){
newArr[arrCount]= a[x];
arrCount++;
}
}
return newArr;
}
public static int ceiling(int[] a, int key){
if(a == null || a.length ==0){
throw new IllegalArgumentException();
}
int min =0;
int exceptionCounter =0;
for(int x : a){
if(x >= key){
min = x;
break;
}
exceptionCounter++;
}
if(exceptionCounter == a.length){
throw new IllegalArgumentException();
}
for(int n : a){
if(n >= key && n < min){
min = n;
}
}
return min;
}
public static int floor(int[] a, int key){
if(a == null || a.length ==0){

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 Programming Questions!