Java Reference
In-Depth Information
■
A constructor that constructs a
Time
object with a specified elapsed time since
midnight, January 1, 1970, in milliseconds. (The values of the data fields will
represent this time.)
■
A constructor that constructs a
Time
object with the specified hour, minute, and
second.
■
Three
get
methods for the data fields
hour
,
minute
, and
second
, respec-
tively.
■
A method named
setTime(long elapseTime)
that sets a new time for the
object using the elapsed time. For example, if the elapsed time is
555550000
milliseconds, the hour is
10
, the minute is
19
, and the second is
10
.
Draw the UML diagram for the class and then implement the class. Write a
test program that creates two
Time
objects (using
new Time()
and
new
Time(555550000)
) and displays their hour, minute, and second in the format
hour:minute:second.
(
Hint
: The first two constructors will extract the hour, minute, and second from the
elapsed time. For the no-arg constructor, the current time can be obtained using
System.currentTimeMillis()
, as shown in Listing 2.6, ShowCurrentTime.java.)
10.2
(
The
BMI
class
) Add the following new constructor in the
BMI
class:
/** Construct a BMI with the specified name, age, weight,
* feet, and inches
*/
public
BMI(String name,
int
age,
double
weight,
double
feet,
double
inches)
10.3
(
The
MyInteger
class
) Design a class named
MyInteger
. The class contains:
■
An
int
data field named
value
that stores the
int
value represented by this
object.
■
A constructor that creates a
MyInteger
object for the specified
int
value.
■
A
get
method that returns the
int
value.
■
The methods
isEven()
,
isOdd()
, and
isPrime()
that return
true
if the
value in this object is even, odd, or prime, respectively.
■
The static methods
isEven(int)
,
isOdd(int)
, and
isPrime(int)
that
return
true
if the specified value is even, odd, or prime, respectively.
■
The static methods
isEven(MyInteger)
,
isOdd(MyInteger)
, and
isPrime(MyInteger)
that return
true
if the specified value is even, odd, or
prime, respectively.
■
The methods
equals(int)
and
equals(MyInteger)
that return
true
if the
value in this object is equal to the specified value.
■
A static method
parseInt(char[])
that converts an array of numeric charac-
ters to an
int
value.
■
A static method
parseInt(String)
that converts a string into an
int
value.
Draw the UML diagram for the class and then implement the class. Write a client
program that tests all methods in the class.
10.4
(
The
MyPoint
class
) Design a class named
MyPoint
to represent a point with
x
-
and
y
-coordinates. The class contains:
■
The data fields
x
and
y
that represent the coordinates with
get
methods.
■
A no-arg constructor that creates a point (
0
,
0
).
■
A constructor that constructs a point with specified coordinates.
■
Two
get
methods for the data fields
x
and
y
, respectively.
VideoNote
The
MyPoint
class