In: Computer Science
12.9 (Target-Heart-Rate Calculator App) 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 (AHA), the formula for calculating your maximum heart rate in beats per minute is 220 minus your age in years (http://bit.ly/AHATargetHeartRates). Your target heart rate is a range that is 50–85% of your maximum heart rate. [Note: These formulas are estimates provided by the AHA. Maximum and target heart rates may vary based on the health, fitness and gender of the individual. Always consult a physician or qualified health care professional before beginning or modifying an exercise program.] Write an app that inputs the person’s age, then calculates and displays the person’s maximum heart rate and target-heart-rate range.
(code in java please)
You are making an app so it should be assumed that you will be
working with GUI programming.
so first we need to enter user's basic details like name, age,
gender etc.
for taking input make respective variables like String Name,
Char gender, int age;
and declare any other variables which you like.
after that you just take input from user from a
textbox/textfield.
like assign and id or variable name to the textbox/textfield.
ex: nameTF you can give name to the text field in which you will
input user's name.
EditText name = (EditText) findViewById(R.id.EnterText);
Button B1 = (Button) findViewById(R.id.nameTF);
String str = text.getText().toString();
This code can be done in android studio for making android
apps.
after that respective codes have been written we can make the
formula for calculating heart rate.
As its said above that the target heart rate is calculated as 220-age(in years)
so create a variable as
int targetHR = 220 - age;
Or whatever is the formula, just make a variable and apply
operations on it. You can make that variable in float also. That
will be good option.
After that you just need to output that variable on some label
or text view.
Use the setText method as
//write this in xml file
<TextView
android:id="@+id/heartRateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
//and write this in the java file
TextView hr = (TextView)findViewById(R.id.heartRateTV);
hr.setText(""+targetHR);
This will display the output the values of the target heart rate that you want to show to the user.