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.