Use this shell http://www.heypasteit.com/clip/0IJF6G Problem: Write a program th
ID: 3731720 • Letter: U
Question
Use this shell
http://www.heypasteit.com/clip/0IJF6G
Problem:
Write a program that will input a numeric month from the user and outputs the number of the days in the given month.
If the user enters 2, ask the user for the year to find out if the year is a leap year or not.
Output:
Print out the name of the month and number of days in that month.
Requirements:
Just to give you practice using switch statements you are required to use 2 switch statements for this program.
• If statement will be used to assign the appropriate name of the month to a String variable (monthName).
• Switch statement will be used to assign the number of days to an int variable.
Note: This switch statement must have only one break for all the 31day months and only one break for all the 30 day months.
The saying goes:
Thirty days has September, April, June, and November.
All the rest have 31, excepting February,
Which has 28 till leap year makes it 29.
The rule for leap years is:
If the year is a multiple of 4 it is a leap year,
unless it is a multiple of 100, then it is not a leap year,
unless it is a multiple of 400, then it is a leap year.
if (((year % 4 == 0 ) && ((year % 100 != 0)) || (year % 400 == 0))) daysOfMonth = 29; else daysOfMonth = 28;
_____________________________________________________________________________
Example
Please enter an integer for a month (1-12): 5
There are 31 days in May.
_____________________________________________________________________________
Example Run 2:
Please enter an integer for a month (1-12): 2
What year are you interested in? 2000
In the year 2000: There are 29 days in February.
_____________________________________________________________________________
Example Run 3:
Please enter an integer for a month (1-12): 13
Error: you input 13 and that is not in the range 1 to 12
Explanation / Answer
DaysInMonth.java
import java.util.Scanner;
public class DaysInMonth {
public static void main(String[] args) {
int month, days;
String mon;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
while (true) {
//Getting the input entered by the user
System.out.print("Please enter an integer for a month (1-12): ");
month = sc.nextInt();
switch (month) {
case 1:
{
days = 31;
mon = "January";
break;
}
case 2:
{
int year;
System.out.print("What year are you interested in? ");
year = sc.nextInt();
mon = "February";
if (isleap(year))
days = 29;
else
days = 28;
System.out.print("In the year 2000: ");
break;
}
case 3:
{
mon = "March";
days = 31;
break;
}
case 4:
{
mon = "April";
days = 30;
break;
}
case 5:
{
mon = "May";
days = 31;
break;
}
case 6:
{
mon = "June";
days = 30;
break;
}
case 7:
{
mon = "July";
days = 31;
break;
}
case 8:
{
mon = "August";
days = 31;
break;
}
case 9:
{
mon = "September";
days = 30;
break;
}
case 10:
{
mon = "October";
days = 31;
break;
}
case 11:
{
mon = "November";
days = 30;
break;
}
case 12:
{
mon = "December";
days = 31;
break;
}
default:
{
System.out.println("Error: you input " + month + " and that is not in the range 1 to 12.");
continue;
}
}
break;
}
System.out.println("There are " + days + " days in " + mon + ".");
}
// This method will check whether the year is leap year or not
private static boolean isleap(int year) {
if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
return true;
else
return false;
}
}
________________
Output:
Please enter an integer for a month (1-12): 5
There are 31 days in May.
________________
Output#2:
Please enter an integer for a month (1-12): 2
What year are you interested in? 2000
In the year 2000: There are 29 days in February.
________________
Output#3:
Please enter an integer for a month (1-12): 13
Error: you input 13 and that is not in the range 1 to 12.
Please enter an integer for a month (1-12): 9
There are 30 days in September.
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.