Lab 15.5.5 Digital Clock
Estimated Time: 40 minutes
Learning Objective:
• In this lab activity, the student will create an applet that will display a digital clock with hour, minute,
and second.
Description/Scenario:
• Create an applet that displays a digital clock
• Display hour, minute, and second
• Use the Thread sleep method to repaint the clock every second
• Have the clock update the time every second using a Thread
File Management:
Open BlueJ. Click on Project from the BlueJ main menu and select New. In the New Project
window and in the Look in: list box select c:\. Now, double click the javacourse folder listed in
the text window and a different New Project window opens with javacourse in the Look in: list
box. Now, double click the chap15 folder listed in the text window and a different New Project
window opens with chap15 in the Look in: list box. Type lab15.5.5 in the File name text box
and click on Create to create a lab15.5.5 subfolder in the chap15 folder.
Tasks:
Step 1 Creat DigitalClock class
a. Create a class named DigitalClock that extends Applet and implements
Runnable.
b. In
the
init() method, create the Thread and start the Thread.
c. Use
the
run() method to call the Thread sleep() method and the repaint() method to
repaint the clock every second.
d. Use this code to get the current hour, minute, and second:
Date myHours = new Date();
Date mySeconds = new Date();
Date myMinutes = new Date();
hrs = myHours.getHours();
sec = mySeconds.getSeconds();
mins = myMinutes.getMinutes(); //Note: these methods are deprecated but work.
Step 2 Run Applet
a. Run the applet in the appletviewer provided by BlueJ.
1 - 1
Fundamentals of Java Programming Lab 15.5.5
Copyright
2003, Cisco Systems, Inc.