Java programming! Write a program that prompts users for two integer, one for mo
ID: 3781323 • Letter: J
Question
Java programming!
Write a program that prompts users for two integer, one for month (between 1 and 12 inclusively) and another for year (between 1 and 2018 inclusively) and prints the number of days in that month of the specified year. You need to take leap year into consideration in computing days in a month. The program prompts for the inputs, making sure that the instructions are clear to users. In particular, you should direct users to enter two numbers in two separate lines, in the order of month, and year. Display the number of days in the specified month of the specified year. The outputs should be descriptive and friendly to end users.Explanation / Answer
Compile and run the below program:
package org.learning.chegg.problem;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class NoOfDaysCalculator {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int monthNumber = 0;
int year = 0;
try{
System.out.println("Enter the month number (Must be between 1-12)");
monthNumber = Integer.parseInt(reader.readLine());
if(monthNumber > 12 || monthNumber < 1){
System.out.println("month number should be from 1 to 12");
}
System.out.println("Enter the year (Must be between 1-2018)");
year = Integer.parseInt(reader.readLine());
if(year > 2018 || year < 1){
System.out.println("year should be from 1 to 2018");
}
}catch (NumberFormatException e) {
System.out.println("enter an integer value and try again");
return;
}
GregorianCalendar calendar = new GregorianCalendar(year, (monthNumber -1), 1);
String isLeapYear = "";
if(calendar.isLeapYear(year)){
isLeapYear = "(Leap Year)";
}
int numberOfDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
String monthStr = new DateFormatSymbols().getMonths()[monthNumber-1];
System.out.println("Number of days in "+monthStr+" "+year+" "+isLeapYear+": "+numberOfDays);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.