Java Reference
In-Depth Information
Enter next person's information:
height (in inches)? 73.5
weight (in pounds)? 230
Enter next person's information:
height (in inches)? 71
weight (in pounds)? 220.5
Person #1 body mass index = 29.93
overweight
Person #2 body mass index = 30.75
obese
In Chapter 1 we introduced the idea of iterative enhancement, in which you
develop a complex program in stages. Every professional programmer uses this tech-
nique, so it is important to learn to apply it yourself in the programs you write.
In this case, we eventually want our program to explain to the user what it does
and compute BMI results for two different people. We also want the program to be
well structured. But we don't have to do everything at once. In fact, if we try to do so,
we are likely to be overwhelmed by the details. In writing this program, we will go
through three different stages:
1. First, we'll write a program that computes results for just one person, without an
introduction. We won't worry about program structure yet.
2. Next, we'll write a complete program that computes results for two people, includ-
ing an introduction. Again, we won't worry about program structure at this point.
3. Finally, we will put together a well-structured and complete program.
One-Person Unstructured Solution
Even the first version of the program will prompt for user input, so we will need to
construct a Scanner object to read from the console:
Scanner console = new Scanner(System.in);
To compute the BMI for an individual, we will need to know the height and weight
of that person. This is a fairly straightforward “prompt and read” task. The only real
decision here is with regard to the type of variable to use for storing the height and
weight. People often talk about height and weight in whole numbers, but the question
to ask is whether or not it makes sense for people to use fractions. Do people ever
describe their heights using half-inches? The answer is yes. Do people ever describe
their weights using half-pounds? Again the answer is yes. So it makes sense to store
the values as doubles, to allow people to enter either integer values or fractions:
System.out.println("Enter next person's information:");
System.out.print("height (in inches)? ");
 
Search WWH ::




Custom Search