Java Reference
In-Depth Information
set through a parameter to the constructor and the field is immutable. Provide an accessor
method for it called
isCourseText
.
Exercise 2.92
Challenge exercise
Create a new project,
heater-exercise
, within BlueJ. Edit
the details in the project description—the text note you see in the diagram. Create a class,
Heater
, that contains a single field,
temperature
whose type is
double-precision floating
point
—see Appendix B, section B.1, for the Java type name that corresponds to this descrip-
tion. Define a constructor that takes no parameters. The
temperature
field should be set
to the value 15.0 in the constructor. Define the mutators
warmer
and
cooler
, whose effect
is to increase or decrease the value of temperature by 5.0° respectively. Define an accessor
method to return the value of
temperature
.
Exercise 2.93
Challenge exercise
Modify your
Heater
class to define three new
double-
precision floating point
fields:
min
,
max
, and
increment
. The values of
min
and
max
should
be set by parameters passed to the constructor. The value of
increment
should be set to
5.0 in the constructor. Modify the definitions of
warmer
and
cooler
so that they use the
value of
increment
rather than an explicit value of 5.0. Before proceeding further with this
exercise, check that everything works as before.
Now modify the
warmer
method so that it will not allow the temperature to be set to a value
greater than
max
. Similarly modify
cooler
so that it will not allow
temperature
to be set to
a value less than
min
. Check that the class works properly. Now add a method,
setIncre-
ment
, that takes a single parameter of the appropriate type and uses it to set the value of
in-
crement
. Once again, test that the class works as you would expect it to by creating some
Heater
objects within BlueJ. Do things still work as expected if a negative value is passed to
the
setIncrement
method? Add a check to this method to prevent a negative value from
being assigned to
increment
.
Search WWH ::
Custom Search