4
\$\begingroup\$

The following question was taken from Absolute Java 5th ed. by Walter Savitch:

A government research lab has concluded that an artificial sweetener commonly used in diet soda pop will cause death in laboratory mice. A friend of yours is desperate to lose weight but cannot give up soda pop. Your friend wants to know how much diet soda pop it is possible to drink without dying as a result. Write a program to supply the answer. The program has no input but does have defined constants for the following items: the amount of artificial sweetener needed to kill a mouse, the weight of the mouse, the starting weight of the dieter, and the desired weight of the dieter. To ensure the safety of your friend, be sure the program uses the weight at which the dieter will stop dieting, rather than the dieter’s current weight, to calculate how much soda pop the dieter can safely drink. You may use any reasonable values for these defined constants. Assume that diet soda contains 1/10th of 1% artificial sweetener. Use another named constant for this fraction. You may want to express the percent as thedouble value 0.001. (If your program turns out not to use a defined constant, you may remove that defined constant from your program.)

This is the code that I have written:

public class Question4 {

    private static final double AMOUNT_OF_ARTIFICIAL_SWEETENER_TO_KILL_MOUSE_IN_LITRE = 0.001;
    private static final double WEIGHT_OF_MOUSE_IN_GRAMS = 25;
    private static final double DESIRED_WEIGHT_OF_DIETER_IN_GRAMS = 65000;
    private static final double CONCENTRATION_OF_ARTIFICIAL_SWEETENER_IN_DIET_SODA = 0.001;

    public static void main(String[] args) {

        System.out.println(maxAmountOfDietSodaDieterCanDrinkInLitre()
                + " litres.");// 2600.00 litres
    }

    public static double maxAmountOfDietSodaAMouseCanDrinkInLitre() {
        return AMOUNT_OF_ARTIFICIAL_SWEETENER_TO_KILL_MOUSE_IN_LITRE
                / CONCENTRATION_OF_ARTIFICIAL_SWEETENER_IN_DIET_SODA;
    }

    public static double maxAmountOfDietSodaDieterCanDrinkInLitre() {
        return (maxAmountOfDietSodaAMouseCanDrinkInLitre() * DESIRED_WEIGHT_OF_DIETER_IN_GRAMS)
                / WEIGHT_OF_MOUSE_IN_GRAMS;
    }
}
\$\endgroup\$

1 Answer 1

10
\$\begingroup\$

Each of the three methods runs correctly, and does so in constant time. That's good.

Comments

You wrote one comment. Either

  • remove the comment (because it could become inconsistent if the constants change and is therefore a maintenance headache), or
  • hard-code the answer and discard everything else (System.out.println("2600.0 litres.");), or
  • write the comment as asssertion instead (assert maxAmountOfDietSodaDieterCanDrinkInLitre() == 2600.0;)

Constants

The names of the constants and methods are extremely clear, which is good. You might want to shorten them, though. If so, I suggest:

private static final double DIETER_TARGET_KG = 65,
                            MOUSE_KG = 0.025,
                            MOUSE_SWEETENER_LD50_L = 0.001,
                            SWEETENER_CONCENTRATION_IN_SODA = 0.001;

That puts the members in an order where each constant is related to its neighbour. As a bonus, they are in alphabetical order. I've chosen to use SI units throughout. "LD50" is standard notation in toxicology for the median lethal dose.

Methods

I think you could have decomposed the problem more logically. For example, what quantity is being expressed by the parenthesized expression

(maxAmountOfDietSodaAMouseCanDrinkInLitre() * DESIRED_WEIGHT_OF_DIETER_IN_GRAMS)

… whose units are L ∙ g?

I would therefore write the solution as follows:

/**
 * Median lethal dose of artificial sweetener, in litres per kilogram of
 * the subject's body weight.
 */
public static double sweetenerLD50() {
    return MOUSE_SWEETENER_LD50_L / MOUSE_KG;
}

/**
 * Median lethal dose of diet soda, in litres per kilogram of the subject's
 * body weight.
 */
public static double sodaLD50() {
    return sweetenerLD50() / SWEETENER_CONCENTRATION_IN_SODA;
}

/**
 * Median lethal dose of diet soda, in litres.
 *
 * @param subjectKG subject's body weight, in kilograms
 */
public static double sodaLD50(double subjectKG) {
    return subjectKG * sodaLD50();
}

public static void main(String[] args) {
    System.out.println(sodaLD50(DIETER_TARGET_KG) + " litres.");
}

Note that unlike the // 2600.0 litres comment, JavaDoc serves as useful documentation, and is unlikely to become out of sync if the numbers change.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.