<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.
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.
Next you need the Java compiler, which you can download for free from the creators of Java, Sun Microsystems, at:
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.
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 links are plentiful (searching "Java" in Yahoo produces hundreds). Two master sites I've found useful are:
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.
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.
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.