Question

In: Computer Science

Array Selector Problem Overview This assignment focuses on implementing the methods of a class much like...

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;
}

}

Solutions

Expert Solution

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.


Related Solutions

Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Assignment Description This assignment focuses on programming basics; expressions, variables, constants, methods, selection and loops. Danny...
Assignment Description This assignment focuses on programming basics; expressions, variables, constants, methods, selection and loops. Danny runs an ice cream shop in the inner suburbs of Melbourne. Due to the growing number of customers, Danny has decided to take on extra casual employees. In order to manage payroll for his employees, Danny has decided to develop an employee payroll management system. Details of each employee to be maintained in the system will include; employee id, name, gender (M or F),...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
Act 1: Class BooleanFunc Overview BooleanFunc will contain a truthTable (a dynamic array) which defines the...
Act 1: Class BooleanFunc Overview BooleanFunc will contain a truthTable (a dynamic array) which defines the boolean function being represented. You can load this truthTable using a mutator, setTruthTable() , and change the function it represents by calling setTruthTable() to give the table new values. You will not be able to change the size of the table (i..e. #inputs to the function) after an object is instantiated using a setTableSize-style method, but you can totally redefine the object using the...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Please submit a Netbeans project with a class called RecursionExercises.java implementing the following methods: public static...
Please submit a Netbeans project with a class called RecursionExercises.java implementing the following methods: public static int reverse(int number) This method returns the positive integer obtained when the digits of the parameter is reversed. For example, when called with the parameter 12345, this method would return 54321. (Do this with proper integer arithmetic instead of just simply converting to String, reversing that and then using parseInt to extract the result). Hint: The number of digits of a number can be...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
How to print these methods in a separate main class? need the output to look like...
How to print these methods in a separate main class? need the output to look like this: Enter three integers whose GCD is to be found -> Enter an integer n to find the nth Fibonacci number -> Enter the base and exponent , an integer, of a power -> Enter two positive integers 1 and j where i < j -> gcd() = fib() = (a number ^ a number) = a number There are ___ palindromic numbers between...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT