Lab 4: BAC Calculator

Overview

The moment you have been anxiously awaiting since the very first day of class has finally arrived - it's now time to start programming. In this lab you will learn how to write and compile applets, test them, and add them to your web page.

The particular applet you will be working on today is a BAC calculator. This applet will calculate an estimate of an individual's blood alcohol content based on the quantity of beverages consumed, the alcohol percentage per drink, the person's gender and weight, and the elapsed time since the first drink. Your mission is to write the subroutine to actually perform the computation.

BAC Calculator

Intoxication is caused by the amount of alcohol in your bloodstream or your blood alcohol content (BAC). Your applet will calculate an individual's blood alcohol content based on the quantity of beverages consumed, the alcohol percentage per drink, the person's gender and weight, and the elapsed time since the first drink. The BAC percentage is computed as follows:

  1. Following consumption, alcohol is distributed in the water in the body. On average, males have 58% of their body weight as water and females have 49%. Given the weight (in kilograms) and gender of an individual, the volume of water in the individual is

    weight_kg × gender_fraction × 1000

    Note: To convert weight in pounds to kilograms, divide the weight in pounds by 2.2046.

  2. Next, compute the concentration of alcohol in water. One ounce of pure alcohol equals 29.57 milliliters. Since alcohol has a specific gravity of 0.79, the 29.57 milliliters will weigh 23.36 grams. Dividing 23.36 grams by the volume of water in the individual gives the concentration of alcohol in water:

    23.36 / (weight_kg × gender_fraction × 1000) = 0.02336 / (weight_kg × gender_fraction)

  3. On average, blood is composed of 80.6 percent water. The concentration of alcohol in blood as grams per deciliter is then the concentration of alcohol in water multiplied by 80.6:

    1.882816 / (weight_kg × gender_fraction)

    This is the theoretical instantaneous BAC for one ounce of alcohol.

  4. Now adjust this calculation for the actual content of alcohol in the beverages consumed. Multiply the BAC for one ounce of alcohol by the product of total ounces of alcohol consumed and the percent concentration of alcohol in the beverage:

    (0.01882816 × beverage_consumed × concentration) / (weight_kg × gender_fraction)

  5. A final factor to take into account is metabolism. Your liver processes alcohol out of your system at an average rate of 0.017 per hour. Multiply this value by the hours elapsed since you consumed your first alcoholic beverage, and subtract it from the result of the previous computation:

    (0.01882816 × beverage_consumed × concentration) / (weight_kg × gender_fraction) - 0.017 × hours

Want to know more? This method for estimating BAC was obtained from the National Highway Traffic Safety Administration, US Department of Transportation. Check out the original document, Computing a BAC Estimate, for details, examples, and further information.

If you are not intimately familiar with the percent alcohol content in various beverages, take a look at this chart: Percent Alcohol by Volume.

This is what your applet will look like once you have it up and running ...


Disclaimer: A person's actual BAC is dependent on many factors, including physical condition, metabolism rates, frequency of alcohol consumption, and amount of food, water, drugs or medication ingested. This calculator is in no way 100% accurate or is it a guideline for how much you can drink and still drive!

Preliminaries

You should use the same Eclipse workspace as in previous labs.

You will need to snarf the files for this assignment. Snarfing is the process of grabbing files from our server. The snarf site for this course is http://www.cs.duke.edu/courses/cps001/fall05/snarf. Use the online instructions for snarfing a project. The project name for this lab is 04_BAC in the labs folder. You should also snarf the 03_Debug project in the classwork folder.

Red Wine Calculation: Using the Dr. Java interactions pane, compute how many 4 ounce glasses of average Red Wine will put a 150lb female at the NC legal limit of 0.08% if drank within one hour. Check your answer in the calculator above. How did you compute your answer? You will put these results in the BACCalculator.html file that you snarfed.

Ready to program?

Your job is to write the subroutine to calculate the BAC percentage. Look for something like this in BACCalculator.java:

/* *************************** */ /* add subroutine below! */ /* *************************** */ /* *****************************/

Any code you write should be between the two sets of comments and you don't need to change anything outside the comments.

Your subroutine should be named ComputeBAC. It takes five parameters as input: the fluid ounces of beverage consumed, the percent alcohol content per ounce, elapsed hours, body weight in pounds, and gender. You will need to assign variable names to these parameters in your subroutine declaration and specify their types. The first four parameters are of type double and the last parameter - gender - is a String. You need to test if the string is equal to "male" or "female" and use the appropriate value when performing your computation. You should also remember that the weight is input in pounds but your computation requires you to convert it to kilograms. The value returned by your subroutine is the BAC percentage, and you need to specify its type - double - in your subroutine declaration.

Start with the method declaration and just have it return 0.0. Check to make sure that works.After that, you can go on to making the overall computation.

Once you have figured everything out and typed in your subroutine, you are ready to compile.

Running in Eclipse

In Eclipse, your code is complied each time it is saved. However, if there are still errors in your code, the compilation will not complete successfully.

Run by right-clicking on the program name (BACCalculator.java) and choosing Run as Applet. You can also run using the Ambient menu and choosing run as applet there.

My program won't compile!

As given to you, your program will have red squiggly lines indicating that certain parts do not compile. You need to fix these errors. Hovering over the red marker in the left margin will give you the error message. The first problem will be that there is no ComputeBAC subroutine written.

Do your best to go through your errors and try to fix them. Start with the top error, attempt to correct it, and save again. Then move to the next error and do the same thing. Sometimes fixing the first error will make some of the ones below it disappear, so it's best to start at the beginning and go through them one at a time.

TEST YOUR APPLET!!!!!!!!!!!!!!!!!

The applet on this webpage is correct, so you can use it as reference when testing yours. Make sure to test several possible input values. If your applet gives different results, then there is a problem in your subroutine. Go back and check your computation for logic errors, fix them, and recompile. You will need to reload and possibly restart your web browser afterwards to load the new class file.

Link to your applet

Submitting

Submit your BACCalculator.java and BACCalculator.html files using Eclispe. See the Ambient submit instructions. You should submit under class cps001, assignment name lab4.