Friday, October 15, 2010

Java Coding InStyle

 

 1: import java.util.*;

 2:

 3: class ClockTalk {

 4:         public static void main(String[] arguments) {

 5:                 // get current time and date

 6:                 GregorianCalendar now = new GregorianCalendar();

 7:                 int hour = now.get(Calendar.HOUROFDAY);

 8:                 int minute = now.get(Calendar.MINUTE);

 9:                 int month = now.get(Calendar.MONTH) + 1;

10:                 int day = now.get(Calendar.DAYOFMONTH);

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: }


Once you have saved the file, try to compile it by entering 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 14-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.

The ClockTalk program is made up of the following sections:

·         Line 1 enables your program to use two classes that are needed to track the current date and time: java.util.Calendar and java.util.GregorianCalendar.

·         Lines 3-4 begin the ClockTalk program and its main() statement block.

·         Line 6 creates a variable called now with a special type of variable called a GregorianCalendar. The now variable stores the current date and current time and 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. 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 Line 25 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 36-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 today's date is 4-13-1997 and the time is 8:30 a.m., your program would display the following text:

Good morning.

It's 30 minutes past 8 o'clock on April 13, 1997.

Run the program several times to see how it keeps up with the clock.

The ClockTalk program uses the Gregorian calendar system that has been used throughout the Western world for many years to determine the date and time. It was introduced in 1582 when Pope Gregory XIII moved the Julian calendar system forward 10 days--turning Oct. 5, 1582, into Oct. 15, 1582. This was needed because the calendar was moving out of alignment with the seasons due to discrepancies in the Julian system. Changes introduced with version 1.1 of the Java language make it possible to create other calendar systems for use with Java programs. Check Gamelan at <http://www.gamelan.com> or other Java programming resources for these calendar systems as they become available.


Summary


Now that you can use conditional statements, the overall intelligence of your Java programs has improved greatly. Your programs can now evaluate information and use it to react differently in different situations, even if information changes as the program is running. They can decide between two or more alternatives based on specific conditions.

Using the if statement and other conditionals in programming also promotes a type of logical thinking that can reap benefits in other aspects of your life. ("If she's attractive, I'll take her to an expensive restaurant, else we're using my two-for-one Taco Barn burrito coupon.") Programming a computer forces you to break down a task into a logical set of steps to undertake and decisions that must be made, and it provides interesting insight.

One thing that conditional statements do not offer insight about is the thinking of the lyricist who penned "Just Dropped In (To See What Condition My Condition Was In)". There may be no rational explanation for lyrics such as, "I tred on a cloud, I fell eight miles high. Told my mind I'm gonna sky. I just dropped in to see what condition my condition was in."

No comments:

Post a Comment