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

1-1 arrayti)1-arraylL-1): ; iarray.length; ++) 4. (40 points) Write a class foll

ID: 3602523 • Letter: 1

Question

1-1 arrayti)1-arraylL-1): ; iarray.length; ++) 4. (40 points) Write a class following methods: hamed Statistics The class will contain a main and should have the . Create a [3][13] double array of random integers between 40 and 55 . A method to print the array in the form of: X X x X x Xx X . count how many times each number appeared and store the counts in array count] A method to return the sum the elements method to print the counts in the form of:. A 40 appeared ## times 41 appeared ## times of 1 Page 6 of 7

Explanation / Answer

Since the programming language is not mentioned, and the questions says main inside Statistics class, giving the code in java.

Given below is the code for the question. The questions says create [3][3] double array of random int.Since we are storing only int , instead of double , I have used the int [][] array. In case you need double array, the changes to the program will be

replace the line int[][] A = new int[3][3]; with double[][] A = new double[3][3] in main. And all other occurence of int[][] to double[][] where you pass the array to functions.

But in my opinion using double data type is not needed since we are only dealing with ints.

Please do rate the answer if it helped. Thank you.

import java.util.Random;

public class Statistics {

   private static void print(int[][] a)

   {

       int[] count = new int[16];//16 counters for numbers from 40, 41 ...55

      

       for(int i = 0; i < 3; i++)

       {

           System.out.println();

           for(int j = 0; j < 3; j++)

           {

               System.out.print(a[i][j] + " ");

               int index = a[i][j] - 40; //calculate the index for the number

               count[index]++; //increment the counter for the number

           }

          

       }

       System.out.println(" ");

      

       printCounts(count);

   }

  

   private static void printCounts(int[] counts)

   {

       for(int i = 0, n = 40; i < 16; i++, n++)

           System.out.println(n + " appeared " + counts[i] +" times");

   }

  

   private static int sum(int[][] a)

   {

       int sum = 0;

       for(int i = 0; i < 3; i++)

       {

           for(int j = 0; j < 3; j++)

               sum += a[i][j];

       }

       return sum;

   }

  

   public static void main(String[] args) {

      

       //create a [3][3] array of random int in rang 40-55

       int first = 40, last = 55;

       int length = last - first + 1;

      

       Random random = new Random();

       int[][] A = new int[3][3];

       for(int i = 0; i< 3; i++)

       {

           for(int j = 0; j < 3; j++)

               A[i][j] = first + random.nextInt(length);

       }

      

       print(A);

       System.out.println("Sum of elements is " + sum(A));

      

   }

}

output


42 43 52  
46 49 49  
52 42 55  

40 appeared 0 times
41 appeared 0 times
42 appeared 2 times
43 appeared 1 times
44 appeared 0 times
45 appeared 0 times
46 appeared 1 times
47 appeared 0 times
48 appeared 0 times
49 appeared 2 times
50 appeared 0 times
51 appeared 0 times
52 appeared 2 times
53 appeared 0 times
54 appeared 0 times
55 appeared 1 times
Sum of elements is 430