cover picture cover picture


Homework 9

CMP 167: Programming Methods I
Lehman College, City University of New York
Spring 2016


This Homework Is Due By 11:59 PM on Wednesday May 18, 2016

In your textbook, this homework problem is in section 6.16 Homework 9-1 (Part 1) and section 6.17 Homework 9-1 (Part 2)..

In this assignment you are being asked to write some methods that operate on arrays of int values. You will code all the methods and use your main method to test your methods.

Your class should be named ArrayMethods.

Your program will have the following methods:

  1. public static int sum(int[] arr)

    This method will compute the sum of all the elements in the parameter array arr.

    Parameters:
    int[] arr
    An array of int values.


    Return Value: This method returns an int representing the computed sum of all the elements in arr.

    Side Effects: None.
  2. public static int sum(int[] arr, int firstIndex, int lastIndex)

    This method will compute the sum of the elements in the parameter array arr starting at index firstIndex and ending at index lastIndex.

    Please note that your method must verify the validity of firstIndex and lastIndex.

    Parameters:
    int[] arr
    An array of int values.
    int firstIndex
    The index of the first element to include in the sum.
    int lastIndex
    The index of the last element to include in the sum.


    Return Value: This method returns an int representing the computed sum of the elements in arr starting at firstIndex and ending at lastIndex. If either index is invalid, the method should return the value -666.

    Side Effects: None.
  3. public static double average(int[] arr)

    This method will compute the average of all the elements in the parameter array arr. You should make use of the sum(int[] arr) method in your computation.

    Parameters:
    int[] arr
    An array of int values.

    Return Value: This method returns a double value representing the computed average of all the elements in arr.

    Side Effects: None.
  4. public static double average(int[] arr, int firstIndex, int lastIndex)

    This method will compute the average of the elements in the parameter array arr starting at index firstIndex and ending at index lastIndex. You should make use of the sum(int[] arr, int firstIndex, int lastIndex) method in your computation.

    Please note that your method must verify the validity of firstIndex and lastIndex.

    Parameters:
    int[] arr
    An array of int values.
    int firstIndex
    The index of the first element to include in the average.
    int lastIndex
    The index of the last element to include in the average.


    Return Value: This method returns a double value representing the computed average of the elements in arr starting at index firstIndex and ending at index lastIndex. If either index is invalid, the method should return the value -666.0.

    Side Effects: None.
  5. public static int maxValue(int[] arr)

    This method will search all the elements in the parameter array arr for the element with the maximum value.

    Parameters:
    int[] arr
    An array of int values.

    Return Value: This method returns the element with the maximum value of all the elements in the array arr.

    Side Effects: None.
  6. public static int maxValue(int[] arr, int firstIndex, int lastIndex)

    This method will search the elements in the parameter array arr, starting at index firstIndex and ending at index lastIndex, for the element with the maximum value.

    Please note that your method must verify the validity of firstIndex and lastIndex.

    Parameters:
    int[] arr
    An array of int values.
    int firstIndex
    The index of the first element to include in the search.
    int lastIndex
    The index of the last element to include in the search.

    Return Value: This method returns the element with the maximum value of the elements in the array arr, starting at index firstIndex and ending at index lastIndex. If either index is invalid, the method should return the value -666.

    Side Effects: None.
  7. public static int indexOfFirstMaxValue(int[] arr)

    This method will search all the elements in the parameter array arr for the index of the first occurrence of the maximum value.

    Parameters:
    int[] arr
    An array of int values.

    Return Value: This method returns the index of the first occurrence of the maximum value of all the elements in the array arr.

    Side Effects: None.
  8. public static int indexOfFirstMaxValue(int[] arr, int firstIndex, int lastIndex)

    This method will search the elements in the parameter array arr, starting at index firstIndex and ending at index lastIndex, for the index of the first occurrence of the maximum value.

    Please note that your method must verify the validity of firstIndex and lastIndex.

    Parameters:
    int[] arr
    An array of int values.
    int firstIndex
    The index of the first element to include in the search.
    int lastIndex
    The index of the last element to include in the search.

    Return Value: This method returns the index of the first occurrence of the maximum value of the elements in the array arr, starting at index firstIndex and ending at index lastIndex. If either index is invalid, the method should return the value -1.

    Side Effects: None.
  9. public static int minValue(int[] arr)

    This method will search all the elements in the parameter array arr for the element with the minimum value.

    Parameters:
    int[] arr
    An array of int values.

    Return Value: This method returns the element with the minimum value of all the elements in the array arr.

    Side Effects: None.
  10. public static int minValue(int[] arr, int firstIndex, int lastIndex)

    This method will search the elements in the parameter array arr, starting at index firstIndex and ending at index lastIndex, for the element with the minimum value.

    Please note that your method must verify the validity of firstIndex and lastIndex.

    Parameters:
    int[] arr
    An array of int values.
    int firstIndex
    The index of the first element to include in the search.
    int lastIndex
    The index of the last element to include in the search.

    Return Value: This method returns the element with the minimum value of the elements in the array arr, starting at index firstIndex and ending at index lastIndex. If either index is invalid, the method should return the value -666.

    Side Effects: None.
  11. public static int indexOfFirstMinValue(int[] arr)

    This method will search all the elements in the parameter array arr for the index of the first occurrence of the minimum value.

    Parameters:
    int[] arr
    An array of int values.

    Return Value: This method returns the index of the first occurrence of the minimum value of all the elements in the array arr.

    Side Effects: None.
  12. public static int indexOfFirstMinValue(int[] arr, int firstIndex, int lastIndex)

    This method will search the elements in the parameter array arr, starting at index firstIndex and ending at index lastIndex, for the index of the first occurrence of the minimum value.

    Please note that your method must verify the validity of firstIndex and lastIndex.

    Parameters:
    int[] arr
    An array of int values.
    int firstIndex
    The index of the first element to include in the search.
    int lastIndex
    The index of the last element to include in the search.

    Return Value: This method returns the index of the first occurrence of the minimum value of the elements in the array arr, starting at index firstIndex and ending at index lastIndex. If either index is invalid, the method should return the value -1.

    Side Effects: None.
  13. public static int numberOfBelowAverageElements(int[] arr)

    This method will search all the elements in the parameter array arr and count the number of elements that are below average. You should make use of the average(int[] arr) method in your computation.

    Parameters:
    int[] arr
    An array of int values.

    Return Value: This method returns the number of elements in the array arr that are below average.

    Side Effects: None.
  14. public static int numberOfBelowAverageElements(int[] arr, int firstIndex, int lastIndex)

    This method will search the elements in the parameter array arr, starting at index firstIndex and ending at index lastIndex, and count the number of elements that are below average. You should make use of the average(int[] arr, int firstIndex, int lastIndex) method in your computation.

    Please note that your method must verify the validity of firstIndex and lastIndex.

    Parameters:
    int[] arr
    An array of int values.
    int firstIndex
    The index of the first element to include in the search.
    int lastIndex
    The index of the last element to include in the search.

    Return Value: This method returns the number of elements in the array arr, starting at index firstIndex and ending at index lastIndex, that are below average. If either index is invalid, the method should return the value -666.

    Side Effects: None.
  15. public static int numberOfAboveAverageElements(int[] arr)

    This method will search all the elements in the parameter array arr and count the number of elements that are above average. You should make use of the average(int[] arr) method in your computation.

    Parameters:
    int[] arr
    An array of int values.

    Return Value: This method returns the number of elements in the array arr that are above average.

    Side Effects: None.
  16. public static int numberOfAboveAverageElements(int[] arr, int firstIndex, int lastIndex)

    This method will search the elements in the parameter array arr, starting at index firstIndex and ending at index lastIndex, and count the number of elements that are above average. You should make use of the average(int[] arr, int firstIndex, int lastIndex) method in your computation.

    Please note that your method must verify the validity of firstIndex and lastIndex.

    Parameters:
    int[] arr
    An array of int values.
    int firstIndex
    The index of the first element to include in the search.
    int lastIndex
    The index of the last element to include in the search.

    Return Value: This method returns the number of elements in the array arr, starting at index firstIndex and ending at index lastIndex, that are above average. If either index is invalid, the method should return the value -666.

    Side Effects: None.
  17. public static void rotateElements(int[] arr)

    This method will rotate all the elements in the parameter array arr by moving every element arr[i] to the position arr[i+1] and moving the last element in the array to arr[0].

    Parameters:
    int[] arr
    An array of int values.

    Return Value: This method does not return anything

    Side Effects: The elements in the parameter array arr are rotated one position.
  18. public static void rotateElements(int[] arr, int rotationCount)

    This method will rotate all the elements in the parameter array arr by moving every element arr[i] to the position arr[i+1] and moving the last element in the array to arr[0] rotationCount times. You should make use of the rotateElements(int[] arr) method in your computation.

    Parameters:
    int[] arr
    An array of int values.
    int rotationCount
    An int representing the number of times to rotate the array arr.

    Return Value: This method does not return anything

    Side Effects: The elements in the parameter array arr are rotated as many times as specified by rotationCount.
  19. public static void main(String[] args).

    This is your main main method and where your program begins execution. For this assignment, you will be using the using the main method to test all the methods specified above, to insure that they are producing the correct output.

For example, given the following array:

    myArray = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58, 72}
  

    Sum of whole array = 1253
    Sum of elements 12-18 = 343
    
    Average of whole array = 50.0
    Average of elements 12-18 = 49.0
    
    Max of whole array = 100
    Max of elements 12-18 = 98
    
    Index of first Max of whole array = 8
    Index of first Max of elements 12-18 = 13
    
    Min of whole array = 7
    Min of elements 12-18 = 16
    
    Index of first Min of whole array = 20
    Index of first Min of elements 12-18 = 12
    
    Count of elements below average of whole array = 13
    Count of elements below average of elements 12-18 = 4
    
    Count of elements above average of whole array = 12
    Count of elements above average of elements 12-18 = 3
    
    Rotating once
    myArray = {72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58}
    
    Rotating 5 more times
    myArray = {14, 7, 16, 49, 58, 72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21}