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

The problem is stated as follows: Write a method public static double[] getArray

ID: 3757746 • Letter: T

Question

The problem is stated as follows:

Write a method public static double[] getArrayStats (double[] a) that finds the minimum, average, and maximum elements in the array a. Your method should return an array containing a's minimum element in index 0, a's average value in index 1, and a's maximum element in index 2.

We just started arrays, and I am hopelessly lost. I was trying to program an input method for the main. Here is what I have so far, but it might be junk:

public static void main (String[]args)
    {
        Scanner input = new Scanner(System.in);
     
       
        System.out.print ("How many numbers are in this array? ");
        int arraySize = input.nextDouble();
        double[] arrayNumbers = new int[arraySize];
        System.out.println();
        System.out.println ("Enter the numbers: ");
        for (double i = 0; i < arraySize; i++)
        {
            arrayNumbers[i] = input.nextDouble();
        } return arrayNumbers;
    }
}

Any help? Suggestions? All would be greatly appreciated.

Thank you,

W.

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Stats
{
   public static double[] getArrayStats (double[] a)
   {
       int len = a.length;
       double max = a[0],min = a[0],sum = 0,avg;
      
       for(int i=0;i<len;i++)
       {
           if(max<a[i])
               max = a[i];
           if(min>a[i])
               min = a[i];
           sum = sum+a[i];
       }
       avg = sum/len;
      
       double[] arrayStats = new double[3];
       arrayStats[0] = min;
       arrayStats[1] = avg;
       arrayStats[2] = max;
      
       return arrayStats;
      
   }
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
Scanner input = new Scanner(System.in);

System.out.print ("How many numbers are in this array? ");
int arraySize = input.nextInt();
double[] arrayNumbers = new double[arraySize];
System.out.println();
System.out.println ("Enter the numbers: ");
for (int i = 0; i < arraySize; i++)
{
arrayNumbers[i] = input.nextDouble();
}
  
double[] arrayStat = new double[3];
arrayStat = getArrayStats(arrayNumbers);
  
System.out.println("Max in array is : "+arrayStat[2]);
System.out.println("Min in array is : "+arrayStat[0]);
System.out.println("Avg in array is : "+arrayStat[1]);
}

}