Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*q1: Write a public static method named q1 that takes an ArrayList of Doubles as

ID: 3706328 • Letter: #

Question

*q1: Write a public static method named q1 that takes an ArrayList of Doubles as a * parameter and returns an int. This method returns the index of the first value in the input that is in the range (-2.95, 15.190000000000001) and returns -1 if the input * contains no values in this range *q2: Write a public static method named q2 that takes an ArrayList of Doubles as a *parameter and returns an int. This method returns the index of the first value in the input that is in the range (0.73, 10.98) and returns -1 if the input contains no values in this range * q3: Write a public static method named q3 that takes an ArrayList of Doubles as a *parameters and returns a double. This method returns the value from the input ArrayList that is closest to 37.57 *q4: Write a public static method named q4 that takes an ArrayList of type Integer and returns an int. This method returns the minimum result (do not return the original value) * of taking the absolute value of each value from the input while considering only negative numbers from the input:s *q5: Write a public static method named q5 that takes an ArrayList of type Integer and * returns a double. This method returns the maximum result (do not return the original *value) of taking the tangent of each value from the input while considering only negative numbers from the inputs

Explanation / Answer

public static int q1( ArrayList<Double> arr )

{

    int i;

   

    // traverse the ArrayList

    for( i = 0 ; i < arr.size() ; i++ )

    {

        // if the current element is in range ( -2.95 - 15.190000000000001 )

        if( arr.get(i) >= -2.95 && arr.get(i) <= 15.190000000000001 )

            return i;

    }

   

    // if no element is in the required range

    return -1;

}

public static int q2( ArrayList<Double> arr )

{

    int i;

   

    // traverse the ArrayList

    for( i = 0 ; i < arr.size() ; i++ )

    {

        // if the current element is in range ( 0.73 - 10.98 )

        if( arr.get(i) >= 0.73 && arr.get(i) <= 10.98 )

            return i;

    }

   

    // if no element is in the required range

    return -1;

}

public static double q3( ArrayList<Double> arr )

{

    int i;

   

    // let the closest element be the first element of arr

    double ans = arr.get(0);

   

    // calculate the difference between 37.57 and the current element

    double diff = Math.abs( 37.57 - arr.get(0) );

   

    // traverse the ArrayList

    for( i = 1 ; i < arr.size() ; i++ )

    {

        // calculate the difference between 37.57 and current element

        double curr_diff = Math.abs( 37.57 - arr.get(i) );

       

        // if the curr_diff is less than diff

        if( curr_diff < diff )

        {

            ans = arr.get(i);

           

            diff = curr_diff;

        }

    }

   

    // if no element is in the required range

    return ans;

}

public static int q4( ArrayList<Integer> arr )

{

    int i;

   

    // initialize ans to maximum integer value

    int ans = Integer.MAX_VALUE;

   

    // traverse the ArrayList

    for( i = 0 ; i < arr.size() ; i++ )

    {

        // if current element is negative

        if( arr.get(i) < 0 )

        {

            // if the absolute value of current element is smaller than ans

            if( Math.abs(ar.get(i)) < ans )

                ans = Math.abs(ar.get(i));

        }

    }

   

    return ans;

}

public static double q5( ArrayList<Integer> arr )

{

    int i;

   

    // initialize ans to minimum integer value

    double max = Integer.MIN_VALUE;

   

    // traverse the ArrayList

    for( i = 0 ; i < arr.size() ; i++ )

    {

        // if current element is negative

      if( arr.get(i) < 0 )

        {

            // if the tangent value of current element is smaller than min

            if( Math.tan( arr.get(i) ) > max )

                max = Math.tan( arr.get(i) );

        }

    }

   

    return max;

}