// An application with a class that calculates a person's
body mass index.
import java.io.*;
import java.text.*;
class BodyMassIndex
{
public static void main (String []
args)
{
Person person
= new Person ();
person.getData
();
person.displayData
();
person.displayBMI
();
} // method main
} // class BodyMassIndex
// Class that displays a person's body mass index.
class Person
{
private String name;
private double weight, height, heightFeet,
heightInches;
public void getData ()
{
System.out.print
("Name: ");
name = Reader.readString
();
System.out.print
("Weight: ");
weight = Reader.readDouble
();
System.out.print
("Height in Feet: ");
heightFeet
= Reader.readDouble ();
System.out.print
("Height in Inches: ");
heightInches
= Reader.readDouble ();
height = 12
* heightFeet + heightInches;
} // method getData
public void displayData ()
{
System.out.println
("Name: " + name);
System.out.println
("Weight: " + Reader.decimals (weight) + " pounds.");
System.out.println
("Height: " + Reader.decimals (height) + " inches.");
} // method displayData ()
public void displayBMI ()
{
double bmi;
bmi = (705
* weight) / (height * height);
System.out.print
(name + "'s body mass index is ");
System.out.println
("" + Reader.decimals (bmi));
} // method displayBMI
} // class Person
// class that contains static I/O methods.
class Reader
{
// Reads a double from the keyboard
and returns it to the calling method.
public static double readDouble ()
{
double num;
BufferedReader
stdin = new BufferedReader (new InputStreamReader (System.in));
try
{
num = Double.valueOf (stdin.readLine ()).doubleValue ();
} catch (IOException
e) { num = 0;}
return num;
} // method readDouble
// Formats a double for string output
with two decimal places.
public static String decimals (double
num)
{
DecimalFormat
decFor = new DecimalFormat ();
decFor.setMaximumFractionDigits
(2);
decFor.setMinimumFractionDigits
(2);
return decFor.format
(num);
} // method decimals
// Reads a String from the keyboard
and returns it to the calling method.
public static String readString ()
{
String str;
BufferedReader
stdin = new BufferedReader (new InputStreamReader (System.in));
try
{
str = stdin.readLine ();
} catch (IOException
e) { str = "";}
return str;
} // method readString
} // class Reader