eCheck02B can be found on page 97 of the textbook .
Write a Java program that starts with 2 positive integers and:
What would happen if the variables first
and second
had been declared as type int
?
What would happen if the variables first
and second
had been declared as type double
?
The textbook provides a hint: You can round down to the nearest
multiple of 5 by dividing the integer by 5 and then multiplying
the result by 5.
4 / 5 * 5
⇒ (4 / 5) * 5
⇒ 0 * 5
⇒ 0
5 / 5 * 5
⇒ (5 / 5) * 5
⇒ 1 * 5
⇒ 5
6 / 5 * 5
⇒ (6 / 5) * 5
⇒ 1 * 5
⇒ 5
How much do you have to add to 6
to get the rounded result
of 10
?
(6 + 4) / 5 * 5
⇒ 10 / 5 * 5
⇒ (10 / 5) * 5
⇒ 2 * 5
⇒ 10
(7 + 4) / 5 * 5
⇒ 11 / 5 * 5
⇒ (11 / 5) * 5
⇒ 2 * 5
⇒ 10
(8 + 4) / 5 * 5
⇒ 12 / 5 * 5
⇒ (12 / 5) * 5
⇒ 2 * 5
⇒ 10
(9 + 4) / 5 * 5
⇒ 13 / 5 * 5
⇒ (13 / 5) * 5
⇒ 2 * 5
⇒ 10
(10 + 4) / 5 * 5
⇒ 14 / 5 * 5
⇒ (14 / 5) * 5
⇒ 2 * 5
⇒ 10
Which of the following statements correctly computes the average?
double average = (1 / 2) * (product + roundedProduct); double average = product + roundedProduct / 2; double average = (product + roundedProduct) / 2; double average = (double) (product + roundedProduct) / 2; double average = (product + roundedProduct) / (double) 2; double average = (product + roundedProduct) / 2.0; double average = 0.5 * (product + roundedProduct);