19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.9 Case Study: Computing Body Mass Index 97<br />

What is 9 - 2? 5<br />

Your answer is wrong<br />

9 - 2 is 7<br />

line# number1 number2 temp answer output<br />

6 2<br />

7 9<br />

11 2<br />

12 9<br />

13 2<br />

20 5<br />

26 Your answer is wrong<br />

9 – 2 should be 7<br />

To swap two variables number1 and number2, a temporary variable temp (line 11) is used<br />

<strong>to</strong> first hold the value in number1. The value in number2 is assigned <strong>to</strong> number1 (line 12),<br />

and the value in temp is assigned <strong>to</strong> number2 (line 13).<br />

3.15 Which of the following is a possible output from invoking Math.random()?<br />

323.4, 0.5, 34, 1.0, 0.0, 0.234<br />

3.16 a. How do you generate a random integer i such that 0 … i 6 20?<br />

b. How do you generate a random integer i such that 10 … i 6 20?<br />

c. How do you generate a random integer i such that 10 … i … 50?<br />

3.9 Case Study: Computing Body Mass Index<br />

You can use nested if statements <strong>to</strong> write a program that interprets body mass index.<br />

Body Mass Index (BMI) is a measure of health based on height and weight. It can be calculated<br />

by taking your weight in kilograms and dividing it by the square of your height in<br />

meters. The interpretation of BMI for people 20 years or older is as follows:<br />

✓Point✓ Check<br />

Key<br />

Point<br />

BMI<br />

Interpretation<br />

Below 18.5 Underweight<br />

18.5–24.9 Normal<br />

25.0–29.9 Overweight<br />

Above 30.0 Obese<br />

Write a program that prompts the user <strong>to</strong> enter a weight in pounds and height in inches and<br />

displays the BMI. Note that one pound is 0.45359237 kilograms and one inch is 0.0254<br />

meters. Listing 3.5 gives the program.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!