Please help me ASAP. This is in Java. Days Elapsed Since January 1 Application T
ID: 3585469 • Letter: P
Question
Please help me ASAP. This is in Java.
Days Elapsed Since January 1 Application 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 entens 03 01 2012 then th is represents March 1,2012. In this case, the number of days elapsed is 61: 1 for January, 29 for February (2012 is a "leap year"), and l during March itself Similarly, if the input were 01 01 2010 then the output should indicate that 1 day has elapsed. IMPORTANT: Do not use any standard calendar-related classes from the Java library for this project. The central idea of this assignment is to implement and use our own Date class. If we were to use a library class such as "Calendar", "GregorianCalendar", or "Date", that would be cheating. In addition to returning the correct result, your program must check for and deal with the following issues: 1. You may use standard Java library classes to check if the input is or is not valid Non-numeric data. numeric data. An appropriate error message for this case would be "Non-numeric data entered: mm dd yyyy Leap years. Not all years that are evenly divisible by four are leap years. Years that end in "00" (so called centurial years) are leap years only if they are also divisible by four hundred. Inappropriate month entered. Allowable values are 01 - 12. (There is no month 13, or month 0.) The error message might be: "Invalid month: Inappropriate day entered. The day value must be between 01 and the maximum for that particular month and year. For example: January and March always have 31 days; February has either 28 or 29, depending on the year. The error message might be: "Invalid day dd, for month-mm, year-yyyy". Inappropriate year: 2. 3. mm” 4. 5. for our purposes, we will allow year values in the range of 1900- 2100 After one date string is processed, either successfully or with an error message, the program should ask the user to enter another date string. This "main loop" continues until the user enters the "g" commandExplanation / Answer
/**
* The java program that prompts user to enter
* date in the format of mm dd yyyy and check if
* date is valid or not. If valid then prints
* the number of days elapsed from 1 jan.
* */
//DaysElapsed.java
import java.util.Scanner;
public class DaysElapsed {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String input = null;
int day=0,month = 0,year=0;
int daysLeft=0;
int maxDays=0;
boolean loop=true;
boolean valid=true;
System.out.println("Enter date :dd mm yyyy");
input=scanner.nextLine();
//continues to prompt until user enters q to quit
while(loop && !input.equals("q"))
{
//split by space
String[] date=input.split(" ");
//checking if data format is valid
try
{
month=Integer.parseInt(date[0]);
year=Integer.parseInt(date[2]);
day=Integer.parseInt(date[1]);
}
catch (NumberFormatException e) {
System.out.println("Non-numeric data is entered: dd mm yyyy");
valid=false;
}
maxDays=getNumberOfDaysInMonth(year, month);
if((month<1 || month>12))
{
System.out.printf("Invalid month: %d ",month);
valid=false;
}
if((day<1 || day>maxDays))
{
System.out.printf("Invalid day: %d, for month= %d, year= %d ",day,month,year);
valid=false;
}
if((year<1900 || year>2100))
{
System.out.printf("Invalid year %d: ",year);
valid=false;
}
//calculate number of days if date is valid
if(valid)
{
for (int m = 1; m < month; m++)
daysLeft+=getNumberOfDaysInMonth(year, m);
daysLeft+=day;
System.out.println("The number of days elapased is : "+daysLeft);
loop=false;
}
valid=true;
//prompt for date
System.out.println("Enter date :dd mm yyyy");
input=scanner.nextLine();
}
}
//Method to return the number of days in month
public static int getNumberOfDaysInMonth (int y, int m)
{
if (m == 1 || m == 3 || m == 5 || m ==7
|| m == 8 || m == 10 || m == 12)
return 31;
if (m == 4 || m == 6 || m == 9 || m == 11)
return 30;
if (m == 2) return isLeapYear (y) ?29:28;
return 0;
}
//Method to check if year,y is leap or not
static boolean isLeapYear (int y)
{
return y % 400 == 0 || (y % 4 ==0 && y % 100 != 0);
}
}
------------------------------------------------------------------------------------------
Sample Output:
Enter date :dd mm yyyy
25 25 2012
Invalid month: 25
Invalid day: 25, for month= 25, year= 2012
Enter date :dd mm yyyy
03 01 2012
The number of days elapased is : 61
Enter date :dd mm yyyy
q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.