Date Validation In this exercise you will write a program that checks to see if
ID: 642320 • Letter: D
Question
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. Save it to your directory. As indicated by the comments in the program, fill in the following:
1. An assignment statement that sets monthValid to true if the month entered is between 1 and 12, inclusive.
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. The rule for a leap year can be found in the lecture slide.
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
Explanation / Answer
import java.util.Scanner;
public class Dates
{
public static void main(String[] args)
{
int month, day, year; //date read in from user
int daysInMonth; //number of days in month read in
boolean monthValid, yearValid, dayValid; //true if input from user is valid
boolean leapYear; //true if user's year is a leap year
dayValid = false;
monthValid = false;
yearValid = false;
leapYear = false;
Scanner scan = new Scanner(System.in);
//Get integer month, day, and year from user
System.out.println ("Imput integers for month, day, & year. Press enter after each: ");
month = scan.nextInt();
day = scan.nextInt();
year = scan.nextInt();
//Check to see if month is valid
if (month >0 && month < 13)
monthValid = true;
//Check to see if year is valid
if (year > 999 && year < 2000)
yearValid = true;
//Determine whether it's a leap year
if (year % 100 == 0 && year % 400 == 0)
leapYear = true;
else
{
if (year % 100 == 0)
leapYear = false;
else
{
if (year % 4 == 0)
leapYear = true;
}}
//Determine number of days in month
if (month % 2 == 0)
day = 31;
else
day = 30;
//User number of days in month to check to see if day is valid
if(leapYear == true && month == 2 && day == 29 || day == 30 || day == 31)
dayValid = false;
if (day >= 1 && day <= 31 && dayValid == false)
dayValid = true;
else
dayValid = false;
//Determine whether date is valid and print appropriate message
if (dayValid = true && monthValid == true && yearValid == true)
System.out.println("Date is valid");
else
System.out.println("Date is invalid");
if (leapYear == true && dayValid == true && monthValid == true && yearValid == true)
System.out.println("Date is a leap year");
else
System.out.println("Date is invalid");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.