Java Reference
In-Depth Information
7.9
( 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:
Enter ten numbers: 1.9 2.5 3.7 2 1.5 6 3 4 5 2
The minimum number is: 1.5
7.10
( Find the index of the smallest element ) Write a method that returns the index of
the smallest element in an array of integers. If the number of such elements is
greater than 1, return the smallest index. Use the following header:
public static int indexOfSmallestElement( double [] array)
Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the smallest element, and displays the index.
*7.11
( Statistics: compute deviation ) Programming Exercise 5.45 computes the stand-
ard deviation of numbers. This exercise uses a different but equivalent formula to
compute the standard deviation of n numbers.
n
n
mean) 2
-
x i
( x i
a
a
+
+ g +
x 1
x 2
x n
i
=
1
i
=
1
mean
=
=
deviation
= H
n
n
n
-
1
To compute the standard deviation with this formula, you have to store the indi-
vidual numbers using an array, so that they can be used after the mean is obtained.
Your program should contain the following methods:
/** Compute the deviation of double values */
public static double deviation( double [] x)
/** Compute the mean of an array of double values */
public static double mean( double [] x)
Write a test program that prompts the user to enter ten numbers and displays the
mean and standard deviation, as shown in the following sample run:
Enter ten numbers: 1.9 2.5 3.7 2 1 6 3 4 5 2
The mean is 3.11
The standard deviation is 1.55738
*7.12
( Reverse an array ) The reverse method in Section  7.7 reverses an array by
copying it to a new array. Rewrite the method that reverses the array passed in
the argument and returns this array. Write a test program that prompts the user to
 
Search WWH ::




Custom Search