Question
Write two overloaded methods that return the average of an array with the following headers: public static int average(int [] array) public static double average(
Write two overloaded methods that return the average of an array with the following headers:
public static int average(int [] array)
public static double average( double [] array) //1st method
Write a method that finds the smallest element in this array of double values using the following
header:
public static double min( double [] array) //2nd method
Write a test program that prompts the user to enter 10 double values, invokes the 1st method, then displays the average value. In addition, invokes 2nd method to return the minimum value, and displays the minimum value.
So I have figured out the how to find the average of the double and int values but i cannot figure out how to find the minimum. i have put the method for this on top commented as 2nd method
java.util.Scanner in = new java.util.Scanner(System.in); double[] vals = new double[10]; System.out.print("Please enter to double values: ");
for (int i = 0; i < 10; i++) vals[i] = in.nextDouble(); System.out.printf("The average is: %.2f", average(vals)); }
public static int average(int[] array) { int sum = 0; for (int val : array) sum += val; return sum / array.length; }
public static double average(double[] array) { double sum = 0.0; for (double val : array) sum += val; return sum / array.length; } public static double min(double[] array) { double min = array[0]; // The minimum value for (double i: array) { if (i < min) min = i; } return min; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started