The following are the review questions from chapter 4 in your book.
Page 184: Do review exercises: R4.2 (a, b, c, d) & R.4.4 (a, b, c, f)
The answers to the questions from page 184 in your book should be typed in the block of comments in the java file such as;
/*
R4.2 a
…
…
R4.4 f
*/
R4.2 Write a loop that computes
R4.4 What do these loops print?
Write a program called Assignment4 (saved in a file Assignment4.java) that reads in a sequence of positive integers until a user enters 0 to quit (we don't know how many numbers the user may enter). The loop continues to process input values until the user enters zero: Here is sample of the program execution in Bold letters.
Enter a positive integer. Enter 0 to quit.
For each positive integer, check if it is a prime number or not. If it is, print out:
The number X is a prime number.
If it is not, print out:
The number X is not a prime number.
Note that X is a positive number read from user input.
Once user enters 0, then stop asking for more positive integers, and compute the maximum , minimum among the integers, the sum, the count, and the average of these integers which were input by the user. DO NOT include 0 in the computation which is the last integer read by the program.
(Note that max, min, sum, and count will be computed through iterations of a loop, see chapter 4 for example the Writing a Loop on page 169)
Your average should be formatted to exactly 2 digits (to the right of the decimal point).
Hint: There are two parts to this program. The first part reads a sequence of positive numbers and keeps track of the maximum, minimum, sum, count, and average.
The second part is, for each positive integer, to determine if it is a prime number or not. A prime number (integer) is an integer that can be divided only by itself and 1. For example, 7 is a prime number and 6 is not (6 can be divided by 2 and 3). We can check if an integer can be divided by another integer using the remainder operator (%). If the remainder is 0, it can be divided. And we need to check this for all integers between 2 and n-1 for a positive integer n.
Hint: Write a separate program to find if a number is prime or not, and then include it to the first part mentioned above.
Example Execution:
The following is an example input and output. The input is shown in red.
Enter a positive integer. Enter 0 to quit.
43
The number 43 is a prime number.
Enter a positive integer. Enter 0 to quit.
6
The number 6 is not a prime number.
Enter a positive integer. Enter 0 to quit.
7
The number 7 is a prime number.
Enter a positive integer. Enter 0 to quit.
9
The number 9 is not a prime number.
Enter a positive integer. Enter 0 to quit.
12
The number 12 is not a prime number.
Enter a positive integer. Enter 0 to quit.
4
The number 4 is not a prime number.
Enter a positive integer. Enter 0 to quit.
0
The maximum positive number is: 43
The minimum positive number is: 4
The sum is: 81
The count of number(s) is: 6
The average is: 13.50