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:
- To give practice writing a simple Java applet.
- To give practice using the nested if/else control structure.
- To introduce the ideas of data validation and object
integrity.
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.
- The user's input should be handled with a TextField object.
- The prompt should be handled with a Label object.
- The result should be reported by the paint() method.
Design Specifications
- This program should be broken into two classes: Days
and MonthApplet. MonthApplet will serve as a user
interface. It will prompt the user for a number and then pass that
number to the Days.howManyDays(int) method , which returns an
integer representing the number of days in the month. The applet will
then paint the result on the screen. You will be provided with
incomplete versions of both of these classes.
- Download MonthApplet.java and
read through it. Note the following features:
- The applet has 3 instance variables, a TextField for user input,
a Label to prompt the user, and an int number to
store the number input by the user.
- The init() method initializes the applets' instance variables and
adds the GUI objects to the applet. Note that the inputField
is assigned an ActionListener to handle its actions. You won't
have to change this method.
- In its present version the paint() method has
just a single g.drawString() statement which reports that
the user's number is NOT a valid month. You will have to modify this method.
- The actionPerformed() method handles only one event: the event that occurs
when the user presses the RETURN or ENTER key in the inputField. All other
events are ignored. This method gets the user's input, converts it to an int
and stores it in the number variable. It then calls repaint() which
in turn calls the paint() method.
- Note how a String input value is converted into an int using
the Integer wrapper class:
number = Integer.parseInt( inputField.getText() );
Any value typed into a TextField is represented as a String.
- Download Days.java and read through it. Note the
following features:
- The Day class is modeled after the Math
class. It is declared final, which means that it cannot be
extended (subclassed). And its constructor is declared
private, which means that this class cannot be instantiated.
- To complete this class, you must define the
howManyDays(int) method. This should be public
static method that takes a single int parameter
and returns an integer value.
- Since howManyDays() is defined as static, it is
known as a class method (rather than an instance
method). It is associated with the Days class itself
rather than with any of its instances. Thus, to invoke this
method (in a print statement), you would use the following
reference:
System.out.println("There are " + Days.howManyDays(6)
+" days") ...
This is similar to the way you would invoke the Math.sqrt(25) method.
- It is not necessary to instantiate Days in your applet.
- Draw a UML diagram for the MonthApplet and Day
classes and have it checked. (REQUIRED)
Hints and Suggestions
- Create a Metrowerks Codewarrier project for this lab that
includes the two source files you have downloaded.
- Write a stub method for your first crack at
howManyDays(int). Have the method just return zero 0. Then
incorporate your algorithm for determining the number of days in the
month and retest the method.
- Your algoritm should use an if/else control
structure to test whether a month is even or odd. It should make use
of modular arithmetic to determine whether its parameter is
divisible by 2. Remember, the expression
N % M == 0
is used to test whether "N is divisible by M". It means literally, "is
the remainder when dividing N by M equal to 0". If so, then N is
divisible by M.
- Ask for help if you get stuck.
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
- Define a public static method named
getMonthName(int) and add it to the
Days class. This method should take a single int parameter
and should return a String giving the name of the month
represented by the integer parameter. Use the method in the g.drawString() output.
For example:
February has 28 days.
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!