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

Java Program Write a method, numberDaysInMonth , that takes two integer paramete

ID: 3576538 • Letter: J

Question

Java Program

Write a method, numberDaysInMonth , that takes two integer parameters, a month of the year (with January being month 1) and a year. If the month is in the range 1-12 and the year is greater than 0, it returns the number of days in the mon th for that year. Otherwise it returns 0. There are always 30 days in September, April, June, an d November. These are the months numbered 9, 4, 6, and 11. All of the other months have 31 days, except February (month 2). February has 28 days unless it is a leap year when it has 29 days. A year is a leap year if it is divisible by 4, unless the year is also divisible by 100. Years tha t are divisible by 100 are leap years only when they are also divisible by 400. The year 2000 was a leap year, but the year 1900 was not.

Explanation / Answer

package org.students;

import java.util.Scanner;

public class NoOfDaysInMonth {

   public static void main(String[] args) {

       // Declaring variables
       int month, year, no_of_days;
      
       //String array of Month names
       String months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};

       // Scanner class object is used read the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       /*
       * This loop continue to execute until user enters a valid month number
       * between 1 and 12 inclusive
       */

       while(true)
       {
           // Getting the month number entered by the user
           System.out.print("Enter Month Number :");
           month = sc.nextInt();
           if(month<1 || month>12)
           {
               System.out.println("** Invalid.Month must be between 1 and 12 (Inclusive) **");
               continue;
           }
           else
           break;
       }
      
       while(true)
       {
           // Getting the month number entered by the user
           System.out.print("Enter Year :");
           year = sc.nextInt();
           if(year<0)
           {
               System.out.println("** Invalid.Year must be greater than zero **");
               continue;
           }
           else
           break;
       }
      
       // Calling the method by passing the month,year as arguments
       no_of_days = daysInMonth(month, year);

       // Displaying the number of days in that month of the year
       System.out.println("The No of days In the Month '" + months[month-1] + "' of year "+ year + " is:" + no_of_days);
  

   }

   // This method will finds the no of day in the month based on year number
   // also
   private static int daysInMonth(int month, int year) {

       int val=0;
       // Declaring an array and initializing corresponding days in the month
       int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

       /*
       * if the month number is not equal to 2 then its returns the no of days
       * in that month of an year
       */
if (month != 2) {
               val= months[month - 1];
           }
           /*
           * If the month is 2 the check whether the year is leap year or not
           * If yes,the it gets the no of days from the months[] array If
           * not,it returns the days in that month
           */
           else if (month == 2) {
               if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
                   val= months[month - 1] + 1;
               } else {
                   val= months[month - 1];
               }
           }

      
       return val;

   }

}

____________________

Output#1:

Enter Month Number :2
Enter Year :2000
The No of days In the Month 'February' of year 2000 is:29

_________________

Output#2:

Enter Month Number :2
Enter Year :1900
The No of days In the Month 'February' of year 1900 is:28

______Thank You

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