In: Computer Science
Written in JAVA
A valid month value mm must be from 1 to 12 (January is 1) and it must contain two digits. The day value dd must be from 1 to a value that is appropriate for the given month and it also must contain two digits. The year value yyyy must contain four digits and be a positive number. September, April, June, and November each have 30 days. February has 28 days except for leap years when it has 29. The remaining months all have 31 days. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.
After you have the three substrings for the month, day, and year, you need to convert those strings into int values. This is done with a line of code like the following (for the year).
int year = Integer.parseInt( yearString );
As soon as you find a problem with the user's input, output an appropriate error message and return from the checkDate method. Here is an example of how that might look.
if ( 4 != yearString.length() ) { System.out.println("Error with " + savedDate + ": The year must have four digits."); return; // exit from the checkDate method }
You need to determine which years are leap years. This is tricky. The main tool for doing this is the "integer remainder" operator, %, which is described on pages 68-70 of the textbook. So, for example, the year is divisible by 4 when 0 == (year % 4) is true. A year is not divisible by 100 when 0 != (year % 100) is true.
check out the solution and do Comments if any queries.
----------------------------------------------------------------
import java.util.*;
public class MyClass {
public static void checkDate(String dayString, String monthString,
String yearString) {
String savedDate = yearString + "/" + monthString + "/" +
dayString;
int day, month, year;
// convert the user input of type 'String' into 'int'
day = Integer.parseInt(dayString);
month = Integer.parseInt(monthString);
year = Integer.parseInt(yearString);
// validate the year length
if(yearString.length() != 4)
{
System.out.println("\nError with " + savedDate + ": The year must
have four digits.");
return; // exit from the checkDate method
}
// validate the month value
if(month < 1 && month > 12)
{
System.out.println("\nError with " + savedDate + ": The month must
be from 1 to 12.");
return; // exit from the checkDate method
}
// validate the day value
if(month == 2)
{
// if month is 2 then validate the day value as either 28 or
29
if(day < 1 || day > 29)
{
System.out.println("\nError with " + savedDate + ": The day value
must be either 28 or 29 days in the month of February");
return; // exit from the checkDate method
}
else
{
System.out.println("\nValid date : " + savedDate);
// if its 28 days or 29 days .. check for leap year
if(year%4 == 0 && year%100 != 0)
System.out.println("\nA Leap year");
else
System.out.println("\nNot a Leap year");
}
}
// check for other months except February month
else
{
if(month == 4 || month == 6 || month == 9 || month == 11)
{
if(day != 30)
{
System.out.println("\nError with " + savedDate + ": The day value
must be 30 in the month of April, June, September or
November");
return; // exit from the checkDate method
}
// print the valid date
else
System.out.println("\nValid date : " + savedDate);
}
else
{
if(day != 31)
{
System.out.println("\nError with " + savedDate + ": The day value
must be 31 in all the months other than April, June, September or
November");
return; // exit from the checkDate method
}
// print the valid date
else
System.out.println("\nValid date : " + savedDate);
}
}
} // checkDate method ends
public static void main(String args[]) {
// required variable declaration
String dayString, monthString, yearString;
int choice;
// creation of Scanner class object to get the user input
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.print("\n1. Enter Date\n2. Exit\n");
System.out.print("Enter your choice : ");
choice = sc.nextInt();
if(choice == 1)
{
// get the user input
System.out.print("\nEnter month : ");
monthString = sc.next();
System.out.print("Enter day : ");
dayString = sc.next();
System.out.print("Enter year : ");
yearString = sc.next();
// function call
checkDate(dayString, monthString, yearString);
}
else
{
System.out.println("\nProgram Exited!!!");
break;
}
} // while ends
} // main method ends
} // main class ends
----------------------------------------------------------------------
--------------------------------------------------------------
OUTPUT ::