Java Reference
In-Depth Information
**7.3
( Count occurrence of numbers ) Write a program that reads the integers between 1
and 100 and counts the occurrences of each. Assume the input ends with 0 . Here
is a sample run of the program:
Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
Note that if a number occurs more than one time, the plural word “times” is used
in the output.
7.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.
**7.5
( Print distinct numbers ) Write a program that reads in ten numbers and displays
the number of distinct numbers and the distinct numbers separated by exactly one
space (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 numbers. Here is
the sample run of the program:
Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The number of distinct number is 6
The distinct numbers are: 1 2 3 6 4 5
*7.6
( Revise Listing 5.15, PrimeNumber.java ) Listing 5.15 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 efficien t approach is to check whether any of the
prime numbers less than or equal to
n can divide n evenly. If not, n is prime.
Rewrite Listing 5.15 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
*7.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 an array of ten integers,
say counts , to store the counts for the number of 0s, 1s, ..., 9s.)
Sections 7.6-7.8
7.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.
 
Search WWH ::




Custom Search