Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.
Getting Ready: Before going any further, you should:
downloads
directory/folder). In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above and then selecting
.java
Files: In most IDEs, .java
files
(i.e., source files) can just be copied into the project.
See the course "Help" page on your IDE for more information.
.class
and .jar
Files:
In some IDEs it is easier to use .class
files and in others it is easier to use a .jar
file
that contains the .class
files. Hence, you have been
provided with both.
See the course "Help" page on your IDE for more information.
Resources: In some IDEs, resources (e.g., images, data files) need to be in a special directory whereas in others they need to be in the working directory. See the course "Help" page on your IDE for more information.
String
class may be referred to as
the java.lang.String
class and the Scanner
class may be referred to as the java.util.Scanner
class.
ClockDriver
in the default
package) that constructs two clocks, one for Harrisonburg and one
for Paris. (Note: The Clock
class is in the time
package.)
AlarmClock.java
that contains the following:
/** * A GUI window that contains an alarm clock * with a digital display * * @author * @version 1.0 */ public class AlarmClock extends Clock { private boolean on; private int hour, minute, second; private String ampm; /** * Default Constructor */ public AlarmClock() { } /** * Explicit Value Constructor * * Constructs a clock for a particular city, setting * the time zone appropriately * * @param city The city */ public AlarmClock(String city) { } }
AlarmClock
class
so that they call the appropriate constructors in the
Clock
class.
AlarmClock
object
and instantiated as an AlarmClock
object.
AlarmClock.java
class:
/** * 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) { } /** * Turn the alarm off */ public void turnAlarmOff() { } /** * Turn the alarm on */ public void turnAlarmOn() { }
and complete the setAlarm()
method (i.e., set the
attributes of the AlarmClock
object to the
values in the corresponding parameters).
turnAlarmOn()
method.
It must set the on
attribute to true
,
create an appropriately formatted String
containing the time of the alarm, and pass the String
to the setText()
method in the Clock
class. The String
can be formatted using the
formatTime()
method in the Clock
class.
If you want to be fancy, you can add an alarm icon to the
String
using the Unicode character '\u23F0'
.
(Note: Not all fonts support this character. So, you may need to use the
"clock-like" '\u00A4'
instead.)
formatTime()
method in the Clock
class
is static. So, it can certainly be called as
Clock.formatTime()
. Can it also be called as
formatTime()
? If so, why? If not, why not?
turnAlarmOff()
method.
It must set the on
attribute to false
,
and pass the empty String
to the setText()
method.
AlarmClock
have that
a Clock
doesn't? How useful is this functionality at
this point?
Clock
class
you'll see that it has a getAMPM()
method that
returns a String
. Obviously, this method can be used
to retrieve information about the time period if you need it.
It turns out, however, that it is also used by
the updateTime()
method in the Clock
class when it displays the time (which happens every second).
Though you don't have the source code for the Clock
class you do for the AlarmClock
class since you
constructed it. That means that you can add a getAMPM()
method to the AlarmClock
class that will
override the version in the clock class.
Wouldn't it be cool to have your own time period? To that end,
add a getAMPM()
method to the AlarmClock
class that returns your initials.
getAMPM()
method so that it returns your
initials, followed by an apostrophe, an "s" and a space, followed
by the actual time period.
Hints:
(1) The getAMPM()
method in the Clock
class still returns the actual timer period.
(2) The apostrophe character is used for char
literals
so you will need to escape it.
Clock
,
the AlarmClock
, or both change?
getAMPM()
method from
the AlarmClock
class. (Though cool to create your
own time period, it's more than a little confusing.)
updateTime()
method in the Clock
class call the getAMPM()
method in the AlarmClock
class rather than the getAMPM()
method in
the Clock
class?
updateTime()
method so
that AlarmClock
objects do everything they need to
do. Specifically, add the following to
your AlarmClock.java
class:
/** * Update the time displayed on the clock */ public void updateTime() { int hourNow, minuteNow, secondNow; String ampmNow; // Call the parent's version of updateTime() // If the alarm is on, get the current hour, minute // second, and ampm; check to see if the alarm // should sound now; if it should, sound the alarm }
and complete the updateTime()
method. As alluded to
earlier, the updateTime()
method will be called every
second (in a manner that is out of your control). Your
implementation of the updateTime()
method must do
everything that needs to be done every second. (Hint: Some things
that need to be done are already being done in
the Clock
class. Take advantage of that and only do
the things that are different for AlarmClock
objects.)
updateTime()
method?
super.
from the call to the updateTime()
? In other words,
what happens if you call updateTime()
instead of
super.updateTime()
? Why?
setAlarm()
method with a version that
is not passed a parameter for second
(and uses
a default of 0). Do not duplicate any existing code!
Tester1.java
that contains the
following:
/** * A Driver for testing the Clock and AlarmClock classes */ public class Tester1 { /** * 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"); setup(home); setup(paris); } /** * Setup a Clock * * @param clock The clock to setup */ private static void setup(Clock clock) { clock.reverseColors(); } }
Tester1.java
.
setup()
method has a Clock
as a formal parameter but is actually passed an AlarmClock
.
Why did it compile properly anyway?
Tester2.java
that contains the following:
/** * A Driver for testing the Clock and AlarmClock classes */ public class Tester2 { /** * 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"); setup(home); setup(paris); } /** * Setup a clock * * @param clock The clock to setup */ private static void setup(Clock clock) { clock.reverseColors(); clock.turnAlarmOn(); } }
Tester2.java
.
Tester3.java
that contains
the following:
/** * A Driver for testing the Clock and AlarmClock classes */ public class Tester3 { /** * 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"); setup(home); setup(paris); } /** * Setup a clock * * @param clock The clock to setup */ private static void setup(AlarmClock clock) { clock.reverseColors(); clock.turnAlarmOn(); } }
Tester3.java
.
Tester4.java
that contains the
following:
/** * A Driver for testing the Clock and AlarmClock classes */ public class Tester4 { /** * The enty point of the application * * @param args The command line arguments */ public static void main(String[] args) { Clock home, paris; home = createClock("Harrisonburg"); paris = createClock("Paris"); } /** * Create and setup a Clock * * @param clock The clock to setup */ private static AlarmClock createClock(String city) { AlarmClock temp; temp = new AlarmClock(city); temp.reverseColors(); return temp; } }
Tester4.java
.
createClock()
method creates and returns
AlarmClock
objects that main
assigns
to variables declared to be Clock
objects.
Why did this class compile anyway?
AlarmClock
objects require more memory than
Clock
objects because they have all of the attributes of
Clock
objects and some additional attributes.
What do we know about the size of references to AlarmClock
and Clock
objects?
Tester5.java
that contains the
following:
/** * A Driver for testing the Clock and AlarmClock classes */ public class Tester5 { /** * The enty point of the application * * @param args The command line arguments */ public static void main(String[] args) { Clock home, paris; home = createClock("Harrisonburg"); paris = createClock("Paris"); home.setAlarm(1, 39, 45, "PM"); home.turnAlarmOn(); } /** * Create and setup a Clock * * @param clock The clock to setup */ private static AlarmClock createClock(String city) { AlarmClock temp; temp = new AlarmClock(city); temp.reverseColors(); return temp; } }
Tester5.java
.
Tester6.java
that contains the
following:
/** * A Driver for testing the Clock and AlarmClock classes */ public class Tester6 { /** * The enty point of the application * * @param args The command line arguments */ public static void main(String[] args) { AlarmClock home; Clock paris; home = (AlarmClock)createClock("Harrisonburg"); paris = createClock("Paris"); home.setAlarm(1, 39, 45, "PM"); home.turnAlarmOn(); } /** * Create and setup a Clock * * @param clock The clock to setup */ private static Clock createClock(String city) { Clock temp; temp = new Clock(city); temp.reverseColors(); return temp; } }
Tester6.java
.
createClock()
returns a Clock
that is being typecast as an AlarmClock
.
Why did this class compile?
Tester6
.
Tester7.java
that contains the
following:
/** * A Driver for testing the Clock and AlarmClock classes */ public class Tester7 { /** * The enty point of the application * * @param args The command line arguments */ public static void main(String[] args) { AlarmClock home; Clock paris; home = (AlarmClock)createClock("Harrisonburg"); paris = createClock("Paris"); home.setAlarm(1, 39, 45, "PM"); home.turnAlarmOn(); } /** * Create and setup a Clock * * @param clock The clock to setup */ private static Clock createClock(String city) { AlarmClock temp; temp = new AlarmClock(city); temp.reverseColors(); return temp; } }
Tester7.java
.
createClock()
claims to return a Clock
but it actually returns an AlarmClock
.
Why did this class compile?
Tester7
.
AlarmClock
class so that it supports four
alarms. Your existing driver must continue to work (without any changes),
hence, you will need to overload the existing methods.
SimpleTime
class that contains the hour, minute, second,
and period of the day?
Copyright 2024