Final answer:
To create the application, use the switch statement in Java programming to determine the number of days in a given month and year. The program should also include logic to allow the user to continue entering month and year until a sentinel value is entered. The example code provided demonstrates how to implement this functionality.
Step-by-step explanation:
To create an application that reads in the month and year from the user and prints out the number of days in that month and year, you can use the switch conditional statement in Java programming. Here is an example of how you can implement this:
import java.util.Scanner;
public class DaysMonth {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int month;
int year;
boolean terminate = false;
while (!terminate) {
System.out.print("Enter the month (1-12) or -1 to exit: ");
month = input.nextInt();
if (month == -1) {
System.out.println("Good Bye");
break;
}
System.out.print("Enter the year: ");
year = input.nextInt();
int daysInMonth;
switch (month) {
case 1: // January
case 3: // March
case 5: // May
case 7: // July
case 8: // August
case 10: // October
case 12: // December
daysInMonth = 31;
break;
case 4: // April
case 6: // June
case 9: // September
case 11: // November
daysInMonth = 30;
break;
case 2: // February
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
default:
daysInMonth = -1;
}
if (daysInMonth != -1) {
System.out.println(getMonthName(month) + " of " + year + " has " + daysInMonth + " days");
} else {
System.out.println("Invalid month");
}
}
}
public static String getMonthName(int month) {
String monthName;
switch (month) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;
default:
monthName = "Invalid month";
break;
}
return monthName;
}
}
This program prompts the user to enter the month (1-12) and the year, and then uses the switch statement to determine the number of days in that month and year. It also includes a helper method, getMonthName, to convert the month number to its corresponding name for better output.