2
\$\begingroup\$

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

One way to measure the amount of energy that is expended during exercise is to use metabolic equivalents (MET). Here are some METS for various activities:

  • Running 6 MPH: 10 METS
  • Basketball: 8 METS
  • Sleeping: 1 MET

The number of calories burned per minute may be estimated using the following formula: Calories/Minute= 0.0175 * MET* Weight in kilograms.

Write a program that calculates and outputs the total number of calories burned for a 150-pound person who runs 6 MPH for 30 minutes, plays basketball for 30 minutes, and then sleeps for 6 hours. One kilogram is equal to 2.2 pounds.

This is the code that I have written:

public class Question1 {

    final static int RUNNING6MPH = 10;
    final static int BASKETBALL = 8;
    final static int SLEEPING = 1;

    private static double value = 0.0175;
    private static double totalCaloriesBurned = 0;

    public static void main(String[] args) {

        totalCaloriesBurned = caloriesPerMinute(RUNNING6MPH, 150, 30)
                + caloriesPerMinute(BASKETBALL, 150, 30)
                + caloriesPerMinute(SLEEPING, 150, hoursToMinutes(6));
        System.out.println(totalCaloriesBurned);
    }

    public static double caloriesPerMinute(int met, double weight, double time) {
        return value * met * poundToKilogram(weight) * time;
    }

    public static double poundToKilogram(double pound) {
        return pound / 2.2;
    }

    public static double hoursToMinutes(double hours) {
        return hours * 60;
    }
}
\$\endgroup\$
4
\$\begingroup\$

First, not the most important point but first to grab my attention, prefer static final instead of final static in the name of least surprise.

Make everything private that can be made private, as @maartinus said.

This is a constant so make it final also:

private static double value = 0.0175;

Also name value is too generic, use the term from the field if available, you can name it CALORIES_PER_MET_PER_KG_PER_MINUTE, or at least SCALING_FACTOR.

This is not used anywhere other than main therefore should be a local variable there:

private static double totalCaloriesBurned = 0;

Then you do not need to initialize it to an unused value.

In main unnamed constant 150 is used repeatedly. You should name it weight or even better weightInPounds.

In:

public static double caloriesPerMinute(int met, double weight, double time)

caloriesPerMinute name is wrong, as it does not compute a per minute value. It could be renamed calories or caloriesBurned.

double weight, double time can be named weightInPounds and timeInMinutes to remind the user to do necessary conversion beforehand.

After renamings I suggested above the formula: value * met * poundToKilogram(weight) * time reads:

CALORIES_PER_MET_PER_KG_PER_MINUTE * met * poundToKilogram(weightInPounds) * timeInMinutes

So that it is easier to verify you did not mess up the units.

\$\endgroup\$
2
\$\begingroup\$

It's OK, just a few comments:

For RUNNING6MPH, etc., an enum seems to be optimal.

I'd recommend to start with all methods private. Only when you need them, you review them and then change the visibility.

For the time conversion, there's TimeUnit.

\$\endgroup\$
  • \$\begingroup\$ Why use enum, though? \$\endgroup\$ – abuzittin gillifirca Sep 9 '14 at 8:09
  • \$\begingroup\$ @abuzittingillifirca Why not? I also do, it nicely groups things, can be extended if needed and also used in switch. \$\endgroup\$ – maaartinus Sep 9 '14 at 8:18
  • \$\begingroup\$ @maaartinus why not is usually not the correct reasoning.. \$\endgroup\$ – Vogel612 Sep 9 '14 at 9:17
  • \$\begingroup\$ @Vogel612 That's why I wrote more. But enums are the default for me, whenever more constants of similar kind come together. \$\endgroup\$ – maaartinus Sep 9 '14 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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