Java Reference
In-Depth Information
Your goal is to earn $30,000 a year. Write a program that finds the minimum sales
you have to generate in order to make $30,000.
5.40
(
Simulation: heads or tails
) Write a program that simulates flipping a coin one
million times and displays the number of heads and tails.
*5.41
(
Occurrence of max numbers
) Write a program that reads integers, finds the larg-
est of them, and counts its occurrences. Assume that the input ends with number
0
. Suppose that you entered
3 5 2 5 5 5 0
; the program finds that the largest
is
5
and the occurrence count for
5
is
4
.
(
Hint
: Maintain two variables,
max
and
count
.
max
stores the current max num-
ber, and
count
stores its occurrences. Initially, assign the first number to
max
and
1
to
count
. Compare each subsequent number with
max
. If the number is
greater than
max
, assign it to
max
and reset
count
to
1
. If the number is equal to
max
, increment
count
by
1
.)
Enter numbers: 3 5 2 5 5 5 0
The largest number is 5
The occurrence count of the largest number is 4
*5.42
(
Financial application: find the sales amount
) Rewrite Programming Exercise
5.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.
■
*5.43
(
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
*5.44
(
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: 5
The bits are 0000000000000101
Enter an integer: -5
The bits are 1111111111111011
(
Hint
: You need to use the bitwise right shift operator (
>>
) and the bitwise
AND
operator (
&
), which are covered in Appendix G, Bitwise Operations.)
**5.45
(
Statistics: compute mean and standard deviation
) In business applications, you
are often asked to compute the mean and standard deviation of data. The mean is
simply the average of the numbers. The standard deviation is a statistic that tells
Search WWH ::
Custom Search