Java Reference
In-Depth Information
What is 9 - 2?
Your answer is wrong
9 - 2 is 7
5
line#
output
number1
number2
temp
answer
6
2
7
9
11
2
12
9
13
2
20
5
26
Your answer is wrong
9 - 2 should be 7
To swap two variables number1 and number2 , a temporary variable temp (line 11) is used
to first hold the value in number1 . The value in number2 is assigned to number1 (line 12),
and the value in temp is assigned to number2 (line 13).
3.15
Which of the following is a possible output from invoking Math.random() ?
Check
Point
323.4 , 0.5 , 34 , 1.0 , 0.0 , 0.234
3.16
a.
How do you generate a random integer i such that
0
i
6
20?
b.
How do you generate a random integer i such that
10
i
6
20?
c.
How do you generate a random integer i such that
10
i
50?
3.9 Case Study: Computing Body Mass Index
You can use nested if statements to write a program that interprets body mass index.
Key
Point
Body Mass Index (BMI) is a measure of health based on height and weight. It can be calcu-
lated by taking your weight in kilograms and dividing it by the square of your height in
meters. The interpretation of BMI for people 20 years or older is as follows:
BMI
Interpretation
Below 18.5
Underweight
18.5-24.9
Normal
25.0-29.9
Overweight
Above 30.0
Obese
Write a program that prompts the user to enter a weight in pounds and height in inches and
displays the BMI. Note that one pound is 0.45359237 kilograms and one inch is 0.0254
meters. Listing 3.5 gives the program.
 
 
Search WWH ::




Custom Search