Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Method 1: createArray Method 2: statsDisplay Write a method that performs some r

ID: 640192 • Letter: M

Question

Method 1: 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.

This method should accept as input an int array. Since we are doing some simple printing, and do not actually need to return any values, use the void return type. Therefore, the header should look as follows:

public static void statsDisplay(int[] intArray)

Explanation / Answer


public class ArrayMethods {
   public static int[] createArray(){
       int []arr = new int[100];
       for(int i = 0; i < 100; ++i){
           arr[i] = (int) (Math.random() * 99);
       }
       return arr;
   }
  
   public static void statsDisplay(int[] intArray){
       int count1 = 0;
       int count2 = 0;
       for(int i = 0; i < intArray.length; ++i){
           if(intArray[i] % 2 == 0){
               count1++;
           }
           else{
               count2++;
           }
       }
       System.out.println("Number of even Numbers = " + count1);
       System.out.println("Number of odd Numbers = " + count2);
   }
  
   public static void main(String args[]){
       int []arr = createArray();
       statsDisplay(arr);
   }
  
}