92.3k views
1 vote
Create an application containing an array that stores eight integers. The application should call five methods that in turn:

Display all the integers
Display all the integers in reverse order
Display the sum of the integers
Display all values less than a limiting argument
Display all values that are higher than the calculated average value.
public class ArrayMethodDemo {
public static void main (String args[]) {
int[] numbers = {12, 15, 34, 67, 4, 9, 10, 7};
int limit = 12;
display(numbers);
displayReverse(numbers);
displaySum(numbers);
displayLessThan(numbers, limit);
displayHigherThanAverage(numbers);
}
public static void display(int[] numbers) {
// Write your code here
}
public static void displayReverse(int[] numbers) {
// Write your code here
}
public static void displaySum(int[] numbers) {
// Write your code here
}
public static void displayLessThan(int[] numbers, int limit) {
// Write your code here
}
public static void displayHigherThanAverage(int[] numbers) {
// Write your code here
}
}

1 Answer

2 votes

Answer:

public class ArrayMethodDemo {

public static void main (String args[]) {

int[] numbers = {12, 15, 34, 67, 4, 9, 10, 7};

int limit = 12;

display(numbers);

displayReverse(numbers);

displaySum(numbers);

displayLessThan(numbers, limit);

displayHigherThanAverage(numbers);

}

public static void display(int[] numbers) {

//Message to be displayed before printing out the numbers

System.out.print("Numbers in the array: ");

//loop through the numbers in the array and print each number

for(int x: numbers){

System.out.print(x + " ");

}

//print a new line

System.out.println();

}

public static void displayReverse(int[] numbers) {

//Message to be displayed before printing out the reverse of the array

System.out.print("Reverse of the array numbers: ");

//loop through the array starting from the last index

for(int i = numbers.length - 1; i > 0; i--){

System.out.print(numbers[i] + " ");

}

//print a new line

System.out.println();

}

public static void displaySum(int[] numbers) {

//initialize the sum variable

int sum = 0;

//loop through the array and add each element to sum

for(int i = 0; i < numbers.length; i++){

sum += numbers[i];

}

//print out the sum

System.out.println("Sum: " + sum);

}

public static void displayLessThan(int[] numbers, int limit) {

//Text to be displayed before printing values less than the limit

System.out.print("Values less than limit (" + limit + "): ");

//loop through the array and print numbers lower than the specified limit

for(int i = 0; i < numbers.length; i++){

if(numbers[i] < limit){

System.out.print(numbers[i] + " ");

}

}

//print a new line

System.out.println();

}

public static void displayHigherThanAverage(int[] numbers) {

//initialize the average variable

double average = 0.0;

//initialize the sum variable

int sum = 0;

//loop through the array and add each element to sum

for(int i = 0; i < numbers.length; i++){

sum += numbers[i];

}

//calculate the average

average = sum / numbers.length;

//Message to be displayed before printing the values higher than the average

System.out.print("Values higher than average (" + average + "): ");

//loop through the array and print numbers higher than the calculated average

for(int i = 0; i < numbers.length; i++){

if(numbers[i] > average){

System.out.print(numbers[i] + " ");

}

}

//print a new line

System.out.println();

}

}

Sample Output:

Numbers in the array: 12 15 34 67 4 9 10 7

Reverse of the array numbers: 7 10 9 4 67 34 15

Sum: 158

Values less than limit (12): 4 9 10 7

Values higher than average(19.0):34 67

Step-by-step explanation:

The code above contains comments explaining important parts of the code. Kindly go through the comments. A sample output, arising from running the application, has also been added to ace understandability.

User Elpmid
by
4.6k points