Write a program Days.java that performs the following, Define a method called ge
ID: 3802490 • Letter: W
Question
Write a program Days.java that performs the following, Define a method called getDays which takes three integer parameters, month, day and year of a date, calculate and return the day number (total number of days since Jan 1^st) of the year. (Should consider leap year, make use of loops and do not use built-in Calendar class) Take user input of month, day and year of a date, call getDays method to print the date with full month name and day number of the year. See sample output. Sample output1: Enter month: 2 Enter day: 1 Enter year: 2017 February 1, 2017 is day 32 of 2017. Sample output2: Enter month: 3 Enter day: 2 Enter year: 2016 March 2, 2016 is day 62 of 2016.Explanation / Answer
Days.java
import java.util.Scanner;
public class Days {
public static void main(String[] args) {
// Declaring variables
int month, year, no_of_days;
int day;
//String array of Month names
String months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
// Scanner class object is used read the inputs entered by the user
Scanner sc = new Scanner(System.in);
/*
* This loop continue to execute until user enters a valid month number
* between 1 and 12 inclusive
*/
while(true)
{
// Getting the month number entered by the user
System.out.print("Enter Month :");
month = sc.nextInt();
if(month<1 || month>12)
{
System.out.println("** Invalid.Month must be between 1 and 12 (Inclusive) **");
continue;
}
else
break;
}
System.out.print("Enter the day :");
day=sc.nextInt();
while(true)
{
// Getting the month number entered by the user
System.out.print("Enter Year :");
year = sc.nextInt();
if(year<0)
{
System.out.println("** Invalid.Year must be greater than zero **");
continue;
}
else
break;
}
// Calling the method by passing the month,year as arguments
no_of_days = getDays(day,month, year);
// Displaying the number of days in that month of the year
System.out.println(months[month-1]+" "+day+","+year+" is day "+no_of_days+" of "+year);
}
// This method will finds the no of day in the month based on year number
// also
private static int getDays(int day,int month, int year) {
int val=0;
int addMonthsDays=0;
// Declaring an array and initializing corresponding days in the month
int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/* Checking the user entered year is leap or not
* if yes,if block of code will be executed
* if not ,else block of code will get executed
*/
if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
{
addMonthsDays++;
for(int i=0;i<month-1;i++)
{
addMonthsDays+=months[i];
}
} else {
for(int i=0;i<month-1;i++)
{
addMonthsDays+=months[i];
}
}
return addMonthsDays+day;
}
}
___________________
Output#1:
Enter Month :2
Enter the day :1
Enter Year :2017
February 1,2017 is day 32 of 2017
_______________
Output#2:
Enter Month :3
Enter the day :2
Enter Year :2016
March 2,2016 is day 62 of 2016
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.