Java Reference
In-Depth Information
instance variable (assume zero degrees if no value is specified and Celsius if no
scale is specified), one with two parameters for the two instance variables, and a
no-argument constructor (set to zero degrees Celsius). Include the following:
(1) two accessor methods to return the temperature—one to return the degrees
Celsius, the other to return the degrees Fahrenheit—use the following formulas
to write the two methods, and round to the nearest tenth of a degree:
degreesC
=
5(
degreesF
32)
/
9
degreesF
=
(9(
degreesC
)
/
5)
+
32;
−
(2) three mutator methods: one to set the value, one to set the scale (
F
or
C
),
and one to set both; (3) three comparison methods: an
equals
method to test
whether two temperatures are equal, one method to test whether one temper-
ature is greater than another, and one method to test whether one tempera-
ture is less than another (note that a Celsius temperature can be equal to a
Fahrenheit temperature as indicated by the above formulas); and (4) a suit-
able
toString
method. Then write a driver program (or programs) that tests
all the methods. Be sure to use each of the constructors, to include at least one
true and one false case for each of the comparison methods, and to test at least
the following temperature equalities: 0.0 degrees C = 32.0 degrees F,
−
40.0
degrees C =
−
40.0 degrees F, and 100.0 degrees C = 212.0 degrees F.
8.
Redefine the class
Date
in Display 4.13 so that the instance variable for the
month is of type
int
instead of type
String
. None of the method headings
should change in any way. In particular, no
String
type parameters should
change to
int
type parameters. You must redefine the methods to make things
work out. Any program that uses the
Date
class from Display 4.13 should be able
to use your
Date
class without any changes in the program. In particular, the
program in Display 4.14 should work the same whether the
Date
class is defined
as in Display 4.13 or is defined as you do it for this project. Write a test program
(or programs) that tests each method in your class definition.
9.
Define a class whose objects are records on animal species. The class should have
instance variables for the species name, population, and growth rate. The growth
rate is a percentage that can be positive or negative and can exceed 100 percent.
Include a suitable collection of constructors, mutator methods, and accessor
methods. Include a
toString
method and an
equals
method. Include a
boolean
valued method named
endangered
that returns
true
when the growth rate is
negative and returns
false
otherwise. Write a test program (or programs) that
tests each method in your class definition.