Visual C# 4.14 (Target-Heart-Rate Calculator)

This Solution is for the “Making a difference Exercise” of chapter 4 in the textbook; Visual c# How to program 6th edition by Deitel.
The first portion is the Program, the second portion is the needed Class;

//Date: 4/20/17
Creates a class called HeartRate with class attributes that include the patient’s first name, last name,
year of birth and the current year. The class has a constructor that receives this data as parameters.
Each attribute provides a property with set and get accessors. The class also includes 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 that calculate and return the person’s minimum and maximum target heart rates.

The app prompts for the person’s information, instantiates an object of class HeartRates 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.
using System;
class Program
static void Main(string[] args)
//prompts for and reads firstName
Console.WriteLine(“Please enter your first name: “);
string firstName = Console.ReadLine();

//prompts for and reads lastname
Console.WriteLine(“Please enter your last name: “);
string lastName = Console.ReadLine();

//prompts for and reads yearOfBirth
Console.WriteLine(“Please enter your year of birth: “);
int yearOfBirth = int.Parse(Console.ReadLine());
//prompts for and reads currentyear
Console.WriteLine(“Please enter the current year: “);
int currentyear = int.Parse(Console.ReadLine());

//creates Heartrate object and assigns it to patient
HeartRate patient = new HeartRate(firstName, lastName, yearOfBirth, currentyear);

Console.WriteLine($”Hello {patient.FirstName}, {patient.LastName}.”); //prints patients full name
Console.WriteLine();//prints blank line
Console.WriteLine(“You are {0} years old.”, patient.PatientAge); //prints patients age
Console.WriteLine(“\nFor your age;\n\nYour max safe heart rate is {0}”, patient.MaxHeartRate);
Console.WriteLine($”\nYour max target heart rate is { patient.MaxHeartRate * 0.85}”); //calculate maximum target heart rate (85% of max)
Console.WriteLine($”\nYour min target heart rate is {patient.MaxHeartRate * 0.5}\n”); //calculate mimimum target heart rate (50% of max)


The following is the Class;

using System;

class HeartRate
//private members
private string _firstName;
private string _lastName;
private int _yearOfBirth;
private int _currentYear;

public HeartRate(string firstName, string lastName, int yearOfBirth, int currentyear)

//assign values passed into constructor
_currentYear = currentyear;
FirstName = firstName;
LastName = lastName;
YearOfBirth = yearOfBirth;


//assigning first name
public string FirstName
get { return _firstName; }
set { _firstName = value; }

//assign last name
public string LastName
get { return _lastName; }
set { _lastName = value; }

//assign year of birth
public int YearOfBirth
get { return _yearOfBirth; }
set { _yearOfBirth = value; }

//calculate patients’s age
public int PatientAge
get { return _currentYear – YearOfBirth; }

//caluclate maximum heart rate
public int MaxHeartRate
get { return 220 – PatientAge; }

Program Output:

Please enter your first name:
Please enter your last name:
Please enter your year of birth:
Please enter the current year:

Hello Some, Body.

You are 26 years old.

For your age;

Your max safe heart rate is 194

Your max target heart rate is 164.9

Your min target heart rate is 97

Press any key to continue . . .

You can  download this code for your own use HERE.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s