CMPU 125 Assignment 1: Calculating Body Mass Index (BMI)

Due on Thursday, September 11th, by midnight.

This lab requires you to use the acm.jar package. To use this package, you need to download the file acm.jar from our course web page and save it either on your Desktop or in your cs125 directory. After you have downloaded and saved this file, you need to add the jar file to your project.  This process is outlined below.

Problem Specification:

The Body Mass Index (BMI) is a widely used measure of fitness. It is calculated as the ratio of a person's weight (in kilograms) to the square of the person's height (in meters).

You are to write an interactive Java program called "BMI.java", a class that calculates the BMI. 

Begin by opening NetBeans and then opening a new project. This project should be called "hw1". When you create the project, NetBeans expects you to name the main class (the main class is the class that contains the main method in the project).  Your main class should either be called Main.java or TestBMI.java. To make the acm.jar file visible to NetBeans, you should right-click (or on a Mac, hold down the ctrl key and the mouse button -- "ctrl-click") the hw1 project in the Projects pane and choose Properties... from the pull-down menu. In the Categories: pane, choose Libraries and then choose Add JAR/Folder. Navigate to find the acm.jar file, select the file and click Choose and then click OK. After you add this JAR file, you will be able to import the packages in acm.jar.
 
At this point, you should already have a main class in your project. To add another class to this project, right-click on hw1 in the upper left pane and select menu choice New..., then choose Java Class from the drop-down menu. The system will prompt you for a file name.  A suggested name (and the name used in this writeup) for this class is BMI (the IDE will add the .java). In your main class your main method should create an object of type BMI.

The BMI class should include a zero-parameter constructor that either contains all the executable statements (this is OK for now because this program should be quite short) or calls other methods that contain executable statements. The package you should import in the BMI class is acm.program.* and the class you should extend in the class definition line of BMI.java is ConsoleProgram. You must include the line this.start(); as the first line in the BMI constructor in order to bring up a console window for user interaction.

To make a more visually appealing display, look up the setFont method of the ConsoleProgram class in the acm.jar API (you can get there from the link on the course web page). Example Strings you can enter as an argument to the setFont method include "Times-18" (for 18 point Times print),  "Helvetica-BOLD-24" (for 24 point boldface Helvetica). In the full Java API you can look up the Font class to find a procedure to see all the fonts available on our Linux system if you are interested. 

Begin your program with a call to a println statement like that shown below. This argument to this statement should be a String explaining the function of the program.

 Welcome to the Body Mass Index (BMI) calculator!
You will be asked to enter a weight in
pounds and a height in inches. After this,
the BMI for your inputs will be displayed.

BMI Categories:

* Underweight = < 18.5
* Normal weight = 18.5-24.9
* Overweight = 25-29.9
* Obesity = BMI of 30 or greater
You can embed a return in a String using the '\n' (backslash n) escape sequence.

Your program should prompt the user for a height in inches and a weight in pounds (since we're assuming our clients are from the US) as shown below and should return a number representing the body mass index calculated for the input parameters using the formula given above. 

 Please enter a height in inches: 54
Please enter a weight in pounds: 75.5

Note that the height and weight may be entered as real numbers, so declare your variables accordingly as doubles. The ConsoleProgram class has many different read methods, in particular it has a read method for the primitive type double. You should use the version of readDouble that both prompts for input with a String argument and that returns a double value from the user. A sample statement to prompt for and read a numeric value named height is: 

double height = this.readDouble("Please enter a height in inches: ");


The metric conversions you will need are:

  1. The metric weight in kilograms is equal to the weight in pounds multiplied by 0.454.
  2. The metric height in meters is equal to the height in feet multiplied by 0.3046.

Because it is, in general, bad form to use literals (such as 0.454) in executable statements in a program (except in data field and local variable declarations), and because the metric conversion values are not likely to change during the program execution, you should declare the conversion values as named constants. 



The output of your program should be as follows:

 A person who weighs 75.5 pounds and is 4.5 feet tall
has a BMI of 18.1.



To output the BMI value with a single digit after the decimal point, use the DecimalFormat class. You can look up this class in the Java API to discover which package it is included in. It is very straightforward to limit the number of decimal places to be displayed. For example, to limit the number of places after the decimal to 2, you create a DecimalFormat object:
 DecimalFormat df = new DecimalFormat("0.00");
To use the DecimalFormat object to format a real number, use syntax like the following:
 double num = 234.8467392;
println("Num: " + df.format(num));
// would output Num: 234.84
In the last statement, the format method of the DecimalFormat object df is receiving a double num as an input parameter. You know the format method is an instance method, since it is called on an instance of the DecimalFormat class, df and not on the DecimalFormat class itself.  
 
Notice that there are some implicit conversions you will need to do in your program because the user enters the height in inches and the metric conversions assume the height is in feet. Declare and use local variables appropriately.

When interpreting these results, you should realize that the BMI does not take percent body fat into account, so please don't despair or go on any crash diets!

What you should hand in:

You are all coming to this class with different experience and therefore, we don't have any expectations of what you know or don't know and you can't ask any questions that we will not at least try to answer. If you don't know where to start on writing this code, we'll get you started. If you want to embellish the assignment with your own personal touches, by all means do so.
 
If you have questions, please send e-mail to either soroberts@vassar.edu or walter@cs.vassar.edu.