Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. switch statement to validate day numbers. 2. At least two boolean variables.

ID: 3620667 • Letter: 1

Question

1. switch statement to validate day numbers.
2. At least two boolean variables.
Write C++ program that will prompt the user to enter a date and determine whether the date is a valid one. For this program, you should assume that valid years are those between 1901 and 2010, inclusive. For this range, leap years are those years that are evenly divisible by 4. Valid months are between 1 and 12, inclusive. Remember that January, March, May, July, August, October, and December all have 31 days. April, June, September, and November have 30 days. February either has 28 or 29 days, depending upon whether the year is a leap year or not.
Five different sample runs are as follows:

Please enter a date (mm dd yyyy) : 4 30 2000
4/30/2000 is a valid date.

Please enter a date (mm dd yyyy) : 13 1 1998
Invalid month: 13

Please enter a date (mm dd yyyy) : 2 29 1997
Invalid day of month: 29

Please enter a date (mm dd yyyy) : 4 33 1995
Invalid day of month: 33

Please enter a date (mm dd yyyy) : 13 17 2015
Invalid month: 13
Invalid year: 2015

Explanation / Answer

please rate - thanks

import java.util.*;
public class validdate
{
public static void main(String [] args)
{int month, day, year,days;
boolean leapy=false,gooddate=true;
Scanner in=new Scanner(System.in);
System.out.print("Please enter a date (mm dd yyyy): ");
month=in.nextInt();
day=in.nextInt();
year=in.nextInt();
    if(month<1||month>12)
          {System.out.println("invalid month "+month);
            gooddate=false;
            }
   else
            {days=getDaysInMonth(month,year);
            if(day<1||day>days)
              if(month!=2)
                 {System.out.println(" Invalid day of month "+day);
                       gooddate=false;
                   }
            else
                  {if(day>29)
                      {System.out.println(" Invalid day of month "+day);
                        gooddate=false;
                     }
                    leapy=leap(year);
               if(!leapy&&day>28)
                  {System.out.println(" Invalid day of month "+day);
                    gooddate=false;
                    }
                }
         }      

            if(year<1901||year>2010)
              { System.out.println("Invalid year "+ year);
                gooddate=false;
            }
if(gooddate)
      System.out.println(month+"/"+day+"/"+year+" is a valid date");
   
}
public static boolean leap(int year)
{if(year%400==0)
     return true;
if(year%100==0)
     return false;
if(year%4==0)
     return true;
return false;
}
public static int getDaysInMonth(int month,int year)
{int days;
switch(month)
    {case 9:
    case 4:
    case 6:
    case 11: days=30;
                 break;
    case 2:   if(leap(year))
                    days=29;
                 else days=28;
              break;
    default: days=31;
    }
return days;
}
}