Java Reference
In-Depth Information
only in the
Account
object being manipulated—
account1
or
account2
. In this exercise, you'll define
a new
displayAccount
method that contains
one
copy of that output statement. The method's pa-
rameter will be an
Account
object and the method will output the object's
name
and
balance
. You'll
then replace the six duplicated statements in
main
with calls to
displayAccount
, passing as an argu-
ment the specific
Account
object to output.
Modify class
AccountTest
class of Fig. 3.9 to declare the following
displayAccount
method
after
the closing right brace of
main
and
before
the closing right brace of class
AccountTest
:
public static void
displayAccount(Account accountToDisplay)
{
// place the statement that displays
// accountToDisplay's name and balance here
}
Replace the comment in the method's body with a statement that displays
accountToDisplay
's
name
and
balance
.
Recall that
main
is a
static
method, so it can be called without first creating an object of the
class in which
main
is declared. We also declared method
displayAccount
as a
static
method.
When
main
needs to call another method in the same class without first creating an object of that
class, the other method
also
must be declared
static
.
Once you've completed
displayAccount
's declaration, modify
main
to replace the statements
that display each
Account
's
name
and
balance
with calls to
displayAccount
—each receiving as its
argument the
account1
or
account2
object, as appropriate. Then, test the updated
AccountTest
class to ensure that it produces the same output as shown in Fig. 3.9.
Making a Difference
3.16
(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 Amer-
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's 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.
] 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 pa-
rameters. For each attribute provide
set
and
get
methods. The class also should include a method that
calculates and returns the person's age (in years), a method that calculates and returns the person's
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 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.
3.17
(Computerization of Health Records)
A health-care issue that has been in the news lately is
the computerization of health records. This possibility is being approached cautiously because of
sensitive privacy and security concerns, among others. [We address such concerns in later exercises.]
Computerizing health records could make it easier for patients to share their health profiles and his-
tories among their various health-care professionals. This could improve the quality of health care,
help avoid drug conflicts and erroneous drug prescriptions, reduce costs and, in emergencies, could
save lives. In this exercise, you'll design a “starter”
HealthProfile
class for a person. The class attri-
butes should include the person's first name, last name, gender, date of birth (consisting of separate