Instructions:Omitted
Getting Ready:Omitted
public static void main(String[] args)
{
Clock home, paris;
home = new Clock();
paris = new Clock("Paris");
}
AlarmClock.java
that contains the
following:
AlarmClock
class
so that they call the appropriate constructors in the
Clock
class.
/**
* Default Constructor
*/
public AlarmClock()
{
super();
}
/**
* Explicit Value Constructor
*
* Constructs a clock for a particular city, setting
* the time zone appropriately
*
* @param city The city
*/
public AlarmClock(String city)
{
super(city);
}
AlarmClock
object.
/**
* The enty point of the application
*
* @param args The command line arguments
*/
public static void main(String[] args)
{
AlarmCLock home;
Clock paris;
home = new AlarmClock();
paris = new Clock("Paris");
}
AlarmClock.java
class:
and complete the setAlarm()
method (i.e., set the
attributes appropriately).
/**
* Set the alarm
*
* @param hour The hour of the alarm
* @param minute The minute of the alarm
* @param second The second of the alarm
* @param ampm "AM" for before noon, "PM" for noon and after
*/
public void setAlarm(int hour, int minute, int second, String ampm)
{
this.hour = hour;
this.minute = minute;
this.second = second;
this.ampm = ampm;
}
AlarmClock.java
class:
and complete the updateTime()
method.
/**
* Update the time displayed on the clock
*/
public void updateTime()
{
int hourNow, minuteNow, secondNow;
String ampmNow;
// Call the parent's version
super.updateTime();
// Check if the alarm is on and if the alarm should sound
if (on) {
hourNow = getHour();
minuteNow = getMinute();
secondNow = getSecond();
ampmNow = getAMPM();
if ((hourNow == hour) && (minuteNow == minute) &&
(secondNow == second) && ampmNow.equals(ampm)) {
beep();
}
}
}
home.setAlarm(1, 39, 45, "PM");
home.turnAlarmOn();
updateTime()
method?
The time does not change every second the way it should because that
behavior is provided by the updateTime()
method in the
Clock
class.
Note that if you called updateTime();
instead of super.updateTime();
you would call
the method in the AlarmClock
class. In other words,
the updateTime()
method in the AlarmClock
class would call the updateTime()
method in
the AlarmClock
. This is an "infinite recursion" and,
ultimately, results in a "stack overflow".
Tester1.java
that contains the
following:
Tester1.java
.
setup()
method has a Clock
as a formal parameter but is actually passed an AlarmClock
.
Why did it compile properly anyway?
It compiles because the AlarmClock
class extends the
Clock
class. Hence, an AlarmClock
object
"is a" Clock
.
Tester2.java
that contains the following:
Tester2.java
.
It did not compile because the Clock
class does not
have a turnAlarmOn()
method. From the compiler's
perspective, clock
in the setup()
method
is a Clock
object (and, hence, does not have a
turnAlarmOn()
method).
Tester3.java
that contains
the following:
Tester3.java
.
It will not compile because an attempt is made to pass a Clock
object (named paris
) to a method that is expecting an
AlarmClock
object.
Tester4.java
that contains the
following:
Tester4.java
.
createClock()
method creates and returns
AlarmClock
objects that main
assigns
to Clock
objects.
Why did this class compile anyway?
The createClock
method returns an
AlarmClock
object which is then assigned to a reference
variable that is supposed to point to a Clock
object.
Since an AlarmClock
object "is a" Clock
this assignment will work.
Tester5.java
that contains the
following:
Tester5.java
.
In the main()
method an attempt is made to call
the setAlarm()
method that belongs to the home
object. However, from the compiler's perspective, home
is a Clock
object and does not have such a method.
You might argue that the home
object is actually an
AlarmClock
since that is how it is created in the
createClock()
method. However, the compiler is
"unaware" of this since it is declared to be a Clock
in the main()
method.
Tester6.java
that contains the
following:
Tester6.java
.
createClock()
returns a Clock
that is being typecast as an AlarmClock
.
Why did this class compile?
The compiler has no way to determine whether a typecast is correct or not. Hence, if you explicitly typecast something the compiler will assume that you know what you're doing and allow it.
You might argue that the compiler should know that a Clock
is being returned because of the return type of the
createClock()
method. However, the object that is returned
could be an AlarmClock
(since an AlaramClock
"is a" Clock
), in which case the typecase would be
appropriate.
Tester6
.
The following exception was thrown:
Exception in thread "main" java.lang.ClassCastException: Clock cannot be cast to
AlarmClock
at Tester6.main(Tester6.java:16)
At run-time, Java noticed that an attempt was made to typecast a
Clock
as an AlarmClock
. An exception was thrown to
prevent any problems that might result.
Tester7.java
that contains the
following:
Tester7.java
.
createClock()
claims to return a Clock
but it actually returns an AlarmClock
.
Why did this class compile?
Because an AlarmClock
"is a" Clock
.
Tester7
.
It executed properly. Since the object that was returned
by createClock()
is, actually, an AlarmClock
,
the typecast did not cause any problems at run-time.
Copyright 2010