Problem
(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) (http://bit.ly/AHATargetHeartRates), 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 is 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, year of birth and the current year. Your class should have a constructor that receives this data as parameters. For each attribute provide a property with set and get accessors. The class also should include a property that calculates and returns the person’s age (in years), a property that calculates and returns the person’s maximum heart rate and properties that calculate and return the person’s minimum and maximum target heart rates. Write an app that prompts for the person’s information, instantiates an object of class Heart- Rates and displays the information from that object—including the person’s first name, last name and year of birth—then calculates and displays the person’s age in years, maximum heart rate and target-heart-rate range.
Step-by-step solution
Step 1 of 5
Program Plan:
• using keyword is used for System libraries.
• The main method is defined to take input from user as first name, last name and year of birth and displays the safe heart rate range.
• Three variables fName, lName and yob is defined to store the user data.
• Console.ReadLine() method is used to read the value from the keyboard.
• Console.WriteLine() method is used to write on the console.
• Console.ReadKey() is used to read a key from the keyboard.
• HeartRate class is defined to perform the heart rate calculation.
• The values are passed through the constructor of HeartRate class.
• DateTime.Today.Year is used to get the current year.
• MaxHRate, MinTHR and MaxTHR are defined to get the max heart rate, min target heart rate and max target heart rate respectively.
Comment Step 2 of 5
Program:
Program.cs
/**********************************************************
* Program to enter person's first name, last name and year*
* of birth then calculate and display the safe heart rate *
* range * **********************************************************/
using System;
namespace TargetHeartRateCalculator
{
class Program
{
// main method
static void Main(string[] args)
{
// prompt for first name
Console.WriteLine(
$"Enter first name: ");
// get first name
var fName = Console.ReadLine();
// prompt for last name
Console.WriteLine(
$"Enter last name: ");
// get last name
var lName = Console.ReadLine();
// prompt for year of birth
Console.WriteLine(
$"Enter year of birth: ");
// get year of birth
var yob = Convert.ToInt32(Console.ReadLine());
// get heart rate
var heartRate = new HeartRate(fName, lName,
yob);
// display full name
Console.WriteLine(
$"Name: {heartRate.FirstName} "+
$"{heartRate.LastName}");
// check for birth year provided
if (heartRate.birthYear != 0)
{
// prompt person's age
Console.WriteLine(
$"Age: {heartRate.age}");
// prompt max heart rate
Console.WriteLine(
$"Maximum safe heart rate: "+
$"{heartRate.MaxHRate}");
// prompt max target heart rate
Console.WriteLine(
$"Maximum target heart rate: "+
$"{heartRate.MaxTHR}");
// prompt min target heart rate
Console.WriteLine(
$"Minimum target heart rate: "+
$"{heartRate.MinTHR}");
}
// if birth year not provided
else
{
// prompt message
Console.WriteLine(
$"Enter year of birth first.");
}
// wait for key press
Console.ReadKey();
}
}
}
Comment Step 3 of 5
HeartRate.csusing System;
namespace TargetHeartRateCalculator
{
class HeartRate
{
//private members
private string fName;
private string lName;
private int YOB;
public HeartRate(string f_Name, string l_Name,
int y_o_b)
{
//getting the current year
currYear = DateTime.Today.Year;
//assign values from constructor
FirstName = f_Name;
LastName = l_Name;
birthYear = y_o_b;
}
//assigning first name
public string FirstName
{
Get
{
return fName;
}
Set
{
fName = value == "" ? "n/a" : value;
}
}
//assign last name
public string LastName
{
Get
{
return lName;
}
Set
{
lName = value == "" ? "n/a" : value;
}
}
//assign year of birth
// default 0
public int birthYear
{
get
{
return YOB;
}
set
{
YOB = value < 1 ? 0 : value;
}
}
// read only current year
public readonly int currYear;
//calculate age
public int age
{
get
{
return currYear - birthYear;
}
}
//caluclate maximum heart rate
public int MaxHRate
{
get
{
return 220 - age;
}
}
//calculate mimimum target heart rate
public int MinTHR
{
get
{
return Convert.ToInt32(MaxHRate * 0.5);
}
}
//calculate maximum target heart rate
public int MaxTHR
{
get
{
return Convert.ToInt32(MaxHRate * 0.85);
Comment Step 4 of 5
}}
}
}
Comment Step 5 of 5
Sample Output:
Enter first name:
Jonny
Enter last name:
Walker
Enter year of birth:
1990
Name: Jonny Walker
Age: 27
Maximum safe heart rate: 193
Maximum target heart rate: 164
Minimum target heart rate: 96
Comment