/* Java program that computes (and reports) body mass index (BMI) values for ** a particular height and two particular weights. ** The premise is that the measurements are all with respect to the same ** person but the weights were taken on different dates, so that the results ** will indicate how the person's BMI value changed over time. ** The "inputs" are "hard-coded", meaning that assignment statements are ** used in place of statements that read data from a true input source ** (such as the keyboard or a file). ** ** The purpose of the program is to illustrate the use of ** --variables (including the idea that a variable's value can change) ** --symbolic constants (i.e., "final" variables) ** --arithmetic expressions, in particular ones involving numbers of ** different types (int and double) ** --casting from int to double ** --string concatenation */ public class BMICalculatorApp2 { public static void main(String[] args) { // Value needed to convert result of BMI formula when height and // weight are measured in inches and pounds rather than in meters // and kilograms, respectively. final int UNIT_CONVERSION_FACTOR = 703; int height; // height in inches (input) int weight; // weights in pounds (input) double bmi; // BMI value (output) // Using assignment statements, simulate the reading of input values. height = 67; weight = 136; // Compute BMI value. (Notice the cast to double to ensure that the // division operator has at least one operand of type double. If both // operands were of type int, the result would be, too.) bmi = UNIT_CONVERSION_FACTOR * ((double)weight / (height * height)); // Report height, weight, and BMI value. System.out.print("Height of " + height + " inches "); System.out.print("and weight of " + weight + " pounds yields "); System.out.println("BMI value of " + bmi); // Now suppose that a second input value for weight is entered, // reflecting that the person's weight has changed. We simulate // the reading of input, again, using an assignment statement. // This illustrates that an assignment statement places a new value // into a variable, replacing whatever value was there before. weight = 140; // Compute updated BMI value bmi = UNIT_CONVERSION_FACTOR * ((double)weight / (height * height)); // Report height, new weight, and new BMI value. System.out.print("Height of " + height + " inches "); System.out.print("and weight of " + weight + " pounds yields "); System.out.println("BMI value of " + bmi); } }