Respuesta :
Answer:
- public class Main {
- public static void main(String[] args) {
- int myArray[] = {3, 7, 2, 5, 9,11, 24, 6, 10, 12};
- int myArray2 [] = {1, 2, 3, 4 ,5};
- displayValue(myArray);
- reverseDisplay(myArray);
- displaySum(myArray);
- displayLess(myArray, 10);
- displayHighAvg(myArray);
- displayBoth(myArray, myArray2);
- }
- public static void displayValue(int arr[]){
- for(int i = 0; i < arr.length; i++){
- System.out.print(arr[i] + " ");
- }
- System.out.println();
- }
- public static void reverseDisplay(int arr[]){
- for(int i = arr.length-1; i >= 0; i--){
- System.out.print(arr[i] + " ");
- }
- System.out.println();
- }
- public static void displaySum(int arr[]){
- int sum = 0;
- for(int i = 0; i < arr.length; i++){
- sum += arr[i];
- }
- System.out.println(sum);
- }
- public static void displayLess(int arr[], int limit){
- for(int i = 0; i < arr.length; i++){
- if(arr[i] < limit){
- System.out.print(arr[i] + " ");
- }
- }
- System.out.println();
- }
- public static void displayHighAvg(int arr[]){
- int sum = 0;
- for(int i = 0; i < arr.length; i++){
- sum += arr[i];
- }
- double avg = sum / arr.length;
- for(int i = 0; i < arr.length; i++){
- if(arr[i] > avg){
- System.out.print(arr[i] + " ");
- }
- }
- System.out.println();
- }
- public static void displayBoth(int arr1[], int arr2 []){
- for(int i = 0; i < arr2.length; i++){
- for(int j = 0; j < arr1.length; j++){
- if(arr1[j] == arr2[i]){
- System.out.print(arr1[j] + " ");
- }
- }
- }
- System.out.println();
- }
- }
Explanation:
There are five methods written to solve all the problems stated in the question.
Method 1 : displayValue (Line 15 - 21)
This is the method that take one input array and use the print() method to display the all the elements in the array.
Method 2: reverseDisplay (Line 23 - 26)
This method will take one input array and print the value in the reverse order. We just need to start with the last index when running the for-loop to print the value.
Method 3: displaySum (Line 31 - 37)
This method will take one input array and use a for-loop to calculate the total of the values in the array.
Method 4: displayLess (Line 39 - 47)
This method will take one two inputs, a array and a limit. We use the limit as the condition to check if any value less than the limit, then the value will only be printed.
Method 5: displayHighAvg (Line 50 - 64)
This method will take one input array and calculate the average. The average will be used to check if any element in the array higher than it, then the value will only be printed.
Method 6: displayBoth (Line 66 - 75)
This method will take two input arrays and compare both of them to find out if any value appears in both input arrays and print it out.
The ArrayMethodDemo application is an illustration of arrays and loops.
- Arrays are variables that hold multiple values
- Loops are used to perform repeated operations
The ArrayMethodDemo application
The ArrayMethodDemo application written in Java, where comments are used to explain each action is as follows:
import java.util.*;
public class Main {
//The following method prints the common elements between the two arrays
public static void checkCommon(int [] myArr1, int [] myArr2) {
for(int i = 0; i<5; i++){
for(int j = 0; j < myArr1.length; j++) {
if(myArr2[i]==myArr1[j]) { System.out.print(myArr2[i]+" "); }
}
}
}
//The main method begins here
public static void main(String[] args) {
//This creates a Scanner object
Scanner input = new Scanner(System.in);
//This initializes the sum of the array elements
int sum = 0;
//This gets input for the number of elements
int n = input.nextInt();
//This declares the array
int [] myArr = new int [n];
//The following loop gets input for the array elements and calculates the sum
for(int i = 0; i<n; i++){
myArr[i] = input.nextInt();
sum+=myArr[i];
}
//This loop displays all integers
for(int i = 0; i<n; i++){
System.out.print(myArr[i]+" ");
}
System.out.println();
//This loop displays all integers, in reverse
for(int i = n - 1; i >= 0; i--){
System.out.print(myArr[i]+" ");
}
System.out.println();
//This prints the sum
System.out.print(sum);
System.out.println();
//This gets input for the argument
int arg = input.nextInt();
//This loop displays all integers less than the argument
for(int i = 0; i<n; i++){
if(myArr[i] < arg){
System.out.print(myArr[i]+" ");
}
}
System.out.println();
//This loop displays all integers greater than the average
for(int i = 0; i<n; i++){
if(myArr[i] > sum/n){
System.out.print(myArr[i]+" ");
}
}
System.out.println();
//This declares a new array
int [] newArr = new int [5];
//The following loop gets input for the new array
for(int i = 0; i<5; i++){
newArr[i] = input.nextInt();
}
//This calls the function that checks for common elements
checkCommon(myArr,newArr);
}
}
Read more about loops and arrays at:
https://brainly.com/question/25610701