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

Design a program that will allow the user to input a date value to three variabl

ID: 3767667 • Letter: D

Question

Design a program that will allow the user to input a date value to three variables for month, day and a 4 digit calendar year.

Validate the data so that month is 1-12 and so that the day is not less than 1 and not greater than the number of days for the corresponding month.

Call a function that returns a value of true if the year is a leap year and false otherwise if the month is February in order to validate the day.

Display a message to the user that confirms the validity of the date or the error that occured.

A leap year is one that can be evenly divided by 4 unless it is also divisible by 100 OR it is evenly divisible by 400.

To see more about this go to the following link http://www.mathsisfun.com/leap-years.html

(no Java)

Explanation / Answer

import java.util.Scanner;

/**
*
*/

/**
* @author Srinivas Palli
*
*/
public class DateValidation {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner scanner;
       try {
           scanner = new Scanner(System.in);
           int month, day, year;

           System.out.print("Enter Day :");
           day = scanner.nextInt();

           System.out.print("Enter Month :");
           month = scanner.nextInt();

           System.out.print("Enter Year :");
           year = scanner.nextInt();

           if (month >= 1 && month <= 12) {

               if (day >= 1 && day <= noOfdaysInMonth(month, year)) {

                   System.out.println("valid day");
               } else {
                   System.out.println("invalid day");
               }
           } else {

               System.out.println("invalid month");
           }

       } catch (Exception e) {
           // TODO: handle exception
       }
   }

   /**
   * @param month
   * @param year
   * @return number of days in a month
   */
   public static int noOfdaysInMonth(int month, int year) {
       int daysInMonth;
       if (month == 4 || month == 6 || month == 9 || month == 11)

           daysInMonth = 30;

       else

       if (month == 2)

           daysInMonth = (checkLeapYear(year)) ? 29 : 28;

       else

           daysInMonth = 31;

       return daysInMonth;

   }

   /**
   * @param year
   * @return check the given year is leap year or not
   */
   public static boolean checkLeapYear(int year) {

       // if year is divisible by 4, it is a leap year

       if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
           return true;
       else
           return false;
   }

}

OUTPUT:

Test1 (Invalid Day)
Enter Day :30
Enter Month :2
Enter Year :2005
invalid day

Test2 (Invalid Month)
Enter Day :25
Enter Month :13
Enter Year :2001
invalid month

Test3 (Invalid Day in month 2(non leap))
Enter Day :29
Enter Month :2
Enter Year :2005
invalid day

Test4 (valid Day in month 2(leap year))
Enter Day :29
Enter Month :2
Enter Year :2004
valid day

Test5 (valid Day in month 2( non leap year))
Enter Day :28
Enter Month :2
Enter Year :2005
valid day

Test6 (valid Day in month 12)
Enter Day :31
Enter Month :12
Enter Year :2001
valid day

Test7 (valid Day in month 11)
Enter Day :30
Enter Month :11
Enter Year :2011
valid day

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote