Days in a Month Write a class named MonthDays. The class’s constructor should accept two arguments: An integer for the month (1 = January, 2 = February, etc.) An integer for the year The class should have a method named getNumberOfDays that returns the number of days in the specified month. The method should use the following criteria to identify leap years: Determine whether the year is evenly divisible by 100. If it is, then it is a leap year if and only if it is evenly divisible by 400. For example, 2000 is a leap year, but 2100 is not. If the year is not evenly divisible by 100, then it is a leap year if and only if it is evenly divisible by 4. For example, 2008 is a leap year but 2009 is not. Demonstrate the class in a program that asks the user to enter the month (letting the user enter an integer in the range of 1 through 12) and the year. The program should then display the number of days in that month. Here is a sample run of the program:

Respuesta :

Answer:

The java program for the given scenario is as shown below.

import java.util.*;

class MonthDays {

//variables as required

static int month;

static int year;

static int days;

//variables initialized inside constructor

MonthDays(int m, int y)

{

month=m;

year=y;

}

//method to compute number of days

static int getNumberOfDays()

{

if(year%400 ==0)

days=366;

if((year%100==0) && (year%4==0))

days=366;

else

days=365;

if(month==1) return 31;

if((month==2) && (days==365)) return 28;

if( (month==2) && (days==366)) return 29;

if(month==3) return 31;

if(month==4) return 30;

if(month==5) return 31;

if(month==6) return 30;

if(month==7) return 31;

if(month==8) return 31;

if(month==9) return 30;

if(month==10) return 31;

if(month==11) return 30;

else return 31;

}

}

public class Test

{ public static void main(String args[]) {

MonthDays ob = new MonthDays(1,2020);

int d=ob.getNumberOfDays();

System.out.print("The month has "+ d +" days");

}

}

Explanation:

1. The variables are declared to hold days in a year, month and the year. All the variables are declared static.

2. Inside constructor, the month and year variables are initialized.

3. Inside method, getNumberOfDays(), it is examined whether the year is leap year or not. The number of days are returned based on the month.

4. Inside another class, Test, having the main() method, an integer variable, d, is declared.

5. An object of the class MonthDays is declared having two parameters, month and year.

6. Using this variable, the method, getNumberOfDays() is called.  

7. The value returned in step 6 is stored in the integer variable, d.

8. The number of days in the given month is displayed.

9. The class Test is declared public since it contains the main() method.

10. The program is written in java since classes are required.

11. The program can be tested for the weekdays and month alike.

12. The output is attached in an image.

13. Since java is purely object-oriented language, program involving classes is best implemented in java.

Ver imagen bucuresti

The java program which is used to calculate the months and days is:

  1. import java.util.*;
  2. class MonthDays {
  3. static int month;
  4. static int year;
  5. static int days;
  6. MonthDays(int m, int y)
  7. {
  8. month=m;
  9. year=y;
  10. }
  11. static int getNumberOfDays()
  12. {
  13. if(year%400 ==0)
  14. days=366;
  15. if((year%100==0) && (year%4==0))
  16. days=366;
  17. else
  18. days=365;
  19. if(month==1) return 31;
  20. if((month==2) && (days==365)) return 28;
  21. if( (month==2) && (days==366)) return 29;
  22. if(month==3) return 31;
  23. if(month==4) return 30;
  24. if(month==5) return 31;
  25. if(month==6) return 30;
  26. if(month==7) return 31;
  27. if(month==8) return 31;
  28. if(month==9) return 30;
  29. if(month==10) return 31;
  30. if(month==11) return 30;
  31. else return 31;
  32. }
  33. }
  34. public class Test
  35. {
  36. public static void main(String args[]) {
  37. MonthDays ob = new MonthDays(1,2020);
  38. int d=ob.getNumberOfDays();
  39. System.out.print("The month has "+ d +" days");
  40. }
  41. }

What is Java Programming?

This is a High Level Programming which is also an object oriented programming that is used to write and support applications.

Read more about java programming here:

https://brainly.com/question/25458754