next up previous
Next: Implementation Up: In the Laboratory: The Previous: Problem Design: Year

Problem Design: LeapYearApplet

The design of the LeapYearApplet should be similar to that of other applets we've built. It should contain a TextField for user input and a Label for prompting the user. Its interface is shown in Figure 1. Note that the interface does not contain a Button. With no button to click, how does the user tell the applet to test whether a year is a leap year or not? The answer is that a TextField generates an action event whenever the user types the Enter key in it. As with button clicks, these events can be handled by an ActionListener. Therefore, after instantiating a TextField, you can simply register it with an appropriate ActionListener:

    inputField = new TextField(10);     // Create a TextField
    inputField.addActionListener(this); // Register it with a Listener

In this case, the applet's actionPerformed() method would handle the TextField's action events just as if it were a Button.


  
Figure 1: The LeapYearApplet.
\begin{figure}
\rule{4.75in}{.05cm}

\epsfig {file=/home/ram/java/text/ch5-models/figures/leapyear.eps}
\rule{4.75in}{.05cm}\end{figure}

The LeapYearApplet should implement the ActionListener.actionPerformed() method to handle TextField actions. When the user types Enter, actionPerformed() should get the input from the TextField and convert it from String to int. Recall that the parseInt() method in the Integer class can be used for this purpose:

    int num = Integer.parseInt(yearField.getText());

Here it is assumed that the user's input is in a text field named yearField. The integer should then be passed to Year.isLeapYear(), which will return a boolean. The applet should then report the result.

How should this applet handle the reporting of the result? Perhaps the easiest way is to override Applet's paint() method, which was the approach we took in the HelloWorld applet in Chapter 1. Recall that paint() uses the following kind of statement to display a String on an applet:

    g.drawString("HelloWorld", 10, 50);

The g in this expression is a reference to paint()'s Graphics object, which controls any drawing or painting on the applet. The two numbers in the expression are the horizontal and vertical coordinates that specify where ``HelloWorld'' will be painted on the applet. You will have do some planning and experimenting to determine what values to use for these coordinates.

One important question remains: How will paint() know whether to report that the year is or is not a leap year? Perhaps the best way to handle this is to have actionPerformed() invoke the Year.isLeapYear() method and store its result in an instance variable. Suppose this boolean variable is named yearIsLeapYear. Then paint() could check its value and print the appropriate message:

    if (yearIsLeapYear) 
        g.drawString("That year is a leap year", 10, 50);

As members of LeapYearApplet, both actionPerformed() and paint() will have access to yearIsLeapYear.

Recall also, that since we have patterned Year after the Math class (and Integer class), there is no need to instantiate it in order to use its static methods. So to test whether a given number is a leap year, we could simply say

    if (Year.isLeapYear(num)) ...

The isLeapYear() method is a class method -- that is, a static method that is associated with the class itself.


next up previous
Next: Implementation Up: In the Laboratory: The Previous: Problem Design: Year
Ralph Morelli {Faculty}
12/22/1999