(Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monitor to see that your heart rate stays within a safe range suggested by your trainers and doctors. According to the American Heart Association (AHA), the formula for calculating your maximum heart rate in beats per minute is 220 minus your age in years. Your target heart rate is a range that’s 50–85% of your maximum heart rate. [Note: These formulas are estimates provided by the AHA. Maximum and target heart rates may vary based on the health, fitness and gender of the individual. Always consult a physician or qualified health-care professional before beginning or modifying an exercise program.] Create a class called HeartRates. The class attributes should include the person’s first name, last name and date of birth (consisting of separate attributes for
the month, day and year of birth). Your class should have a constructor that receives this data as parameters. For each attribute provide set and get methods. The class also should include a method that calculates and returns the person’s age (in years), a method that calculates and returns the person’s maximum heart rate and a method that calculates and returns the person’s target heart rate. Write a Java app that prompts for the person’s information, instantiates an object of class HeartRates and prints the information from that object—including the person’s first name, last name and date of birth—then calculates and prints the person’s age in (years), maximum heart rate and target-heart-rate range.
Target-Heart-Rate Calculator in Java
/*
* Filename: HeartRates.java
*
* Description: Exercise 3.16 - Target-Heart-Rate Calculator
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class HeartRates{
private String fName;
private String lName;
private int bDay, bMonth, bYear;
// constructor
public HeartRates(String fName, String lName, int bDay, int bMonth, int bYear){
setFirstName(fName);
setLastName(lName);
setDay(bDay);
setMonth(bMonth);
setYear(bYear);
}
// setters
public void setFirstName(String fName){
this.fName = fName;
}
public void setLastName(String lName){
this.lName = lName;
}
public void setDay(int bDay){
this.bDay = bDay;
}
public void setMonth(int bMonth){
this.bMonth = bMonth;
}
public void setYear(int bYear){
this.bYear = bYear;
}
// getters
public String getFirstName(){
return fName;
}
public String getLastName(){
return lName;
}
// return person's dob
public String getDOB(){
return String.format("%d/%d/%d", bDay, bMonth, bYear);
}
// calculate and return person's age
public int getAge(){
return java.util.Calendar.getInstance().get(java.util.Calendar.YEAR) - bYear;
}
// calculate and return max heartrate
public int getMaxHeartRate(){
// max heartrate in bpm = 220 - age in years.
return 220 - getAge();
}
// calculate and return target heartrate
public String getTargetHeartRate(){
return String.format("%.0f - %.0f",
getMaxHeartRate() * 0.5, getMaxHeartRate() * 0.85);
}
}
HeartRatesTest.java
/*
* Filename: HeartRatesTest.java
*
* Description: Exercise 3.16 - Target-Heart-Rate Calculator
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
* =====================================================================================
*/
import java.util.Scanner;
public class HeartRatesTest{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
// get user information
System.out.print("Enter first name: ");
String fName = input.nextLine();
System.out.print("Enter last name: ");
String lName = input.nextLine();
System.out.print("Enter Date of birth dd mm yyyy: ");
int[] tmpdob = new int[3];
for(int i=0; i<3; i++){
tmpdob[i] = input.nextInt();
}
HeartRates user1 = new HeartRates(fName, lName, tmpdob[0], tmpdob[1], tmpdob[2]);
printInfo(user1);
}
private static void printInfo(HeartRates user){
System.out.printf("%s %s\n%s - %d years of age\nMax HeartRate: %d\n",
user.getFirstName(), user.getLastName(), user.getDOB(),
user.getAge(), user.getMaxHeartRate());
System.out.printf("Target Heart Rate Range: %s\n", user.getTargetHeartRate());
}
}