Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Workshop: Watching the Clock
This hour's workshop gives you another look at each of the conditional tests you can use in your programs. For this project, you will use Java's built-in timekeeping feature, which keeps track of the current date and time, and present this information in sentence form.
Run the word processor you're using to create Java programs and give a new document the name ClockTalk.java. This program is long, but most of it consists of long conditional statements. Type the full text of Listing 7.3 into the word processor and save the file as ClockTalk.java when you're done.
Example 7.3. The ClockTalk Program
1: import java.util.*;
2:
3: class ClockTalk {
4: public static void main(String[] arguments) {
5: // get current time and date
6: Calendar now = Calendar.getInstance();
7: int hour = now.get(Calendar.HOUR_OF_DAY);
8: int minute = now.get(Calendar.MINUTE);
9: int month = now.get(Calendar.MONTH) + 1;
10: int day = now.get(Calendar.DAY_OF_MONTH);
11: int year = now.get(Calendar.YEAR);
12:
13: // display greeting
14: if (hour < 12)
15: System.out.println("Good morning.\n");
16: else if (hour < 17)
17: System.out.println("Good afternoon.\n");
18: else
19: System.out.println("Good evening.\n");
20:
21: // begin time message by showing the minutes
22: System.out.print("It's");
23: if (minute != 0) {
24: System.out.print(" " + minute + " ");
25: System.out.print( (minute != 1) ? "minutes" :
26: "minute");
27: System.out.print(" past");
28: }
29:
30: // display the hour
31: System.out.print(" ");
32: System.out.print( (hour > 12) ? (hour - 12) : hour );
33: System.out.print(" o'clock on ");
34:
35: // display the name of the month
36: switch (month) {
37: case 1:
38: System.out.print("January");
39: break;
40: case 2:
41: System.out.print("February");
42: break;
43: case 3:
44: System.out.print("March");
45: break;
46: case 4:
47: System.out.print("April");
48: break;
49: case 5:
50: System.out.print("May");
51: break;
52: case 6:
53: System.out.print("June");
54: break;
55: case 7:
56: System.out.print("July");
57: break;
58: case 8:
59: System.out.print("August");
60: break;
61: case 9:
62: System.out.print("September");
63: break;
64: case 10:
65: System.out.print("October");
66: break;
67: case 11:
68: System.out.print("November");
69: break;
70: case 12:
71: System.out.print("December");
72: }
73:
74: // display the date and year
75: System.out.println(" " + day + ", " + year + ".");
76: }
77: }
Save the file when you're done, and attempt to compile it (SDK users can enter javac ClockTalk.java at the command line). Correct any typos that cause error messages to occur during the attempted compilation. After the program compiles correctly, look over Lines 13–75 before going over the description of the program. See whether you can get a good idea about what is taking place in each of these sections and how the conditional tests are being used.
With the exception of Lines 6–11, the ClockTalk program contains material that has been covered up to this point. After a series of variables are set up to hold the current date and time, a series of if or switch conditionals are used to determine what information should be displayed.
This program contains several uses of System.out.println() and System.out.print() to display strings.
Lines 6–11 refer to a Calendar variable called now. The Calendar variable type is capitalized, just as String is capitalized in a program that uses strings. The reason for the capitalization is that Calendar is an object.
You'll learn how to create and work with objects during Hour 10, "Creating Your First Object." For this hour, focus on what's taking place in Lines 6–11 rather than how it's happening.
The ClockTalk program is made up of the following sections:
- Line 1 enables your program to use a class that is needed to track the current date and time: java.util.Calendar.
- Lines 3–4 begin the ClockTalk program and its main() statement block.
- Line 6 creates a Calendar object called now that contains the current date and time of your system. The now object will change each time you run this program (unless, of course, the physical laws of the universe are altered and time stands still).
- Lines 7–11 create variables to hold the hour, minute, month, day, and year. The values for these variables are pulled from the Calendar object, which is the storehouse for all of this information. These variables are used in the subsequent sections as the program displays information.
- Lines 14–19 display one of three possible greetings: Good morning., Good afternoon., or Good evening. The greeting to display is selected based on the value of the hour variable.
- Lines 22–28 display the current minute along with some accompanying text. First, the text It's is displayed in Line 22. If the value of minute is equal to 0, Lines 24–27 are ignored because of the if statement in Line 23. This statement is necessary because it would not make sense for the program to tell someone that it's 0 minutes past an hour. Line 24 displays the current value of the minute variable. A ternary operator is used in Lines 25–26 to display either the text minutes or minute, depending on whether minute is equal to 1. Finally, in Line 27 the text past is displayed.
- Lines 30–33 display the current hour by using another ternary operator. This ternary conditional statement in Line 32 causes the hour to be displayed differently if it is larger than 12, which prevents the computer from stating things like 15 o'clock.
- Lines 35–72 , almost half of the program, are a long switch statement that displays a different name of the month based on the integer value stored in the month variable.
- Lines 74–75 finish off the display by showing the current date and the year.
- Lines 76–77 close out the main() statement block and then the entire ClockTalk program.
When you run this program, the output should resemble the following code, with changes based on the current date and time. For example, if the program was run on 7/5/2002 at 11:36 p.m., it would display the following text:
Good evening. It's 36 minutes past 11 o'clock on July 5, 2002.
Run the program several times to see how it keeps up with the clock. If the time doesn't match the time on your computer, the Java interpreter might be using the wrong time zone to determine the current time. When the interpreter does not know the default time zone to use, it uses Greenwich Time instead.
The following statements set the current time zone:
TimeZone tz = TimeZone.getTimeZone("EST");
TimeZone.setDefault(tz);
The setDefault() method should be used before calendar or any other date-related items are created.
The first statement creates a TimeZone object called tz. The text EST is sent as an argument to the getTimeZone () method, and this causes TimeZone to be set up for Eastern Standard Time.
The second statement sets the time zone by calling the setDefault () method of the TimeZone class. If you're having trouble finding the right time zone arguments, the following statements display all valid zones recognized by Java on your system:
String[] ids = TimeZone.getAvailableIDs();
for (int i = 0; i < ids.length; i++)
System.out.println(ids[i].toString());
Summary | Next Section

Account Sign In
View your cart