0% found this document useful (0 votes)
2 views6 pages

Java Task 4

The document contains a Java program that determines the number of days in a specified month of a given year. It uses a constructor to take user input for the month and year, then employs a switch statement to calculate the number of days based on the month and leap year rules. The program outputs the name of the month, the year, and the corresponding number of days.

Uploaded by

mageshproject123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Java Task 4

The document contains a Java program that determines the number of days in a specified month of a given year. It uses a constructor to take user input for the month and year, then employs a switch statement to calculate the number of days based on the month and leap year rules. The program outputs the name of the month, the year, and the corresponding number of days.

Uploaded by

mageshproject123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME: MAGESH

RRRN: 200181601013

FIND A DAYS IN A MONTH USING CONSTRUCTOR

PROGRAM:
import java.util.Scanner;
public class Year {
private Year(){
Scanner input = new Scanner(System.in);

int number_Of_DaysInMonth = 0;
String MonthOfName = "Unknown";

System.out.print("Input a month number: ");


int month = input.nextInt();

System.out.print("Input a year: ");


int year = input.nextInt();

switch (month) {
case 1:
MonthOfName = "January";
number_Of_DaysInMonth = 31;
break;
case 2:
MonthOfName = "February";
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
{
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
break;
case 3:
MonthOfName = "March";
number_Of_DaysInMonth = 31;
break;
case 4:
MonthOfName = "April";
number_Of_DaysInMonth = 30;
break;
case 5:
MonthOfName = "May";
number_Of_DaysInMonth = 31;
break;
case 6:
MonthOfName = "June";
number_Of_DaysInMonth = 30;
break;
case 7:
MonthOfName = "July";
number_Of_DaysInMonth = 31;
break;
case 8:
MonthOfName = "August";
number_Of_DaysInMonth = 31;
break;
case 9:
MonthOfName = "September";
number_Of_DaysInMonth=30
break;
case 10:
MonthOfName = "October";
number_Of_DaysInMonth = 31;
break;
case 11:
MonthOfName = "November";
number_Of_DaysInMonth = 30;
break;
case 12:
MonthOfName = "December";
number_Of_DaysInMonth = 31;
}
System.out.print(MonthOfName + " " + year + " has " +
number_Of_DaysInMonth + " days\n");
}

public static void main(String[] args) {


Year obj=new Year();

}
}
OUTPUT:

You might also like