Chapter 6: More Conditionals and Loops 69
Date Validation
In this exercise you will write a program that checks to see if a date entered by the user is a valid date in the second
millenium. A skeleton of the program is in Dates.java. Open this program and save it to your directory. As indicated by the
comments in the program, fill in the following:
2. An assignment statement that sets yearValid to true if the year is between 1000 and 1999, inclusive.
3. An assignment statement that sets leap Year to true if the year is a leap year. Here is the leap year rule (there’s more to it
than you may have thought!):
4. An if statement that determines the number of days in the month entered and stores that value in variable daysInMonth.
If the month entered is not valid, daysInMonth should get 0. Note that to figure out the number of days in February you’ll
need to check if it’s a leap year.
6. If the month, day, and year entered are all valid, print “Date is valid” and indicate whether or not it is a leap year. If any
of the items entered is not valid, just print “Date is not valid” without any comment on leap year.
// ************************************************************
// Dates.java
//
public class Dates
{
public static void main(String[] args)
Scanner scan = new Scanner(System.in);
//Get integer month, day, and year from user
//Check to see if month is valid
//Check to see if year is valid
}