Chapter 3 Exercise 3.16 (Target-Heart-Rate Calculator) While exercising, you can
ID: 639977 • Letter: C
Question
Chapter 3
Exercise 3.16 (Target-Heart-Rate Calculator) While exercising, you can use a hear-rate monitor to see that your heart rate stays within a safe range suggested by your trainders and doctors. According tot he American Heart Association (AHA) (www.americanheart.org/presenter.jhtml?identifer=4376) the formual for calculatinf your maximus heart rate in beats per minute is 220 minus your age in yers. Your target heart rate is a range that's 50-85% of your maximus heart rate. (Note: these formulas are estimates provided by AHA. Max and target heart rates may vary based on health, fitness and gender of individual.
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 persons age (in years) , a method that caclulates and returns the persons 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 a class HeartRates and prints the info 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), max heart rate and target heart rate range.
Explanation / Answer
//HeartRate.java
import java.util.Calendar;
import java.util.GregorianCalendar;
public class HeartRate
{
private String fname;
private String lname;
private int day;
private int month;
private int year;
//Parameterized constructor that sets the first name,
//last name and day,month and year
public HeartRate(String fname,String lname,int day,int month,int year)
{
this.fname=fname;
this.lname=lname;
this.day=day;
this.month=month;
this.year=year;
}
//setter metods
public void setFirstName(String fname)
{
this.fname=fname;
}
public void setLastName(String lname)
{
this.lname=lname;
}
public void setDate(int day,int month,int year)
{
this.day=day;
this.month=month;
this.year=year;
}
//getter methods
public String getFirstName()
{
return fname;
}
public String getLastName()
{
return lname;
}
//Returns the age of the person
public int getAge()
{
Calendar cal = new GregorianCalendar(year, month, day);
Calendar now = new GregorianCalendar();
int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
return res;
}
//Calculats the maximum heart rate
public int maximumHeartRate()
{
return 220-getAge();
}
//Calculats the target heart rate
public int targetHeartRate()
{
int minimum=50;
int maximum=85;
//generates a number in between 50 and 85
int randomNum = minimum + (int)(Math.random()*maximum);
//return a target heart rate on maximumheart rate.
return maximumHeartRate()*randomNum/100;
}
}//end of the class
----------------------------------------------------------------------
//Java program that prompts first name, last name,
//day,month and year and prints the name,age
//maximum heart rate and minimum heart rate
import java.util.Scanner;
public class TestHeartRates
{
public static void main(String[] args)
{
Scanner reader=new Scanner(System.in);
System.out.println("First name : ");
String fname=reader.nextLine();
System.out.println("Last name : ");
String lname=reader.nextLine();
System.out.println("Enter day : ");
int day=Integer.parseInt(reader.nextLine());
System.out.println("Enter month : ");
int month=Integer.parseInt(reader.nextLine());
System.out.println("Enter year : ");
int year=Integer.parseInt(reader.nextLine());
HeartRate heartRate=new HeartRate(fname, lname, day, month, year);
System.out.println("Name : "+heartRate.getFirstName()+" "+heartRate.getLastName());
System.out.println("Age : "+heartRate.getAge());
System.out.println("Maximum heart rate :"+heartRate.maximumHeartRate());
System.out.println("Minimum heart rate :"+heartRate.targetHeartRate());
}
}
------------------------------------------------------------------------
Sample output:
First name :
Michell
Last name :
Johnson
Enter day :
8
Enter month :
2
Enter year :
1988
Name : Michell Johnson
Age : 27
Maximum heart rate :193
Minimum heart rate :243
Hope this helps yyou
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.