In: Computer Science
write the program named Lab06.java that contains the following four static methods:
• public static double max(double[] data) that returns the maximum value in the array data
• public static double min(double[] data) that returns the minimum value in the array data
• public static double sum(double[] data) that sums all items in the array and return the result
• public static double ave(double[] data) that call the sum() method and then return the average.
Once you have completed the methods above, write a simple main program that does the following:
1. Asks the user how many items will be entered
2. Creates an array of double of the correct size
3. Prompts the user and reads in the values
4. Calculates and prints out the max, min, sum, and average using the methods above.
Code
import java.util.Scanner;//for taking user input
public class Lab06 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);//creating the object of the
scanner class
double []data;//declare the array name data type of double
int size;//variable that holds the numebr of element user want to
eter in the array
System.out.print("How many items will be entered: ");
size=input.nextInt();
data=new double[size];//assign the size to the data array
//taking the numebr from the user one by one
for(int i=0;i<size;i++)//for loop till size
{
System.out.print("Enter the "+ (i+1) +"th element in array:
");
data[i]=input.nextDouble();
}
//printing the information
System.out.println(" Maximum element from the data array is:
"+max(data));
System.out.println("Minimum element from the data array is:
"+min(data));
System.out.println("Sum of the all element in data array is:
"+sum(data));
System.out.println("Average of the data array is:
"+ave(data));
}
//method that will return the element
public static double max(double[] data)
{
double maxElement=data[0];//assing the first value to maxElement
variable
for(int i=1;i<data.length;i++)//for loop from 1 to length of the
data array
{
if(maxElement<data[i])//if current element is bigger then
max
maxElement=data[i];//change the value of max with current
element
}
//return maxElement
return maxElement;
}
public static double min(double[] data)
{
double minElement=data[0];
for(int i=1;i<data.length;i++)
{
if(minElement>data[i])
minElement=data[i];
}
return minElement;
}
public static double sum(double[] data)
{
double sum=0;//varibale that hold the sum of all the element in the
array
for(int i=1;i<data.length;i++)
sum+=data[i];
//return the sum
return sum;
}
//function that will find the average of the data array
public static double ave(double[] data)
{
double avg=sum(data)/data.length;
return avg;
}
}
output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.