(Find the smallest element) Write a method that finds the smallest element in an
ID: 3811100 • Letter: #
Question
(Find the smallest element) Write a method that finds the smallest element in an array of double values using the following header.
public static double min(double[] array)
Write a test program that prompts the user to enter ten numbers, invokes this method to return the minimum value, and displays the minimum value. Here is a sample run of the program:
Sample Run for Exercise07 09 Enter input data for the program. Sample data provided below. You may modify it.) 1.9 2.5 3.7 2 1.5 6 3 4 5 2 Show the Sample Output Using the Preceeding Input command java Exercise 07 09 Enter ten double numbers 1.9 2.5 3.7 2 1.5 6 3 4 5 2 The minimal value is 1.5 commandExplanation / Answer
MinTest.java
import java.util.Scanner;
public class MinTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double array[] = new double[10];
System.out.print("Enter 10 double values: ");
for(int i=0;i< array.length; i++){
array[i] = scan.nextDouble();
}
System.out.println("The mminimal value is "+min(array));
}
public static double min(double[] array) {
double min = array[0];
for (int i=0; i<array.length; i++){
if(min > array[i]){
min = array[i];
}
}
return min;
}
}
Output:
Enter 10 double values: 1.9 2.5 3.7 2 1.5 6 3 4 5 2
The mminimal value is 1.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.