Java Reference
In-Depth Information
6.4
( Analyze scores ) Write a program that reads an unspecified number of scores and
determines how many scores are above or equal to the average and how many
scores are below the average. Enter a negative number to signify the end of the
input. Assume that the maximum number of scores is 100.
**6.5
( Print distinct numbers ) Write a program that reads in ten numbers and displays
distinct numbers (i.e., if a number appears multiple times, it is displayed only
once). ( Hint : Read a number and store it to an array if it is new. If the number is
already in the array, ignore it.) After the input, the array contains the distinct num-
bers. Here is the sample run of the program:
Enter ten numbers:
The distinct numbers are: 1 2 3 6 4 5
1 2 3 2 1 6 3 4 5 2
*6.6
( Revise Listing 4.14, PrimeNumber.java ) Listing 4.14 determines whether a num-
ber n is prime by checking whether 2 , 3 , 4 , 5 , 6 , ..., n/2 is a divisor. If a divisor is
found, n is not prime. A more efficient a pproach is to check whether any of the
prime numbers less than or equal to can divide n evenly. If not, n is prime.
Rewrite Listing 4.14 to display the first 50 prime numbers using this approach.
You need to use an array to store the prime numbers and later use them to check
whether they are possible divisors for n .
2
n
*6.7
( Count single digits ) Write a program that generates 100 random integers between 0
and 9 and displays the count for each number. ( Hint : Use (int)(Math.random()
* 10) to generate a random integer between 0 and 9. Use an array of ten integers,
say counts , to store the counts for the number of 0s, 1s, ..., 9s.)
Sections 6.6-6.8
6.8
( Average an array ) 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)
Write a test program that prompts the user to enter ten double values, invokes this
method, and displays the average value.
6.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:
The minimum number is: 1.5
1.9 2.5 3.7 2 1.5 6 3 4 5 2
6.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)
 
Search WWH ::




Custom Search