Java programming: Create an application containing an array that stores 10 integ
ID: 3936912 • Letter: J
Question
Java programming:
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).
Explanation / Answer
/**
* @author
*
*/
public class ArrayUtils {
/**
* (1) method to print the array elements
*
* @param arr
*/
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(" " + arr[i]);
}
System.out.println();
}
/**
* (2) display all the integers in reverse order
*
* @param arr
*/
public static void printArrayReverse(int[] arr) {
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(" " + arr[i]);
}
System.out.println();
}
/**
* (3) display the sum of the integers,
*
* @param arr
*
*/
public static void getSumOfArray(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println("Sum of array :" + sum);
}
/**
* (4) display all values less than a limiting argument
*
* @param arr
* @param n
*/
public static void displayGreaterthanN(int[] arr, int n) {
for (int i = 0; i < arr.length; i++) {
if (n < arr[i])
System.out.print(" " + arr[i]);
}
System.out.println();
}
/**
* display all values that are higher than the calculated average value
*
* @param arr
*/
public static void displayGreaterthanAverage(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
double average = (double) sum / arr.length;
for (int i = 0; i < arr.length; i++) {
if (average < arr[i])
System.out.print(" " + arr[i]);
}
}
/**
* @param args
*/
public static void main(String[] args) {
int[] arr = { 12, 32, 43, 54, 11, 42, 22, 21, 43, 13 };
System.out.println("(1) display all the integers: ");
System.out.println("(2) display all the integers in reverse order: ");
printArray(arr);
System.out.println("(3) display the sum of the integers: ");
printArrayReverse(arr);
System.out
.println("(4) display all values less than a limiting argument 5 : ");
displayGreaterthanN(arr, 5);
System.out
.println("(5) display all values that are higher than the calculated average value: ");
displayGreaterthanAverage(arr);
}
}
OUTPUT:
(1) display all the integers:
(2) display all the integers in reverse order:
12 32 43 54 11 42 22 21 43 13
(3) display the sum of the integers:
13 43 21 22 42 11 54 43 32 12
(4) display all values less than a limiting argument 5 :
12 32 43 54 11 42 22 21 43 13
(5) display all values that are higher than the calculated average value:
32 43 54 42 43
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.