Java Reference
In-Depth Information
*4.42
(
Financial application: find the sales amount
) Rewrite Exercise 4.39 as follows:
Use a
for
loop instead of a
do-while
loop.
■
Let the user enter
COMMISSION_SOUGHT
instead of fixing it as a constant.
■
*4.43
(
Simulation: clock countdown
) Write a program that prompts the user to enter the
number of seconds, displays a message at every second, and terminates when the
time expires. Here is a sample run:
Enter the number of seconds:
2 seconds remaining
1 second remaining
Stopped
3
**4.44
(
Monte Carlo simulation
) A square is divided into four smaller regions as shown
below in (a). If you throw a dart into the square 1,000,000 times, what is the prob-
ability for a dart to fall into an odd-numbered region? Write a program to simulate
the process and display the result.
(
Hint
: Place the center of the square in the center of a coordinate system, as shown
in (b). Randomly generate a point in the square and count the number of times for
a point to fall into an odd-numbered region.)
2
2
3
3
1
1
4
4
(a)
(b)
*4.45
(
Math: combinations
) Write a program that displays all possible combinations for
picking two numbers from integers
1
to
7
. Also display the total number of all
combinations.
1 2
1 3
...
...
The total number of all combinations is 21
*4.46
(
Computer architecture: bit-level operations
) A
short
value is stored in
16
bits.
Write a program that prompts the user to enter a short integer and displays the
16
bits for the integer. Here are sample runs:
Enter an integer:
The bits are 0000000000000101
5