Create an application containing an array that stores 10 integers. The applicati
ID: 3705135 • Letter: C
Question
Create an application containing an array that stores 10 integers. The application should call five methods that in turn (1) display all the integers, (2) display all the integers in reverse order, (3) display the sum of the integers, (4) display all values less than a limiting argument, and (5) display all values that are higher than the calculated average value. Then, create another array that store 5 integers. Pass the two arrays to a method that will display the integer value(s), if any, that appear in both arrays (note that the two arrays can have no stored values in common). Save the file as ArrayMethodDemo.java.
I'm more concerned with the last part, "Then, create another array that store 5 integers. Pass the two arrays..."
Explanation / Answer
ArrayMethodDemo.java
import java.util.Scanner;
public class ArrayMethodDemo {
public static void main(String[] args) {
int a[] = new int[10];
Scanner scan = new Scanner(System.in);
System.out.println("Enter 10 integer values :");
for(int i=0; i<10; i++){
a[i] = scan.nextInt();
}
displayArray(a);
displayArrayReverse(a);
sumArray(a);
System.out.println("Enter Limit value:");
int limit = scan.nextInt();
arrayElementsLessThanLimit(a, limit);
arrayElementsHigherThanAvg(a);
int newArr[] ={10,20,30,40,50};
displayArray(newArr);
displayArray(a);
}
public static void displayArray(int a[]){
System.out.println("Array Elements: ");
for (int i = 0; i < a.length; i++)
System.out.print(a[i]+" ");
System.out.println();
}
public static void displayArrayReverse(int a[]){
System.out.println("Array Elements in reverse order : ");
for (int i = a.length-1; i >0 ; i--)
System.out.print(a[i]+" ");
System.out.println();
}
public static int sumArray(int a[]){
int sum = 0;
for(int i=0; i<a.length; i++){
sum = sum + a[i];
}
System.out.println("The Sum of all integers in Array is :"+sum);
return sum;
}
public static void arrayElementsLessThanLimit(int a[], int limit){
System.out.println("All values less than limiting value "+limit+" are :");
for(int i=0; i<a.length; i++){
if(a[i] < limit){
System.out.print(a[i]+" ");
}
}
System.out.println();
}
public static void arrayElementsHigherThanAvg(int a[]){
System.out.println("All values that are higher than the calculated average value :");
int sum = sumArray(a);
double avg = sum/a.length;
System.out.println("The average of all values : "+avg);
for(int i=0; i<a.length; i++){
if(a[i] > avg){
System.out.print(a[i]+" ");
}
}
System.out.println();
}
}
Output:
Enter 10 integer values :
1 2 3 4 5 6 7 8 9 10
Array Elements:
1 2 3 4 5 6 7 8 9 10
Array Elements in reverse order :
10 9 8 7 6 5 4 3 2
The Sum of all integers in Array is :55
Enter Limit value:
5
All values less than limiting value 5 are :
1 2 3 4
All values that are higher than the calculated average value :
The Sum of all integers in Array is :55
The average of all values : 5.0
6 7 8 9 10
Array Elements:
10 20 30 40 50
Array Elements:
1 2 3 4 5 6 7 8 9 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.