Create an application containing an array that stores 10 integers . The applicat
ID: 3814190 • 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
(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
Explanation / Answer
public class ArrayMethodDemo {
private static void displayAllIntegers(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
private static void displayAllIntegersInReverese(int arr[]) {
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
private static int sumOfAllIntegers(int arr[]) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
count += arr[i];
}
System.out.println(count);
return count;
}
private static void displayAllIntegersLessThanLimit(int arr[], int limit) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] < limit) {
System.out.print(arr[i] + " ");
}
}
System.out.println("");
}
private static void displayAllElementsBelowAverage(int arr[]) {
int avg = sumOfAllIntegers(arr) / arr.length;
displayAllIntegersLessThanLimit(arr, avg);
}
private static void intersection(int arr10[], int arr5[]) {
for (int i = 0; i < arr5.length; i++) {
for (int j = 0; j < arr10.length; j++) {
if (arr5[i] == arr10[j]) {
System.out.print(arr5[i] + " ");
}
}
}
System.out.println("");
}
public static void main(String[] args) {
int arr10[] = new int[10];
int arr5[] = new int[5];
for (int i = 0; i < 10; i++) {
arr10[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
arr5[i] = i + 7;
}
displayAllIntegers(arr10);
displayAllIntegersInReverese(arr10);
sumOfAllIntegers(arr10);
displayAllIntegersLessThanLimit(arr10, 6);
displayAllElementsBelowAverage(arr10);
intersection(arr10, arr5);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.