-
Notifications
You must be signed in to change notification settings - Fork 1
/
HeartRatesTest.java
163 lines (136 loc) · 4.67 KB
/
HeartRatesTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Muhammad Naveed.
// Heart Rate calculator.
// Assignment no. 01
import java.util.Scanner;
import java.time.LocalDate;
import java.time.Period;
// heart rate clas
class HeartRates
{
// private fields for holding user data.
private String firstName;
private String lastName;
private LocalDate dob;
// Default constructor for initializing private fields.
public HeartRates()
{
firstName = lastName = "";
dob = LocalDate.of(2023, 03, 20);
}
// Parametrized constructor.
public HeartRates(String firstName, String lastName, LocalDate dob)
{
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
}
// First name setter method.
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
// Last name setter method.
public void setLastName(String lastName)
{
this.lastName = lastName;
}
// Date of birth setter method.
public void setDob(LocalDate dob)
{
this.dob = dob;
}
// Overloaded Date of birth setter method.
public void setDob()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter your birth year : ");
int year = sc.nextInt();
System.out.print("Enter your birth month : ");
int month = sc.nextInt();
System.out.print("Enter your birth day : ");
int day = sc.nextInt();
sc.close();
LocalDate birth = LocalDate.of(year, month, day);
dob = birth;
}
// First name getter method.
public String getFirstName()
{
return firstName;
}
// Last name getter method.
public String getLastName()
{
return lastName;
}
// Date of birth getter method.
public LocalDate getDob()
{
return dob;
}
// Calculate age in years method.
public int calculateAge()
{
LocalDate nowDate = LocalDate.now();
Period dateDifference = Period.between(dob, nowDate);
int ageInYear = dateDifference.getYears();
return ageInYear;
}
// maximum heart rate finder method.
public int maximumHeartRate()
{
return (220 - calculateAge());
}
// calculate target heart rate at 50%
public int targetHeartRate1()
{
return (int)(maximumHeartRate() * 0.50);
}
// calculate target heart rate at 85%
public int targetHeartRate2()
{
return (int)(maximumHeartRate() * 0.85);
}
}
// heart rate test class.
public class HeartRatesTest
{
// main driving method.
public static void main(String[] args)
{
var sc = new Scanner(System.in);
LocalDate myBirth = LocalDate.of(2002, 06, 24);
// creating first object.
var hr_obj1 = new HeartRates("Muhammad", "Naveed", myBirth);
// creating second object.
var hr_obj2 = new HeartRates();
// prompting information from user.
System.out.print("Enter your first name : ");
hr_obj2.setFirstName(sc.nextLine());
System.out.print("Enter your last name : ");
hr_obj2.setLastName(sc.nextLine());
hr_obj2.setDob();
sc.close();
// Printing information for first object.
System.out.println("");
System.out.println("Printing Object 1 Information.");
System.out.println("------------------------------");
System.out.println("First Name : " + hr_obj1.getFirstName());
System.out.println("Last Name : " + hr_obj1.getLastName());
System.out.println("Date of birth : " + hr_obj1.getDob());
System.out.println("Age in years : " + hr_obj1.calculateAge());
System.out.println("Maximum heart rate " + hr_obj1.maximumHeartRate() + " per minute.");
System.out.println("Target heart reate lies in between " + hr_obj1.targetHeartRate1() + " to " + hr_obj1.targetHeartRate2() + " beats per minute.");
System.out.println("");
// Printing information for second object.
System.out.println("");
System.out.println("Printing Object 2 Information.");
System.out.println("------------------------------");
System.out.println("First Name : " + hr_obj2.getFirstName());
System.out.println("Last Name : " + hr_obj2.getLastName());
System.out.println("Date of birth : " + hr_obj2.getDob());
System.out.println("Age in years : " + hr_obj2.calculateAge());
System.out.println("Maximum heart rate " + hr_obj2.maximumHeartRate() + " per minute.");
System.out.println("Target heart reate lies in between " + hr_obj2.targetHeartRate1() + " to " + hr_obj2.targetHeartRate2() + " beats per minute.");
}
}