JAVA

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 1:

Enter a year: 2001
Enter a month: Jan
Jan 2001 has 31 days

SAMPLE RUN 2:

Enter a year: 2016
Enter a month: Feb
Feb 2016 has 29 days

SAMPLE RUN 3:

Enter a year: 2016

Enter a month: jan

jan is not a correct month name


Class Name: Exercise04_17

Respuesta :

Answer:

Following are the code to this question:

import java.util.*;//import package for user input

public class Exercise04_17 //defining class Exercise04_17

{

public static void main(String[] as)//defining the main method  

{

int y,d=0; //defining integer variable

String m;//defining String variable

Scanner oxc=new Scanner(System.in);//creating scanner class object oxc

System.out.print("Enter a year: ");//print message

y=oxc.nextInt();//input year value which store in y variable

System.out.print("Enter a month: ");//print message

m=oxc.next();//input month value which store in m variable

if(m.equals("Jan")||m.equals("Mar")||m.equals("May")||m.equals("Jul")||m.equals("Aug")||m.equals("Oct")||m.equals("Dec"))//defining if block to check Month value

{

d=31;//assign value in d variable

}

if(m.equals("Apr")||m.equals("Jun")||m.equals("Sep")||m.equals("Nov"))//definingif block to check month value

{

d=30;//assign value in d variable

}

if(m.equals("Feb"))//defining if block to check month value is Feb

{

if(y%4==0||y%400==0)//defining if blook to check leap year

{

d=29;//assign value in d variable

}

else//else block

{  

d=28;//assign value in d variable

}

}

if(d!=0)//defining if block that check d variable value not equal to 0

{

System.out.println(m+" "+y+" has "+d+" days");//print inserted value with message

}

else//else block

{

System.out.println(m+" is not a correct month name");//print message

}

}

}

Output:

please find the attachment.

Explanation:

Description of the code:

  • In the above code, two integer variable  "d and y" and one string variable "m" are declared, in which the "y and m" variable is used to input the value from the user end.
  • In the y variable, we simply input the value and print its value, but in the m variable, we input the value and used to check its value by using multiple if blocks and in this, we also check leap year value, and then assign value in "d" variable.
  • In the last step, we print input values with the "d" variable value.
Ver imagen codiepienagoya