Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Method 4: A method to calculate the current BAC. This method will: o Have 5 para

ID: 3886101 • Letter: M

Question

Method 4: A method to calculate the current BAC. This method will:
o Have 5 parameters:
weight in pounds
volume distribution to use
metabolic rate to use
number of drinks consumed
number of hours elapsed
o Define and use two local (non-static) constants, that contain the values for:
the number of grams in one pound (453.592)
the grams of alcohol in one drink (14)

Reminder: CS210 coding standards require: Local constants will be declared at the top of the method in which they are used, before any other executable statements or variable declarations.
o Convert the weight to grams.
o Calculate the grams of alcohol in the drinks consumed.
o Compute the initial BAC using the Widmark formula on page 1.
o Compute the current BAC using the last formula on page 1.
o Return a double value: the current BAC.

JAVA PROGRAMMING

Problem Summary A person would like to know how to determine his/her blood alcohol content (BAC). In Colorado, the legal driving limit is 0.08. Blood alcohol content (BAC) is usually expressed as a percentage of ethanol in the blood in units of mass of alcohol per volume of blood or mass of alcohol per mass of blood. So, for example, in North America a BAC of 0.1 (0.1% or one tenth of one percent) means that there are 0.10 g of alcohol for every dL (docilikx) oblood. A standard American drink contains 0.5 US fl oz (15 ml) of alcohol by volume, and is equivalent to: 12 oz of beer (5% alcohol) 5 oz of wine (12% alcohol) 1.5 oz shot of 80 proof liquor (40% alcohol) This means that there are 14 grams of alcohol in one standard American drink. Back in 1932, Swedish scientist Eric Widmark established the following formula for determining a person's initial BAC is: Alcohol consumed (in grams) weight (in grams) x Volume distribution (by gender) BAC = x 100 In the original formulas, the volume distributions of alcohol in the blood by gender were fixed values. One value (0.68) was used for males, and another value (0.55) was used for females However, in 1986, English chemist Robert Forrest came up with a better way to calculate the volume distribution (by gender), taking the person's weight and height into account.

Explanation / Answer


import java.util.Scanner;

public class WilsonBACCalculator {
// /**
// * Main method, instantiates a scanner for input, sets some constants for calculations
// * and calls the class methods for calculating
// * @param args, the command line arguments
// */
public static void main(String[] args) {
// // set constant local vars for
final double LBS_IN_KG = 2.20462D;
final double INCHES_IN_METER = 39.3701D;
final double MALE_METABOLIC_RATE = 0.015D;
final double FEMALE_METABOLIC_RATE = 0.014D;
// call description method
describeProgram();
// // get input without type checking
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the person's height in inches: ");
double height = scanner.nextDouble();
System.out.println("Please enter the person's weight, in pounds: ");
double weight = scanner.nextDouble();
System.out.println("Please enter number of drinks consumed: ");
int drinksConsumed = scanner.nextInt();
System.out.println("Please enter number of WHOLE hours elapsed since first drink was consumed: ");
int hoursElapsed = scanner.nextInt();
// // convert weight to kg, and height to meters
double heightMeters = height / INCHES_IN_METER;
double weightKgs = weight / LBS_IN_KG;
// // get volumes for both genders
double maleVol = calculateMenVolume(weightKgs, heightMeters);
double femaleVol = calculateWomenVolume(weightKgs, heightMeters);
// get current BAC for both genders
double maleCurrent = calculateCurrentBAC(weight, maleVol, MALE_METABOLIC_RATE, drinksConsumed, hoursElapsed);
double femaleCurrent = calculateCurrentBAC(weight, femaleVol, FEMALE_METABOLIC_RATE, drinksConsumed, hoursElapsed);
// display results
displayResults(drinksConsumed, hoursElapsed, maleCurrent, femaleCurrent);

}
//
// /**
// * Program description method, simply states what will be done
// */
public static void describeProgram() {
System.out.println("This program will calculate and show current BAC");
System.out.println("Based upon height, weight, and gender, drinks, and hours, the program will calculate BAC at this time:");
System.out.println();
}
//
// /**
// * This method calculates the male volume
// * @param weightKgs, weight in kg
// * @param heightMts, height in meters
// * @return volDist, the male volume
// */
public static double calculateMenVolume(double weightKgs, double heightMts) {
double volDist = 1.0178 - ((0.012127 * weightKgs) / Math.pow(heightMts, 2));
return volDist;
}
//
// /**
// * This method calculates the female volume
// * @param weightKgs, weight in kg
// * @param heightMts, height in meters
// * @return volDist, the female volume
// */
public static double calculateWomenVolume(double weightKgs, double heightMts) {
double volDist = 0.8736 - ((0.0124 * weightKgs) / Math.pow(heightMts, 2));
return volDist;
}
//
// /**
// * This method will calculate the person's current BAC
// * @param weightLbs
// * @param volDistrib
// * @param metabolicRate
// * @param numDrinks
// * @param hoursElapsed
// * @return currentBAC, the current BAC
// */
public static double calculateCurrentBAC(double weightLbs, double volDistrib, double metabolicRate, int numDrinks, double hoursElapsed) {
final double gramsInLb = 453.592;
final int gramsInDrink = 14;

double convertedWeight = weightLbs * gramsInLb;
int totalAlcoholInDrinks = numDrinks * gramsInDrink;
double initialBAC = (totalAlcoholInDrinks / (convertedWeight * volDistrib)) * 100;
double currentBAC = initialBAC - (hoursElapsed * metabolicRate);
return currentBAC;
}

/**
* Method to display results and format them accordingly
* @param numDrinks
* @param hours
* @param maleBAC
* @param femaleBAC
*/
public static void displayResults(int numDrinks, int hours, double maleBAC, double femaleBAC) {
System.out.println();
System.out.println();
System.out.println("After drinking " + numDrinks + "drinks in " + hours + " hours: ");
System.out.printf(" Male BAC is: %.3f", maleBAC);
System.out.println();
System.out.printf(" Female BAC is: %.3f ", femaleBAC);
}
}

sample output

This program will calculate and show current BAC
Based upon height, weight, and gender, drinks, and hours,
the program will calculate BAC at this time:

Please enter the person's height in inches:
6.2
Please enter the person's weight, in pounds:
150
Please enter number of drinks consumed:
3
Please enter number of WHOLE hours elapsed since first drink was consumed:
2


After drinking 3drinks in 2 hours:
   Male BAC is: -0.032
   Female BAC is: -0.030