Laboratory 7: The Leap Year Applet

March 14, 15, 2006

Bring your Java, Java, Java text to the laboratory.

Objectives

The objectives of this laboratory are
  1. To give practice designing and writing a simple Java program.
  2. To give practice using if-else and assignment statements.
  3. To give practice using basic arithmetic and relational operators.
In preparation for this lab you should review Chapter 5 of Java, Java, Java.

Problem statement

A year is a leap year if it is evenly divisible by 4 but not evenly divisible by 100 unless it is also evenly divisible by 400. So 1996 was a leap year. But 1900 was not a leap year because, although it is divisible by 4, it is also divisible by 100 and not by 400. 2000 is a leap year because it is divisible by 400. Write a Java applet GUI that allows the user to enter a year (as an integer) and reports whether the year entered is a leap year or not.

Sample Solution

Leap Year Applet

Problem Decomposition

One way to decompose this problem is to follow the design of last week's lab. This will require three classes: the LeapYearApplet class, which creates an applet as a top-level window and adds the GUI to it, the LeapYearGUIPanel, which implements the GUI for this problem as a subclass of a JPanel, and the LeapYear class, which encapsulates the expertise needed to decide whether a given value is a leap year or not. LeapYearApplet should get the input from the user (in a JTextField), pass it to LeapYear, converting it to whatever form LeapYear requires, and then display the result that LeapYear returns.

Implementation

Step 1. Set Up.

Copy and rename the following files from your lab6 folder into this week's lab7 folder:

Step 2. Adapting the Applet and Panel Classes.

Modify both LeapYearApplet and LeapYearGUIPanel so they interface with the LeapYear class. Here are a list of required changes:

Step 2. Adapting the Applet and Panel Classes.

Hand In.

Before handing in your programs, be sure to document your source code.

Have your work checked by the laboratory instructor or TA. Print out and hand in a copy of your ScrambleGUIPanel.java.

You're done. Great work!