In: Computer Science
Array Selector
Problem Overview
This assignment focuses on implementing the methods of a class much like java.util.Arrays. The Selector.java file defines a class with static methods designed to provide useful functionality on arrays, with the common theme of selecting values in an array with particular properties. Each method of Selector is clearly specified, is independent of the other methods in the class, and is designed to provide relatively simple functionality. So, this is a great context in which to practice systematic, disciplined development and test-based verification.
The Selector class
You must correctly implement all the method bodies of the provided Selector class. Your implementation must adhere exactly to the API of the Selector class, as described in the provided source code comments and as described below.
public final class Selector { public static int min(int[] a) public static int max(int[] a) public static int kmin(int[] a, int k) public static int kmax(int[] a, int k) public static int[] range(int[] a, int low, int high) public static int ceiling(int[] a, int key) public static int floor(int[] a, int key) }
A brief description of each method is provided below, along with examples of its use. Refer to the Selector.java source code file for the complete specification of each method’s required behavior.
The min method
This method selects the minimum value from a given array. Examples:
a[] | min(a) |
---|---|
[2, 8, 7, 3, 4] | 2 |
[5, 9, 1, 7, 3] | 1 |
[8, 7, 6, 5, 4] | 4 |
[8, 2, 8, 7, 3, 3, 4] | 2 |
The max method
This method selects the maximum value from a given array. Examples:
a[] | max(a) |
---|---|
[2, 8, 7, 3, 4] | 8 |
[5, 9, 1, 7, 3] | 9 |
[8, 7, 6, 5, 4] | 8 |
[8, 2, 8, 7, 3, 3, 4] | 8 |
The kmin method
This method selects the k-th minimum (smallest) value from a given array. A value is the k-th minimum if and only if there are exactly k - 1 distinct values strictly less than it in the array. Note that kmin(a, 1) == min(a) and kmin(a, a.length()) == max(a). Examples:
a[] | k | kmin(a, k) |
---|---|---|
[2, 8, 7, 3, 4] | 1 | 2 |
[5, 9, 1, 7, 3] | 3 | 5 |
[8, 7, 6, 5, 4] | 5 | 8 |
[8, 2, 8, 7, 3, 3, 4] | 3 | 4 |
The kmax method
This method selects the k-th maximum (largest) value from a given array. A value is the k-th maximum if and only if there are exactly k - 1 distinct values strictly greater than it in the array. Note that kmax(a, 1) == max(a) and kmax(a, a.length()) == min(a). Examples:
a[] | k | kmax(a, k) |
---|---|---|
[2, 8, 7, 3, 4] | 1 | 8 |
[5, 9, 1, 7, 3] | 3 | 5 |
[8, 7, 6, 5, 4] | 5 | 4 |
[8, 2, 8, 7, 3, 3, 4] | 3 | 4 |
The range method
This method selects all values from a given array that are greater than or equal to low and less than or equal to high.
a[] | low | high | range(a, low, high) |
---|---|---|---|
[2, 8, 7, 3, 4] | 1 | 5 | [2, 3, 4] |
[5, 9, 1, 7, 3] | 3 | 5 | [5, 3] |
[8, 7, 6, 5, 4] | 4 | 8 | [8, 7, 6, 5, 4] |
[8, 2, 8, 7, 3, 3, 4] | 3 | 7 | [7, 3, 3, 4] |
The floor method
This method selects from a given array the largest value that is less than or equal to key. Examples:
a[] | key | floor(a, key) |
---|---|---|
[2, 8, 7, 3, 4] | 6 | 4 |
[5, 9, 1, 7, 3] | 1 | 1 |
[8, 7, 6, 5, 4] | 9 | 8 |
[8, 2, 8, 7, 3, 3, 4] | 5 | 4 |
The ceiling method
This method selects from a given array the smallest value that is greater than or equal to key. Examples:
a[] | key | ceiling(a, key) |
---|---|---|
[2, 8, 7, 3, 4] | 1 | 2 |
[5, 9, 1, 7, 3] | 7 | 7 |
[8, 7, 6, 5, 4] | 0 | 4 |
[8, 2, 8, 7, 3, 3, 4] | 5 | 7 |
Notes and Other Requirements
The Selector.java source code file is provided in the startercode folder in Vocareum. You can download Selector.java from Vocareum and work on your local machine.
The comments provided in Selector.java describe the required behavior of each method.
The constructor of the Selector class has been written for you and it must not be changed in any way.
You may add any number of private methods that you like, but you may not add any public method or constructor, nor may you change the signature of any public method or constructor.
You must not add any fields, either public or private, to the Selector class.
The java.util.Arrays class has been imported for you. You are free to delete this import statement if you do not use any methods from the Arrays class in your solutions.
You may not add any other import statement, and you may not use another resource from the java.util package. The penalty for violating this constraint will be a deduction of points up to 50% of the total points available on the assignment.
You may not use sorting in any method, except for kmin and kmax. The penalty for violating this constraint will be a deduction of points up to 50% of the total points available on the assignment.
You do not have to use sorting in kmin and kmax, but doing so makes the solution more straightforward. If you choose to use sorting in these two methods, I suggest that you use the sort(int[]) method from the java.util.Arrays class.
import java.util.Arrays;
/**
* Defines a library of selection methods
* on arrays of ints.
*
* @author YOUR NAME ([email protected])
* @author Dean Hendrix ([email protected])
* @version TODAY
*
*/
public final class Selector {
/**
* Can't instantiate this class.
*
* D O N O T C H A N G E T H I S C O N S T R U C T O R
*
*/
private Selector() { }
/**
* Selects the minimum value from the array a. This method
* throws IllegalArgumentException if a is null or has zero
* length. The array a is not changed by this method.
*/
public static int min(int[] a) {
return -99;
}
/**
* Selects the maximum value from the array a. This method
* throws IllegalArgumentException if a is null or has zero
* length. The array a is not changed by this method.
*/
public static int max(int[] a) {
return -99;
}
/**
* Selects the kth minimum value from the array a. This method
* throws IllegalArgumentException if a is null, has zero
length,
* or if there is no kth minimum value. Note that there is no
kth
* minimum 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 kmin(int[] a, int k) {
return -99;
}
/**
* 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) {
return -99;
}
/**
* 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) {
return null;
}
/**
* Returns the smallest value in a that is greater than or equal
to
* the given key. This method throws an IllegalArgumentException
if
* a is null or has zero length, or if there is no qualifying
* value. Note that key does not have to be an actual value in
a.
* The array a is not changed by this method.
*/
public static int ceiling(int[] a, int key) {
return -99;
}
/**
* Returns the largest value in a that is less than or equal
to
* the given key. This method throws an IllegalArgumentException
if
* a is null or has zero length, or if there is no qualifying
* value. Note that key does not have to be an actual value in
a.
* The array a is not changed by this method.
*/
public static int floor(int[] a, int key) {
return -99;
}
}
The Java code to implement the above all functionalities along with the main method ehich is used to test the menthods in all scenarios. If you don't need main method, you can ignore.
import java.util.Arrays;
/**
* Defines a library of selection methods
* on arrays of ints.
*
* @author YOUR NAME ([email protected])
* @author Dean Hendrix ([email protected])
* @version TODAY
*
*/
public final class Selector {
/**
* Can't instantiate this class.
*
* D O N O T C H A N G E T H I S C O N S T R U C T O R
*
*/
private Selector() { }
/**
* Selects the minimum value from the array a. This method
* throws IllegalArgumentException if a is null or has zero
* length. The array a is not changed by this method.
*/
public static int min(int[] a) {
if(a==null || a.length < 1)
throw new IllegalArgumentException();
else{
return kmin(a, 1); //CALCULATING FIRST MIN VALUE
}
}
/**
* Selects the maximum value from the array a. This method
* throws IllegalArgumentException if a is null or has zero
* length. The array a is not changed by this method.
*/
public static int max(int[] a) {
if(a==null || a.length < 1)
throw new IllegalArgumentException();
else{
return kmax(a, 1); //CALCULATING FIRST MIN VALUE
}
}
/**
* Selects the kth minimum value from the array a. This method
* throws IllegalArgumentException if a is null, has zero length,
* or if there is no kth minimum value. Note that there is no kth
* minimum 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 kmin(int[] a, int k) {
int x=0;
// COUNTING NUMBER OF DISTINCT VALUES IN THE GIVEN ARRAY
for(int i=0;i<a.length;i++){
int j=0;
for(j=0;j<i;j++){
if(a[i]==a[j])
break;
}
if(j==i)
x++;
}
int[] b = new int[x]; // ALLOCATION SIZE TO DISTINCT ARRAY BASED ON THE DISTINCT COUNT
x=0;
// COPYING THE DISTINCT VALUES IN TO ANOTHER ARRAY
for(int i=0;i<a.length;i++){
int j=0;
for(j=0;j<i;j++)
if(a[i]==a[j])
break;
if(j==i)
b[x++]=a[i];
}
if(b==null||b.length<1||b.length<k)
throw new IllegalArgumentException();
else{
Arrays.sort(b); //SORTING THE DISTINCT VALUE ARRAY
return b[k-1]; //RETURNING K MIN VALUE
}
}
/**
* 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) {
int x=0;
//COUNTING NUMBER OF DISTINCT VALUES IN THE GIVEN ARRAY
for(int i=0;i<a.length;i++){
int j=0;
for(j=0;j<i;j++){
if(a[i]==a[j])
break;
}
if(j==i)
x++;
}
int[] b = new int[x]; // ALLOCATION SIZE TO DISTINCT ARRAY BASED ON THE DISTINCT COUNT
x=0;
//COPYING THE DISTINCT VALUES IN TO ANOTHER ARRAY
for(int i=0;i<a.length;i++){
int j=0;
for(j=0;j<i;j++)
if(a[i]==a[j])
break;
if(j==i)
b[x++]=a[i];
}
if(b==null||b.length<1||b.length<k)
throw new IllegalArgumentException();
else{
Arrays.sort(b); //SORTING THE DISTINCT VALUE ARRAY
return b[b.length-k]; //RETURNING K MAX VALUE
}
}
/**
* 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) {
int j=0;
if(a==null || a.length < 1)
throw new IllegalArgumentException();
else{
for(int i : a){
if(i>= low && i<=high)
j++; // calculating number of elements between low and high to allocate size of array
}
}
int[] b = new int[j]; //declaring array to store values between low and high
j=0;
for(int i : a)
if(i>= low && i<=high)
b[j++]=i;
return b;
}
/**
* Returns the smallest value in a that is greater than or equal to
* the given key. This method throws an IllegalArgumentException if
* a is null or has zero length, or if there is no qualifying
* value. Note that key does not have to be an actual value in a.
* The array a is not changed by this method.
*/
public static int ceiling(int[] a, int key) {
int i=0,result=0;
if(a==null || a.length < 1)
throw new IllegalArgumentException();
else{
for(i=1;i<=a.length;i++){
result = kmin(a, i);
if(result>=key)
break;
}
return result;
}
}
/**
* Returns the largest value in a that is less than or equal to
* the given key. This method throws an IllegalArgumentException if
* a is null or has zero length, or if there is no qualifying
* value. Note that key does not have to be an actual value in a.
* The array a is not changed by this method.
*/
public static int floor(int[] a, int key) {
int i=0,result=0;
if(a==null || a.length < 1)
throw new IllegalArgumentException();
else{
for(i=1;i<=a.length;i++){
result = kmax(a, i);
if(result<=key)
break;
}
return result;
}
}
//Driver Method
public static void main(String[] args){
int[] test1 = {2,8,7,3,4};
int[] test2 = {5,9,1,7,3};
int[] test3 = {8, 7, 6, 5, 4};
int[] test4 = {8, 2, 8, 7, 3, 3, 4};
System.out.println("Minimum values are "+ min(test1)+" "+min(test2)+" "+min(test3)+" "+min(test4));
System.out.println("Maximum values are "+ max(test1)+" "+max(test2)+" "+max(test3)+" "+max(test4));
System.out.println("Kth Min values are "+ kmin(test1,1)+" "+kmin(test2,3)+" "+kmin(test3,5)+" "+kmin(test4,3));
System.out.println("Kth Max values are "+ kmax(test1,1)+" "+kmax(test2,3)+" "+kmax(test3,5)+" "+kmax(test4,3));
System.out.println("range values are ");
for(int i : range(test1,1,5)){
System.out.print(i+" ");
}
System.out.println();
for(int i : range(test2,3,5)){
System.out.print(i+" ");
}
System.out.println();
for(int i : range(test3,4,8)){
System.out.print(i+" ");
}
System.out.println();
for(int i : range(test4,3,7)){
System.out.print(i+" ");
}
System.out.println();
System.out.println("floor values are "+ floor(test1,6)+" "+floor(test2,1)+" "+floor(test3,9)+" "+floor(test4,5));
System.out.println("Ceiling values are "+ ceiling(test1,2)+" "+ceiling(test2,7)+" "+ceiling(test3,4)+" "+ceiling(test4,7));
}
}
The output screenshot after executing the above program is as below:
If you have any queries regarding this answer, please reach out throught the comment section.