LAB
9 : EXPERIMENTING WITH
SPECIALIZATION
Getting Ready: Before
going any further you should:
1. Make a directory on your N: drive for this lab.
2. Setup your development
environment.
3. Download Clock.class and icon.gif, and put them in your working directory.
The documentation for the Clock class is available at javadocs/Clock.html.
Part I: In this
part of the lab you will use an existing class.
Part II: In this
part of the lab you will specialize an existing class. (Or in java terms, extend the class.)
/** * 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() { // call parent
default constructor } /** * Explicit Value Constructor * * Constructs a clock for a particular city, setting * the time zone appropriately – think about what the parent does * * @param city The city */ public AlarmClock(String city) { } } |
See worksheet
/** * 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 */ public void
setAlarm(int hour, int minute, int second, String ampm) { } /** * Turn the
alarm off */ public void
turnAlarmOff() { on = false; } /** * Turn the
alarm on */ public void
turnAlarmOn() { on = true; } |
and complete the setAlarm() method (i.e., set the attributes appropriately).
/** * 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 and check to see if the alarm // should sound now } |
and complete the udpateTime() method.
See worksheet
See worksheet
Part III: In this
part of the lab you will think about specialization in Java, and what kinds of
errors you might encounter when you compile classes that you develop.
/** * 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 home = new AlarmClock(); setup(home); setup( } /** * Setup a Clock * * @param clock The clock to setup */ private static void setup(Clock clock) { clock.reverseColors(); } } |
/** * 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 home = new AlarmClock(); setup(home); setup( } /** * Setup a clock * * @param clock The clock to setup */ private static void setup(Clock clock) { clock.reverseColors(); clock.turnAlarmOn(); } } |
/** * 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 home = new AlarmClock(); setup(home); setup( } /** * Setup a clock * * @param clock The clock to setup */ private static void setup(AlarmClock clock) { clock.reverseColors(); clock.turnAlarmOn(); } } |
/** * 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, home = createClock(" } /** * 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; } } |
/** * 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, home = createClock(" 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; } } |