(JAVA)Use a variable to accept and store user input. This variable will be a num
ID: 3741283 • Letter: #
Question
(JAVA)Use a variable to accept and store user input. This variable will be a number representing the month. Ex. Jan = 1, Feb =2, Mar =3, … Your code will figure out and display the name of the, the number of days in that month and for Feb ask the user to input the year and display a message whether it is a leap year or not how many days, hours, min and sec as shown in the sample output below.
i. Extra credit: figure out the best way to accept user input to help you determine whether this is a leap year or not. Based on the result, display February as 28 or 29 days with its corresponding number of seconds.
ii. Extra credit: Odd numbered months have 31 days until you get to July, then starting with August even numbered months have 31 days. How can you make your code more efficient as not having to write 12 if conditions?
Hint: A leap year is any year divisible by four except those years divisible by 100 unless the year is also divisible by 400.” You could modify your code to accept user input for the year and use the expression in the book to determine leap year.
*Some context, this all needs to be within one class. It cannot use advanced java techniques. There also must be a catch for incorrect user input*
DaysHrsSec [Java Applicationl C:1Program FilesJava idk1.8.0 144in iavaw.ex Enter the Month: 1 Month Name is: Jarn Jan has 31 days744 Hrs, 44640 Min, 2678400 Sec. DaysHrsSec [Java Applicationl C:1Program FilesJava idk1.8.0 1441biniavaw. Enter the Month: 2 Enter the year? (YYYY) 1900 1900 is NOT a leap year Month Name is: Feb Feb has 28 days672 Hrs, 40320 Min, 2419200 Sec. DaysHrsSec [Java Applicationl C:1Program FilesJava idk1.8.0 144in iavaw. Enter the Month: 2 Enter the year? (YYYY) 2000 2000 is a leap year Month Name is: Feb Sep has 29 days696 Hrs, 41760 Min, 2505600 Sec. DaysHrsSec [Java Applicationl C:1Program FilesJava idk1.8.0 144bin iavaw.e Enter the Month: 6 Month Name is: Jun Jun has 30 days720 Hrs, 43200 Min, 2592000 SecExplanation / Answer
import java.util.*;
public class Year {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in); // Declaring the scanner object which will help take user input
String[] monthNames = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
System.out.println("Enter the month : ");
int month = sc.nextInt(); // Monthh entered by user will be stored in this variable
if(month==2) // This block will be executed if month is 2
{
System.out.println("Enter the year? (YYYY) : ");
int year = sc.nextInt(); // year entered by user will be stored in this variable
boolean leap = false; // this variable will store result if year is leap or not
if(year % 4 == 0) // The following logic will determine if it is a leap year or not
{
if( year % 100 == 0)
{
if ( year % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else {
leap = false;
}
if(leap==true) // This block will show the result when it is leap year
{
System.out.println(year + " is leap year ");
System.out.println("Month name is : "+monthNames[month-1]);
System.out.println(monthNames[month-1]+" has 29 days = "+(29*24)+" Hrs,"+(29*24*60)+" Min, "+(29*24*60*60)+" Sec.");
}
else // This block will show the result when it is not leap year
{
System.out.println(year + " is NOT leap year ");
System.out.println("Month name is : "+monthNames[month-1]);
System.out.println(monthNames[month-1]+" has 28 days = "+(28*24)+" Hrs,"+(28*24*60)+" Min, "+(28*24*60*60)+" Sec.");
}
}
else if(month==1||month==3||month==5 ||month==7||month==8||month==10||month==12) // this block will show the output for alll months having 31 days
{
System.out.println("Month name is : "+monthNames[month-1]);
System.out.println(monthNames[month-1]+" has 31 days = "+(31*24)+" Hrs,"+(31*24*60)+" Min, "+(31*24*60*60)+" Sec.");
}
else if(month==4||month==6||month==9||month==11) // this block will show the output for alll months having 30 days
{
System.out.println("Month name is : "+monthNames[month-1]);
System.out.println(monthNames[month-1]+" has 30 days = "+(30*24)+" Hrs,"+(30*24*60)+" Min, "+(30*24*60*60)+" Sec.");
}
else
{
System.out.println("Invalid month entered ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.