Java program to calculate Heart Rate – In this article, we will explain the various ways.
The methods that have been used in this article are as follows:
- Using Scanner Class
- Using Static Method
It is absolutely known to all of us that the Heart is responsible for pumping blood to all parts of our body. It is the central element of the Circulatory system of our body.
It pumps out pure blood to various organs and tissues and impure blood to the lungs, kidneys and liver.
Thus, the Heart Rate can be defined as the number of times the Heart pumps out the blood to and from the organs in a given unit period of time. One heartbeat results in the completion of one cycle of the transported blood.
A normal adult human being has a heart rate of around 60 – 100 beats per minute. Anything lower or higher than that needs medical attention.
Java Calculate Heart Rate – Using Scanner Class
To calculate the target heart rate zone, we require the following inputs- age (age), resting heart rate (rhr), low end heart rate zone (minPer), high end hear rate zone (maxPer) and gender (g). These inputs are read at runtime by making use of Scanner class in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.Scanner; class HeartRate { public static void main(String args[]) { double hrr,min,max,maxHr; Scanner sc=new Scanner(System.in); System.out.println("Enter your age"); int age=sc.nextInt(); System.out.println("Enter resting heart rate"); int rhr=sc.nextInt(); System.out.println("Enter low end heart rate zone"); double minPer=sc.nextInt(); System.out.println("Enter high end heart rate zone"); double maxPer=sc.nextInt(); System.out.println("Choose gender :\n1.male\n2.female\n"); int g=sc.nextInt(); if(g==1) { maxHr=206.9-(0.67*age); hrr=(maxHr-rhr); min=(hrr*(minPer/100))+rhr; max=(hrr*(maxPer/100))+rhr; System.out.println("Target Heart Rate zone is between "+min+" to "+max); } else if(g==2) { maxHr=206-(0.88*age); hrr=(maxHr-rhr); min=hrr*(minPer/100)+rhr; max=hrr*(maxPer/100)+rhr; System.out.println("Target Heart Rate zone is between "+min+" to "+max); } } } |
Output1:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter your age 23 Enter resting heart rate 65 Enter low end heart rate zone 65 Enter high end heart rate zone 85 Choose gender : 1.male 2.female 1 Target Heart Rate zone is between 147.2185 to 172.5165 |
Using Static Method
In the above method, it is observed that, the entire code is written within the main method itself. If ever, in future we would like to make use of the same logic in some other part of the code, we would require to write the same set of statements again.
So, it is convenient to reuse the same block than rewriting it and for this we make use of separate static method blocks.
In our example, the main method will read all the required inputs as done in the above method. It then checks for the gender. If the gender is male (g=1) then, a static method named maleHeartRate is called by sending age, rhr, minPer and maxPer as parameters.
Else, if the gender is female (g=2) then, another static method named femaleHeartRate is called sending age, rhr, minPer and maxPer as parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.Scanner; class HeartRate { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter your age"); int a=sc.nextInt(); System.out.println("Enter resting heart rate"); int rhr=sc.nextInt(); System.out.println("Enter low end heart rate zone"); double minPer=sc.nextInt(); System.out.println("Enter high end heart rate zone"); double maxPer=sc.nextInt(); System.out.println("Choose gender :\n1.male\n2.female\n"); int g=sc.nextInt(); if(g==1) maleHeartRate(a,rhr,minPer,maxPer); else if(g==2) femaleHeartRate(a,rhr,minPer,maxPer); } static void maleHeartRate(int age,int rhr,double m1,double m2) { double hrr,min,max,maxHr; maxHr=206.9-(0.67*age); hrr=(maxHr-rhr); min=(hrr*(m1/100))+rhr; max=(hrr*(m2/100))+rhr; System.out.println("Target Heart Rate zone is between "+Math.round(min)+" to "+Math.round(max)); } static void femaleHeartRate(int age,int rhr,double m1,double m2) { double hrr,min,max,maxHr; maxHr=206-(0.88*age); hrr=(maxHr-rhr); min=hrr*(m1/100)+rhr; max=hrr*(m2/100)+rhr; System.out.println("Target Heart Rate zone is between "+Math.round(min)+" to "+Math.round(max)); } } |
Output1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Enter your age 30 Enter resting heart rate 95 Enter low end heart rate zone 65 Enter high end heart rate zone 95 Choose gender : 1.male 2.female 1 Target Heart Rate zone is between 155 to 182 |