cover picture cover picture


Homework 7

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


This Homework Is Due By 11:59 PM on Saturday December 10, 2016

Textbook Section 6.18 Homework 7-1

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

Your class should be named Array

Your class will have the following methods (click on the method signatures for the Javadoc description of the methods):

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

  2. public static int sum(int[] arr, int firstIndex, int lastIndex)

  3. public static double average(int[] arr)

  4. public static double average(int[] arr, int firstIndex, int lastIndex)

  5. public static int maxValue(int[] arr)

  6. public static int maxValue(int[] arr, int firstIndex, int lastIndex)

  7. public static int indexOfFirstMaxValue(int[] arr)

  8. public static int indexOfFirstMaxValue(int[] arr, int firstIndex, int lastIndex)

  9. public static int minValue(int[] arr)

  10. public static int minValue(int[] arr, int firstIndex, int lastIndex)

  11. public static int indexOfFirstMinValue(int[] arr)

  12. public static int indexOfFirstMinValue(int[] arr, int firstIndex, int lastIndex)

  13. public static int numberOfBelowAverageElements(int[] arr)

  14. public static int numberOfBelowAverageElements(int[] arr, int firstIndex, int lastIndex)

  15. public static int numberOfAboveAverageElements(int[] arr)

  16. public static int numberOfAboveAverageElements(int[] arr, int firstIndex, int lastIndex)

  17. public static void rotateElements(int[] arr)

  18. public static void rotateElements(int[] arr, int rotationCount)

  19. public static void reverseArray(int[] arr)

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}

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