Ch 5 Lab: The Days in Month Problem

Authors: Bronzell Dinkins
Trinity College, Hartford, CT
For use with Chapter 5

Brief description

This is a lab that is suitable for Chapter 5. It requires simple arithmetic, relational, and if/else operations and the creation of a simple applet interface involving Button and TextField components. Its emphasizes the use of inheritance and polymorphism in creating a subclass of the java.applet.Applet class that implements the ActionListener interface.

Objectives

The objectives of this lab are:

Problem Statement

The months of the year can contain 28, 30, or 31 days. In the months preceding August (the 8th month), the odd numbered months contain 31 days and even numbered months contains 30 days, except for February (the 2nd month), which has 28 days. Beginning in August, however, the even numbered months contain 31 days and the odd numbered months 30 days. Example: September (the 9th month) has 30 days, while December (the 12th month) has 31 days. Implement a Java applet that prompts the user to enter a number which represents a month of the year, and reports whether that month contains 28, 30, or 31 days.

In-lab Directions

There are two parts to this in-lab. Part I involves writing a Java applet that solves the Days in Month problem. On this part, the TAs and Lab Instructor will give you as much help as you need to successfully complete the program. Part II involves extending the program you wrote in Part I so that it validates user input. This part is meant as a self-check that you have grasped the main concepts involved in this lab. While the TAs and Lab Instructor will try to be helpful if you run into problems, the idea on this part is to see if you can complete it on your own. The Optional part is meant as an exercise to be completed entirely on your own.

Part I

Implement a Java applet that prompts the user to enter a number which represents a month of the year, and reports whether that month contains 28, 30, or 31 days.

GUI Specifications

The Graphical User Interface (GUI) for this project should correspond to the GUI interface of the demo for this lab.

Design Specifications


Hints and Suggestions

NOTE: Upon completion of part I, have your work checked by the Lab Instructor or TA.

Part II Self-check: Data Validation

The days in month rule given above assumes that the input, value representing the month, will be between 1 and 12. So, we don't want to apply our days in month rule on numbers that are less than 1 or greater than 12.

Define a public static method named isValidMonth() and add it to the Days class. This method should take a single int parameter and should return a boolean . It should return true when its parameter meets the above definition for a valid month and false otherwise.

Modify MonthApplet.java so that it reports an error message whenever the user types in an invalid month. When completed your applet should function exactly the same way as the demo applet. If the number entered is a valid month, the applet should report the number of days. If it is not a valid month, the applet should report an error message.

NOTE: Upon completion of part II, have your work checked by the Lab Instructor or TA.

Optional Exercise

Documentation Specifications

Your Java source code files should be documented according to the specifications given in: documentation guidelines.

Lab Checkout

Have your work checked by the lab instructor before you leave the lab.

You're done. Great work!