Write a method that creates an integer array of size 100. Once created, use some type of loop to fill the array with random integers. Make sure these integers are in the range [0-99]. This method should accept no parameters and return an integer array. Therefore, the method header should look as follows:
public static int[] createArray()
Method 2: statsDisplay
Write a method that performs some rudimentary statistics on the recently created array. The statistics to take are as follows:
Count and display the amount of even numbers (Note: You do not have to print the actual numbers).
Count and display the amount of odd numbers (Note: You do not have to print the actual numbers).
Calculate and display the average.
1
Expert's answer
2016-02-25T00:00:54-0500
public static int[] createArray() {
int size = 100; int[] array = new int[size];
for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 100); }
return array; }
public static void statsDisplay(int[] array) {
int amountEvenNumbers = 0; int amountOddNumbers = 0; int summ = 0; double average = 0;
for (int i = 0; i < array.length; i++) { if (array[i] % 2 == 0) { amountEvenNumbers++; } else { amountOddNumbers++; } summ += array[i]; }
average = (1. * summ) / array.length; System.out.println("Amount of even numbers : " + amountEvenNumbers); System.out.println("Amount of odd numbers : " + amountOddNumbers); System.out.println("Average : " + average); }
Comments
Leave a comment