PEARSON *4.17 (Days of a month) Write a program that prompts the user to enter a
ID: 3802257 • Letter: P
Question
PEARSON *4.17 (Days of a month) Write a program that prompts the user to enter a year and the first three letters of a month name (with the first letter in uppercase) and displays the number of days in the month. If the input for month is incorrect, display a message as shown in the following sample run: Sample Run for Exercise04 17 Enter input data for the program (Sample data provided below You may modify it) 2001 Jan Show the sample output Using the Preceeding Input command java Exercisee4 17 Enter a year: 2001 Enter a month (first three letters with the first letter in uppercase): Jan Jan 2001 has 31 days commandExplanation / Answer
// Test.java
import java.util.Scanner;
class Test
{
public static boolean isLeapYear(int year) {
if (year % 4 != 0) {
return false;
} else if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else {
return true;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a year:");
int year=sc.nextInt();
System.out.println("Enter a month( first three letters with the first letter in uppercase): ");
String month=sc.next();
sc.close();
int days= 0;
if(month.equals("Jan"))
days = 31;
else if(month.equals("Feb"))
{
if(isLeapYear(year) == true)
days = 29;
else
days = 28;
}
else if(month.equals("Mar"))
days = 31;
else if(month.equals("Apr"))
days = 30;
else if(month.equals("May"))
days = 31;
else if(month.equals("Jun"))
days = 30;
else if(month.equals("Jul"))
days = 31;
else if(month.equals("Aug"))
days = 31;
else if(month.equals("Sep"))
days = 30;
else if(month.equals("Oct"))
days = 31;
else if(month.equals("Nov"))
days = 30;
else if(month.equals("Dec"))
days = 31;
else
{
System.out.println("Invalid month");
System.exit(1);
}
System.out.println(month + " " + year + " has " + days + " days");
}
}
/*
output:
Enter a year:
201
Enter a month( first three letters with the first letter in uppercase):
Jan
Jan 201 has 31 days
Enter a year:
2016
Enter a month( first three letters with the first letter in uppercase):
Feb
Feb 2016 has 29 days
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.