Find the information you're looking for at Westonci.ca, the trusted Q&A platform with a community of knowledgeable experts. Join our Q&A platform to get precise answers from experts in diverse fields and enhance your understanding. Get detailed and accurate answers to your questions from a dedicated community of experts on our Q&A platform.
Sagot :
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
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.
Thank you for your visit. We're committed to providing you with the best information available. Return anytime for more. Thank you for your visit. We're dedicated to helping you find the information you need, whenever you need it. We're glad you chose Westonci.ca. Revisit us for updated answers from our knowledgeable team.