InterviewSolution
| 1. |
Solve : Java heart rate calculator? |
|
Answer» I'm writing a simple program to calculate someone's target heart rate, which is said to be between 50-85% of their max heart rate. Their max heart rate is defined as 220 minus their age. but I've a feeling it's a problem with data types between strings and ints. You're close; it's actually because of the way you are doing the MATH: Code: [Select]int targetHeartRateMin = (50/100) * getMaxHeartRate(); int targetHeartRateMax = (85/100) * getMaxHeartRate(); in this expression, the (50/100) and (85/100) values consist of int's. What this means is that the division will have a result that is an int; the problem is that since 50/100 is 0.5, the int result is 0; same with 85/100 (it rounds down as well). so you end up multiplying it by ZERO. The solution is to force that calculation to be a float: Code: [Select]int targetHeartRateMin = (int)((50f/100f) * getMaxHeartRate()); int targetHeartRateMax = (int)((85f/100f) * getMaxHeartRate()); Basically, the f makes the literals floats, so their result will also be a float, and a float multiplied by a int will be a float as well, and that get's cast to an int... I made that sound way more complicated than it is. Another note, is that you might want to store the Result of getMaxHeartRate() beforehand, particularly if it asks for user input; if so you would need to enter the age twice. Ah right, okay. I thought it would do the calculation first and then convert the result to an int. Thanks anyway, works great. Quote Another note, is that you might want to store the Result of getMaxHeartRate() beforehand, particularly if it asks for user input; if so you would need to enter the age twice.Yep, I am doing that actually. I'm using Scanner to get input and a setUserAge() method to save it to a variable.Who can help me To have much higher efficiency during exercises fitness / aerobic heart tries preferably 20 to 30 minutes maximum heartbeat which a person's heart can the afford. The written program for the calculation of the maximum heartbeat if given the entrance age, weight in kilograms and the number of heartbeat per minute (BEATS per minute). FORMULA MHR = 223 - age. Feil MHR formula = 211 415 - 0.5 * age * weight + 4.5. THR = (MHR-RHR) + RHR * 1.25. I need te write this in jgrasp. thanks! |
|