Answer:
package b4;
public class ArrayMethodDemo {
public static void main(String[] args) {
// Create an array to store 8 random integers.
int[] integernumbers = { 3, 4, 6, 9, 5, 6, 7, 2 };
// Call the display method to show the elements in the array.
display(integernumbers);
// Call the method to display the elements in reverse order.
displayReverse(integernumbers);
// Call the method to find the sum of the elements.
sum(integernumbers);
// Call the method to display all elements less than a limiting value, say 5.
lessThan(integernumbers, 5);
// Call the method to display all elements greater than the average of the array
// elements.
higherThan(integernumbers);
}
// Method to display the elements in the array.
// The method receives only a single argument which is the array.
// Loop through the array and at every cycle, print out each element
public static void display(int[] arr) {
System.out.print("All integers in the array : ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
// Method to display the elements in the array in reverse order
// The method receives only a single parameter which is the array
// Loop through the array starting at the last index
// At every cycle, print out the elements in the array
public static void displayReverse(int[] arr) {
System.out.print("All integers in the array in reverse : ");
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
// Method to print out the sum of the elements in the array.
// The method receives only a single parameter which is the array.
// Declare a variable called sum to hold the sum of the elements
// Initialize sum to zero
// Loop through the array and cumulatively add each element to sum
// Print out the sum at the end of the loop
public static void sum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println("The sum of the integers in the array is " + sum);
}
// Method to print all values in the array that are less than a limiting
// argument.
// The method has two parameters - the array and the limiting argument.
// Loop through the array and at every cycle,
// check if the current array element is less than the limiting argument.
// If it is, print it out. Else continue
public static void lessThan(int[] arr, int limitingargument) {
System.out.print("All values less than the limiting argument (" + limitingargument + ") are: ");
for (int i = 0; i < arr.length; i++) {
if (arr[i] < limitingargument) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
}
// Method to print all values in the array that are higher than the average of
// the array.
// The method has one parameter - the array.
// First, calculate the average of the array elements.
// Loop through the array and at every cycle,
// check if the current array element is greater than the average.
// If it is, print it out. Else continue
public static void higherThan(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
double average = sum / arr.length;
System.out.print("All values higher than the calculate average (" + average + ") is ");
for (int i = 0; i < arr.length; i++) {
if (arr[i] > average) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
}
}
Explanation:
The program has been written in Java.
Please go through the comments in the code for explanation.
The source code has also been attached to this response.
Hope this helps!