Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Java heart rate calculator  (Read 13757 times)

0 Members and 1 Guest are viewing this topic.

kpac

    Topic Starter
  • Web moderator


  • Hacker

  • kpac®
  • Thanked: 184
    • Yes
    • Yes
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
Java heart rate calculator
« on: October 13, 2011, 09:52:50 AM »
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.

Code: [Select]
    // based on age, return maximum HR
    public int getMaxHeartRate()
    {
        int maxHeartRate = 220 - getUserAge();
        return maxHeartRate;
    }
   
    // return target heart rate
    public String getTargetHeartRate()
    {
        int targetHeartRateMin = (50/100) * getMaxHeartRate();
        int targetHeartRateMax = (85/100) * getMaxHeartRate();
        String targetHeartRate = " between " + targetHeartRateMin + " and " + targetHeartRateMax + " BPM.";
        return targetHeartRate;
    }

The getUserAge() method just takes a user inputted year and subtracts it from a Calendar.YEAR object, which works fine.

I'm calling the method with
Code: [Select]
        System.out.printf( "Your target heart rate range is: %s", getTargetHeartRate() );

I'm not sure where I'm going wrong, but I've a feeling it's a problem with data types between strings and ints. The output I get is "between 0 and 0 BPM", which obviously isn't correct.

Any help appreciated as usual. :)

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Java heart rate calculator
« Reply #1 on: October 13, 2011, 01:52:01 PM »
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.
I was trying to dereference Null Pointers before it was cool.

kpac

    Topic Starter
  • Web moderator


  • Hacker

  • kpac®
  • Thanked: 184
    • Yes
    • Yes
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
Re: Java heart rate calculator
« Reply #2 on: October 14, 2011, 03:09:36 AM »
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.
« Last Edit: October 14, 2011, 03:21:27 AM by kpac »

leonora



    Newbie

    • Experience: Beginner
    • OS: Windows 7
    Re: Java heart rate calculator
    « Reply #3 on: May 24, 2015, 01:10:54 AM »
    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!