Textbook Section 5.14 Homework 5-1
Write a program to read a list of exam grades given as int
's in the range of
0
to 100
into an array. You can assume that the maximum number of
grades is 100. Use a negative number as a sentinel value to indicate the end of the input.
(The negative value is used only to end the loop, do not use it in your calculations.)
Your program will compute the following:
greater than or equal
to the
average
.
less than
the average
.
Your prompt to the user to enter a grade should be:
Enter a grade:
Your program must produce the following output:
Number of grades =Number of Grades Entered
Number of grades >= average =Computed Number of Grades >= Average
Number of grades < average =Computed Number of Grades < Average
For example, if the input is:
98 76 45 56 82 75 99 87 35 44 67 32 11 5 0 17 94 56 73 74 75 -1
The output will be:
Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Enter a grade: Number of grades = 21 Number of grades >= average = 11 Number of grades < average = 10
Please note that your class should be named GradesArray.
Textbook Section 5.15 Homework 5-2
Write a Java program that reads in ten integers and displays distinct numbers (i.e. if a number appears multiple times, it is displayed only once). Hint: Read a number and store it in an array if it is new. If the number is already in the array, discard it. After the input, the array contains the distinct numbers.
Your prompt to the user should be:
Enter a number:
Your program must produce the following output:
Distinct Numbers = List of distinct numbers seperated by comas
For example, if the input is:
15 20 15 20 15 20 15 20 15 20
The output will be:
Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Distinct Numbers = 15,20,
Please note that your class should be named DistinctNumbers.
Textbook Section 5.16 Homework 5-3
One method for finding a prime number n
is by checking whether it can be divided by
2, 3, 4, 5, 6, ..., n/2
. If a divisor is found, n
is not prime.
A more efficient approach to determine whether n
is prime is to check whether any of the
known prime numbers less than or equal to
√
n
can divide n
evenly. If not, n
is prime.
Write a Java program that computes and displays the first fifty prime numbers using the second approach.
You will need to use a array to store the prime numbers and later use them to check whether they are
possible divisors for n
.
Please note that this program has no user prompts.
Your program must produce the list of the first fifty prime numbers on a single line seperated by spaces:
2 3 5 ... 229
Please note that your class should be named PrimeNumbers.