4 1st Task Create a program that uses a switch statement and
4.
1st Task: Create a program that uses a switch statement and case statements to calculate the number of days in any given month based upon a users input.
February is a unique month. Prompt the user again to input the year if the user wants the number of days for February.
Hint***. You will need to use some of the code from your leap year program.
Use the new Scanner (System.in); method for input. Make sure the program can deal with any integer input. If a number entered does not correspond to a month of the year print “Invalid Month.”
Solution
Hi friend, Please find my program.
Please let me know in case of any issue:
import java.util.Scanner;
public class MonthDays {
public static void main(String[] args) {
// scanner object to take user input
Scanner sc = new Scanner(System.in);
int month;
int year = 0;
// taking user input
System.out.print(\"Enter month number: \");
month = sc.nextInt();
if(month == 2){
System.out.print(\"Enter year: \");
year = sc.nextInt();
}
int days = 0;
switch (month) {
case 1:
days = 31;
break;
case 2:
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
days = 29;
else
days = 28;
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 31;
break;
case 12:
days = 31;
break;
default:
System.out.println(\"Invalid Month.\");
}
System.out.println(\"Number of days in month \"+month+\": \"+days);
}
}
/*
SAMPLE RUN:
Enter month number: 6
Number of days in month 6: 30
*/



