fork download
  1. /*
  2. Sabater, Carl Ferddie U.
  3. ICST214 - N2
  4. 11.___.2015
  5. E3 - Target-Heart-Rate Calculator (3.16/139 Deitel)
  6. Problem:
  7. (Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monitor to see
  8. that your heart rate stays within a safe range suggested by your trainers and doctors. According to the
  9. American Heart Association (AHA) ( www.americanheart.org/presenter.jhtml?identifier=4736 ),
  10. the formula for calculating your maximum heart rate in beats per minute is 220 minus your age in
  11. years. Your target heart rate is a range that’s 50–85% of your maximum heart rate. [Note: These for-
  12. mulas are estimates provided by the AHA. Maximum and target heart rates may vary based on the
  13. health, fitness and gender of the individual. Always consult a physician or qualified health care pro-
  14. fessional before beginning or modifying an exercise program.] Create a class called HeartRates . The
  15. class attributes should include the person’s first name, last name and date of birth (consisting of sep-
  16. arate attributes for the month, day and year of birth). Your class should have a constructor that re-
  17. ceives this data as parameters. For each attribute provide set and get methods. The class also should
  18. include a method that calculates and returns the person’s age (in years), a method that calculates and
  19. returns the person’s maximum heart rate and a method that calculates and returns the person’s target
  20. heart rate. Write a Java application that prompts for the person’s information, instantiates an object
  21. of class HeartRates and prints the information from that object—including the person’s first name,
  22. last name and date of birth—then calculates and prints the person’s age in (years), maximum heart
  23. rate and target-heart-rate range.
  24. Algorithm:
  25.  
  26. */
  27.  
  28. import java.util.*;
  29.  
  30.  
  31.  
  32.  
  33. public class Main {
  34. public static class HeartRates {
  35.  
  36.  
  37.  
  38. //Initialize Variables.
  39.  
  40. private String firstName, lastName;
  41. private int month, day, year = 0;
  42.  
  43.  
  44. //Constructor
  45.  
  46. public HeartRates(String FName, String LName, int mon, int da, int ye) {
  47.  
  48. firstName = FName;
  49. lastName = LName;
  50. month = mon;
  51. day = da;
  52. year = ye;
  53. }
  54.  
  55. //Set First Name.
  56.  
  57. public void setFirstName(String FName) {
  58. firstName = FName;
  59. }
  60. //Get First Name.
  61. public String getFirstName() {
  62. return firstName;
  63. }
  64.  
  65. //Set Last Name.
  66. public void setLastName(String LName) {
  67. lastName = LName;
  68. }
  69.  
  70. //Get Last Name.
  71.  
  72. public String getlastName() {
  73. return firstName;
  74. }
  75. //Set Month, Day and Year
  76. public void setBirthDate(int mon, int da, int ye) {
  77. month = mon;
  78. day = da;
  79. year = ye;
  80. }
  81. //Get Month, Day and Year
  82. public int getBirthDate() {
  83. return day/month/year;
  84. }
  85.  
  86. public int getAge(){
  87. return 2015 - year;
  88. }
  89.  
  90. public int MaximumHeartRate() {
  91. return 220 - getAge();
  92. }
  93.  
  94. //displays the target heart rate range
  95. public void targetHeartRate(){
  96. System.out.print("Target Heart Rate Range: "+ MaximumHeartRate() * 0.5 + " to "+ MaximumHeartRate() * 0.85);
  97. }
  98. }
  99.  
  100. public static void main(String[] args) {
  101.  
  102. Scanner input = new Scanner(System.in);
  103.  
  104. System.out.println("Enter First Name: ");
  105. String firstName = input.nextLine();
  106. System.out.println("Enter Last Name: ");
  107. String lastName = input.nextLine();
  108. System.out.println("Enter BirthDate: ");
  109. int month = input.nextInt();
  110. int day = input.nextInt();
  111. int year = input.nextInt();
  112.  
  113. HeartRates myHeartRate = new HeartRates(firstName, lastName, month, day, year);
  114. System.out.printf("Name: "+ firstName +", " + lastName);
  115. System.out.printf("Age in years: \n", myHeartRate.getAge());
  116. System.out.println("Maximum Heartrate: "+ myHeartRate.MaximumHeartRate());
  117. myHeartRate.targetHeartRate();
  118.  
  119. }
  120.  
  121. }
  122.  
  123.  
Success #stdin #stdout 0.22s 59012KB
stdin
Jaswant
Singh
08 23 1992
stdout
Enter First Name: 
Enter Last Name: 
Enter BirthDate: 
Name: Jaswant, SinghAge in years: 
Maximum Heartrate: 197
Target Heart Rate Range: 98.5 to 167.45