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:
public static int sum(int[] arr)
arr
.
Parameters
:
int[] arr
int
values.
Return Value
: This method returns an int
representing the computed
sum of all the elements in arr
.
Side Effects
: None.
public static int sum(int[] arr, int firstIndex, int lastIndex)
arr
starting at index firstIndex
and ending at index lastIndex
.
firstIndex
and lastIndex
.
Parameters
:
int[] arr
int
values.
int firstIndex
int lastIndex
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.
public static double average(int[] arr)
arr
.
You should make use of the sum(int[] arr)
method in your computation.
Parameters
:
int[] arr
int
values.
Return Value
: This method returns a double
value representing the
computed average of all the elements in arr
.
Side Effects
: None.
public static double average(int[] arr, int firstIndex, int lastIndex)
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.
firstIndex
and lastIndex
.
Parameters
:
int[] arr
int
values.
int firstIndex
int lastIndex
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.
public static int maxValue(int[] arr)
arr
for the
element with the maximum value.
Parameters
:
int[] arr
int
values.
Return Value
: This method returns the element with the maximum value
of all the elements in the array arr
.
Side Effects
: None.
public static int maxValue(int[] arr, int firstIndex, int lastIndex)
arr
,
starting at index firstIndex
and ending at index lastIndex
,
for the element with the maximum value.
firstIndex
and lastIndex
.
Parameters
:
int[] arr
int
values.
int firstIndex
int lastIndex
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.
public static int indexOfFirstMaxValue(int[] arr)
arr
for the
index of the first occurrence of the maximum value.
Parameters
:
int[] arr
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.
public static int indexOfFirstMaxValue(int[] arr, int firstIndex, int lastIndex)
arr
,
starting at index firstIndex
and ending at index lastIndex
,
for the index of the first occurrence of the maximum value.
firstIndex
and lastIndex
.
Parameters
:
int[] arr
int
values.
int firstIndex
int lastIndex
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.
public static int minValue(int[] arr)
arr
for the
element with the minimum value.
Parameters
:
int[] arr
int
values.
Return Value
: This method returns the element with the minimum value
of all the elements in the array arr
.
Side Effects
: None.
public static int minValue(int[] arr, int firstIndex, int lastIndex)
arr
,
starting at index firstIndex
and ending at index lastIndex
,
for the element with the minimum value.
firstIndex
and lastIndex
.
Parameters
:
int[] arr
int
values.
int firstIndex
int lastIndex
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.
public static int indexOfFirstMinValue(int[] arr)
arr
for the
index of the first occurrence of the minimum value.
Parameters
:
int[] arr
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.
public static int indexOfFirstMinValue(int[] arr, int firstIndex, int lastIndex)
arr
,
starting at index firstIndex
and ending at index lastIndex
,
for the index of the first occurrence of the minimum value.
firstIndex
and lastIndex
.
Parameters
:
int[] arr
int
values.
int firstIndex
int lastIndex
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.
public static int numberOfBelowAverageElements(int[] arr)
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
int
values.
Return Value
: This method returns the number of elements in the array
arr
that are below average.
Side Effects
: None.
public static int numberOfBelowAverageElements(int[] arr, int firstIndex, int lastIndex)
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.
firstIndex
and lastIndex
.
Parameters
:
int[] arr
int
values.
int firstIndex
int lastIndex
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.
public static int numberOfAboveAverageElements(int[] arr)
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
int
values.
Return Value
: This method returns the number of elements in the array
arr
that are above average.
Side Effects
: None.
public static int numberOfAboveAverageElements(int[] arr, int firstIndex, int lastIndex)
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.
firstIndex
and lastIndex
.
Parameters
:
int[] arr
int
values.
int firstIndex
int lastIndex
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.
public static void rotateElements(int[] arr)
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
int
values.
Return Value
: This method does not return anything
Side Effects
: The elements in the parameter array arr
are rotated one
position.
public static void rotateElements(int[] arr, int rotationCount)
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
int
values.
int rotationCount
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
.
public static void main(String[] args)
.
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}