Previous Next |
Workshop: Watching the ClockThis 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. Listing 7.3. The ClockTalk Program1: 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: 22: // begin time message by showing the minutes 23: System.out.print("It's"); 24: if (minute != 0) { 25: System.out.print(" " + minute + " "); 26: System.out.print( (minute != 1) ? "minutes" : 27: "minute"); 28: System.out.print(" past"); 29: } 30: 31: // display the hour 32: System.out.print(" "); 33: System.out.print( (hour > 12) ? (hour - 12) : hour ); 34: System.out.print(" o'clock on "); 35: 36: // display the name of the month 37: switch (month) { 38: case 1: 39: System.out.print("January"); 40: break; 41: case 2: 42: System.out.print("February"); 43: break; 44: case 3: 45: System.out.print("March"); 46: break; 47: case 4: 48: System.out.print("April"); 49: break; 50: case 5: 51: System.out.print("May"); 52: break; 53: case 6: 54: System.out.print("June"); 55: break; 56: case 7: 57: System.out.print("July"); 58: break; 59: case 8: 60: System.out.print("August"); 61: break; 62: case 9: 63: System.out.print("September"); 64: break; 65: case 10: 66: System.out.print("October"); 67: break; 68: case 11: 69: System.out.print("November"); 70: break; 71: case 12: 72: System.out.print("December"); 73: } 74: 75: // display the date and year 76: System.out.println(" " + day + ", " + year + "."); 77: } 78: } Save the file when you're done, and attempt to compile it (JDK 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 it over before reading more about the program. See whether you can get a good idea about what is taking place in each of its 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 those lines rather than how it's happening. The ClockTalk program is made up of the following sections:
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 6/26/2005 at 11:33 p.m., it would display the following text: Good evening. It's 33 minutes past 11 o'clock on June 26, 2005. 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 to U.S. Eastern time: 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 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());
|
Previous Next |