Java Perpetual Calendar

by Michael Bertrand




Getting Started with Java

Java is a programming language used to deliver executable content, that is, a program, through a web page. The Java object code resides on the server together with the HTML file which triggers it with tags like:

        <applet code="Calendar.class" width=421 height=481></applet>
When the Web browser receives this tag, it downloads and starts to execute the Java object file Calendar.class. The "width=421" and "height=481" settings specify the amount of screen space the browser will allocate to the Java applet (in pixels). Java includes GUI facilities like combo boxes and line drawing which a Java-enabled browser translates into equivalent API calls on the platform the browser is running on. So Java not only allows programs to be sent across the Web, but also makes them platform independent; no matter which platform was used to create the program, it will run on any computer hooked up to the Web through a browser understanding Java (Netscape 2 or above, Microsoft Internet Explorer 2 or above).

The Java applet embedded at the top of this page, CALENDAR, is a perpetual calendar maker. Enter any year after 1582 (the start of the modern calendar), choose a month, then click 'New Calendar' to show the calendar for that month.

View source: Calendar.java

Java is an event-driven GUI API, so familiarity with other such systems will be a great help in understanding Java. The drop-down combo box through which the user chooses a month in CALENDAR, for example, will be familiar to Windows programmers, even though Java calls it a 'Choice' object (in fact the same phrase, 'addItem', is used in both Visual Basic and Java to load strings into a combo box). Similarly the paint() method in Java resembles the Paint Event in Visual BASIC or the code a Windows SDK programmer writes when receiving a WM_PAINT message.

Java source code looks much like C++, which is based in turn on C. CALENDAR uses five utility functions for basic calculations like deciding if a given year is a leap year and determining the day of the week on which the first day of the month falls. I wrote and tested these functions, about 150 lines of code, in a C program, then inserted them as methods into the Java class virtually without change.

Software Tools

Of course you need a Java-enabled browser to test your code. Create a minimal HTML file with the applet tag referring to the Java object (class) file. Browsers will open an HTML file on disk as if it were on a server, so you needn't be connected to the Web at all during development (in Netscape 2.01, choose File / Open File in Browser).

Next you need the Java compiler, which you can download for free from the creators of Java, Sun Microsystems, at:

Java Development Kit

The Java Development kit (JDK) includes the compiler and other tools and sample applications, including source code. As of this writing (August 1996), the current release of the JDK for Windows is JDK 1.0.2, available for 32-bit Windows only (Win95 or WinNT). Which JDK you want depends on the platform you're developing on. After installing the JDK in Win95, for example, you can compile Java source from the DOS command line with:

        C:\>javac Calendar.java

A successful compile results in the creation of Calendar.class, the object file embedded in an HTML file which you can then test in your browser.

Learning Java

Books abound on this subject. I recommend checking out The Java Series from Addison-Wesley, particularly The Java Programming Language, by Ken Arnold and Java creator James Gosling. Like Kernighan and Ritchie's The C Programming Language, Arnold and Gosling is concise, correct, and thorough on Java basics, with many short examples artfully explained. Prentice Hall publishes additional titles in The Java Series, including the helpful Graphic Java: Mastering the AWT by David Geary and Alan McClellan. "AWT" stands for "Abstract Window Toolkit", a supplement to Java enabling graphics and interactive controls like combo boxes. Graphic Java includes a CD_ROM with the source for many examples discussed in the book and software tools including the JDK 1.0.2 for Windows 95. Further details on these series can be found at the publishers:

Addison Wesley Java Series

Prentice Hall Java Series

The Web contains a wealth of information on Java. One stop is Sun itself, which features online tutorials in addition to the sample applications, including source code, that come with the JDK:

Java Tutorial

Java links are plentiful (searching "Java" in Yahoo produces hundreds). Two master sites I've found useful are:

Symantec Java Central

Gamelan

Gamelan is a spectacular repository of Java information, nicely organized into sections like "Arts and Entertainment", "News", "Programming in Java", and so on. Searching Gamelan for "calendar" turned up 17 items as of this writing, including one perpetual calendar applet together with source code (beat to the punch again!).

Another resource is src.zip in the JDK, containing Java source code for the AWT and other Java libraries. Either unzip src.zip or use a utility program like WinZip to peek at the zipped files. ChoicePeer.java, for example, contains little except the names of Choice's methods: addItem and select. A little experimenting shows that select is the key to setting an initial string in a Choice object (a Java combo box), a method akin to dredging C header files for function prototypes.

Plucking Applets from the Web

Java classes are public much like the HTML for a page showing in your browser. By way of example, here's how to capture the perpetual calendar applet referenced in Gamelan from Netscape 2.01. First note the time when viewing the page containing the applet (11:49 am in this case), then choose View / Document Info to show the HTML file's cached name (M0P1UCSU.HTM). Close Netscape immediately. The last three files in the local CACHE directory, in date/time order, are:
        M0P1UCSU HTM         1,212  08-24-96 11:49a M0P1UCSU.HTM
        M0P1UCT5 CLA         5,709  08-24-96 11:49a M0P1UCT5.CLA
        M0P1UCVO JAV         5,629  08-24-96 11:51a M0P1UCVO.JAV
This works less well if you've viewed the page before and the browser uses previous files from the cache; then the relevant files have the earlier date and time (this is the point of the cache, to obviate resending data). I find it easiest simply to delete all cached files before the trolling session. This ensures that the cached files all result from the last session, at the expense of slowing the browser down a bit until the cache is built back up.

You don't always get the java file. In this case the author linked in the source code and, since I viewed it before leaving Netscape, it showed up in the cache. You don't need it to capture the applet however. Copy the three files into a separate subdirectory. The HTML file, M0P1UCSU.HTM, calls the applet with this line:

        <applet code=calendar.class height=300 width=375></applet>
So rename the cached class file accordingly (this name is hard coded inside the class file and cannot be changed short of recompiling):
        C:\>rename M0P1UCT5.CLA calendar.class
That's it. You can now view M0P1UCSU.HTM in your browser and it will load and run the calendar.class applet. And of course the applet can be called from any other HTML file with the tag referring to calendar.class. This process becomes more complicated if the applet shows GIF files, but similar principles apply there as well.

Creating the Perpetual Calendar

I decided to try a perpetual calendar maker as my first Java application, thinking this would exercise basic calculating, control, graphics, and input functions, at the same time creating a program of some utility, available now to the world at large.

Pope Gregory XIII established the modern calendar in 1582 by removing three leap years from every 400 year cycle, compared to the old Julian calendar which provided for a leap year every fourth year without exception. The excluded leap years were those a multiple of 100 but not of 400; so 1700, 1800, and 1900 were no longer leap years under the Gregorian calendar, but the year 2000, a multiple of 400, is still a leap year.

Normally the calendar advances one day each year because a year is 52 weeks and one day. Leap years, however, are 52 weeks and two days, so the calendar advances by two days following a leap year. CALENDAR calculates how many normal years and how many leap years have elapsed between 1582 and the given year, then calculates the day of the week for January 1 of the given year by comparing with 1582. Calculating the day of the week for the given month is then a simple matter, and the calendar can be drawn.


Mike teaches Mathematics and programming at Madison Area Technical College, Madison, WI 53704.