Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monito

ID: 3831192 • Letter: #

Question

(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, 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. The class, named HeartRates.java will have properties that include the person’s first name, last name and date of birth which will be modelled by a class named DateOfBirth.java that will have attributes for the month, day and year of birth, all of which are integers.

Your classes should have constructors that receive all data as parameters. For properties provide set and get functions as necessary. The HeartRates class also should include a function getAge that calculates and returns the person’s age (in years based only on the birth year and current year), a function getMaxiumumHeartRate that calculates and returns the person’s maximum heart rate and a function getTargetHeartRate that calculates and returns the person’s maximum and minimum target heart rate. Write a test class called HeartDemo.java   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.

Note: The DateOfBirth and HeartRates classes should have all private properties. The age should only be calculated based only on the year. To get the current year from the computer you can use the following expression Year.now().getValue();.There is a sample I/O session below

Explanation / Answer

Here is your code: -

HeartRate.java

import java.time.Year;

public class HeartRates {
   private String firstName;
   private String lastName;
   private DateOfBirth dob;

   public HeartRates(String fName, String lName, DateOfBirth db) {
       this.firstName = fName;
       this.lastName = lName;
       this.dob = db;
   }

   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public DateOfBirth getDob() {
       return dob;
   }

   public void setDob(DateOfBirth dob) {
       this.dob = dob;
   }
  
   public int getAge() {
       int age = 0;
       int currentYear = Year.now().getValue();
       age = currentYear -this.getDob().getYear();
       return age;
   }
  
   public int getMaxiumumHeartRate() {
       return 220 - getAge();
   }
  
   public void getTargetHeartRate() {
       System.out.println("Minimum Target Heart Rate:"+(50*getMaxiumumHeartRate())/100);
       System.out.println("Maximum Target Heart Rate:"+(85*getMaxiumumHeartRate())/100);
   }
}

DateOfBirth.java

public class DateOfBirth {
   private int day;
   private int month;
   private int year;

   public DateOfBirth(int d, int m, int y) {
       this.day = d;
       this.month = m;
       this.year = y;
   }

   public int getDay() {
       return day;
   }

   public void setDay(int day) {
       this.day = day;
   }

   public int getMonth() {
       return month;
   }

   public void setMonth(int month) {
       this.month = month;
   }

   public int getYear() {
       return year;
   }

   public void setYear(int year) {
       this.year = year;
   }

}

HeartDemo.java

public class HeartDemo {
   public static void main(String[] args) {
       DateOfBirth dob = new DateOfBirth(26, 3, 1992);
       HeartRates hr = new HeartRates("Dean", "Winchester", dob);
       System.out.println("Name :"+hr.getFirstName()+" "+hr.getLastName());
       System.out.println("Date Of Birth :"+dob.getDay()+"/"+dob.getMonth()+"/"+dob.getYear());
       System.out.println("Age :"+hr.getAge());
       System.out.println("Maximum Heart Rate :"+hr.getMaxiumumHeartRate());
       hr.getTargetHeartRate();
   }
}

Sample output: -

Name :Dean Winchester
Date Of Birth :26/3/1992
Age :25
Maximum Heart Rate :195
Minimum Target Heart Rate:97
Maximum Target Heart Rate:165