Calculating total number of calories burned
$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;
}
}
java beginner
$endgroup$
add a comment |
$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;
}
}
java beginner
$endgroup$
add a comment |
$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;
}
}
java beginner
$endgroup$
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;
}
}
java beginner
java beginner
edited Sep 9 '14 at 7:43
Jamal♦
30.4k11121227
30.4k11121227
asked Sep 9 '14 at 7:39
Oompa LoompaOompa Loompa
1761311
1761311
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$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$
add a comment |
$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 useenum
, 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 inswitch
.
$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. Butenum
s are the default for me, whenever more constants of similar kind come together.
$endgroup$
– maaartinus
Sep 9 '14 at 9:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f62371%2fcalculating-total-number-of-calories-burned%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$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$
add a comment |
$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$
add a comment |
$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$
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.
edited May 23 '17 at 12:40
Community♦
1
1
answered Sep 9 '14 at 8:30
abuzittin gillifircaabuzittin gillifirca
5,992924
5,992924
add a comment |
add a comment |
$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 useenum
, 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 inswitch
.
$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. Butenum
s are the default for me, whenever more constants of similar kind come together.
$endgroup$
– maaartinus
Sep 9 '14 at 9:20
add a comment |
$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 useenum
, 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 inswitch
.
$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. Butenum
s are the default for me, whenever more constants of similar kind come together.
$endgroup$
– maaartinus
Sep 9 '14 at 9:20
add a comment |
$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$
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
.
answered Sep 9 '14 at 8:00
maaartinusmaaartinus
12.3k12669
12.3k12669
$begingroup$
Why useenum
, 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 inswitch
.
$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. Butenum
s are the default for me, whenever more constants of similar kind come together.
$endgroup$
– maaartinus
Sep 9 '14 at 9:20
add a comment |
$begingroup$
Why useenum
, 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 inswitch
.
$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. Butenum
s are the default for me, whenever more constants of similar kind come together.
$endgroup$
– maaartinus
Sep 9 '14 at 9:20
$begingroup$
Why use
enum
, though?$endgroup$
– abuzittin gillifirca
Sep 9 '14 at 8:09
$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$
@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$
@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
enum
s are the default for me, whenever more constants of similar kind come together.$endgroup$
– maaartinus
Sep 9 '14 at 9:20
$begingroup$
@Vogel612 That's why I wrote more. But
enum
s are the default for me, whenever more constants of similar kind come together.$endgroup$
– maaartinus
Sep 9 '14 at 9:20
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f62371%2fcalculating-total-number-of-calories-burned%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown