Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Must Show Work With Explanations. This is my homework: This programming assignme

ID: 3542564 • Letter: M

Question

Must Show Work With Explanations.


This is my homework:

This programming assignment involves writing a Java application which inputs a date from the user, and calculates how many days have elapsed from January 1 of that year to the date specified. The user enters the date from the console, as a string of characters in the format: MM DD YYYY. For example, if the user enters
03 01 2012
then this represents March 1, 2012. In this case, the number of days elapsed is 61:
31 for January,
29 for February (2012 is a

Explanation / Answer

import java.util.*;

class date_elapser
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter Date in this format MM DD YYYY");
int month = in.nextInt();
int day = in.nextInt();
int year = in.nextInt();
int[] month_array = {31,28,31,30,31,30,31,31,30,31,30,31};
int day_elapsed = day;
for(int i=0; i<month-1; i++)
{
if(i==1 && (year%400 ==0 || (year%100 != 0 && year%4 == 0)))
day_elapsed++;
day_elapsed = day_elapsed + month_array[i];
}
System.out.println("the number of days elapsed is " + day_elapsed);
}
}
}