laD a program that determines the d Writeoplex algorithm that takes into account
ID: 3929815 • Letter: L
Question
laD a program that determines the d Writeoplex algorithm that takes into account the special leap year rul output is ay of the week for a given date. You can invent your own complex al our oin calendars, but this is a case where it makes sense to look for things t 8 Write a program t nges in calend chanr Who else might need to compute values from dates over a wide span o ear rules, and dars, but this is a case where it makes sense to look for things that ar er a wide span of time? Historians rians work with dates, but generally don't compute from them. Astronomers, Histove, need to know the difference in time between orbital events in the solar system that span h nan hundreds of years. Consulting an astronomy text, you will find that there is a srandard way of representing a date, called the Julian Day Number (JDN). This value ahe number of days that have elapsed since January 1,4713 B.c. Given the JDN for date, there is a simple formula that tells the day of the week: DayOfWeek = (JDN + 1) % 7 The result is in the range of 0 to 6, with 0 representing Sunday The only remaining problem is how to compute the JDN, which is not so simple. The algorithm computes several intermediate results that are added together to give the IDN. We look at the computation of each of these three intermediate values in turn. If the date comes from the Gregorian calendar (later than October 15, 1582), then compute intRes1 with the following formula; otherwise, let intResi be compute intRes1 with the following formula; otherwise, let intRes1 be zero intRes1 2 year 100 + year 400 (integer division) The second intermediate result is computed as follows: intRes2 = staticcast(365 . 25 * Year) - compute the third intermediate value with this formula: intRes3 = static_castExplanation / Answer
This Program computes exatcly the Julian day of entered Date,Month,Year
import java.util.*;
public class CalculateJDN
{
public static void main(String[] args)
{
long JDN;
double years,days;
int intRes1=0,startYear=4713,month,date;//Taking start year as 4713
System.out.println("Enter the date :"); // taking input for date
Scanner sc = new Scanner(System.in);
date = sc.nextInt();
System.out.println("Enter the month:");
month=sc.nextInt(); // taking input for month
System.out.println("Enter the year: "):
year=sc.nextInt(); // taking input for year
JDN = getNumdays(year); // storing result in JDN variable
System.out.println("The Day of this julian date is : ");
int intRes1 = (JDN+1)%7; // finally calculating the day
if(intRes1 == 0)
System.out.println("Sunday");
else if(intRes1 == 1)
System.out.println("Monday");
else if(intRes1 == 2)
System.out.println("Tuesday");
else if(intRes1 == 3)
System.out.println("Wednesday");
else if(intRes1 == 4)
System.out.println("Thursday");
else if(intRes1 == 5)
System.out.println("Friday");
else
System.out.println("Saturday"):
}
int getnumdaysofMonth(int month) // This method returns days of months
{
int numdays=0;
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month ==10 || month == 12) // Calculating no of //days for the month if it has 30 or 31.
numdays=31;
else if(month == 2)
numdays == 28+getleapyear(year);
else
numdays = 30;
return numdays;
}
int getcurrentdays(int month)
{
int numdays=0;
for(int i=1;i<month;i++)
{
numdays = numdays+getnumdaysofMonth(i); //Getting number of days of current till current date in the year
}
numdays=numdays+date;
}
int getleapyear(int year) // validating whether the entered year is leap year or not.
{
if(year % 400 == 0 ) // If year is divisible by 400 then it is a leap year
return 1;
else if(year%4 == 0 && year % 100 ==0) // If the year is divisible by both 4 and 100 then it is a leap year
return 1;
else return 0;
}
long getNumDays(int year) // calculating total number of days till date.
{
long numdays=0;
for(int i=startYear;i>=0;i--)
{
numdays = numdays +365;
}
for(int i=1;i<=year;i++)
{
numdays=numdays+365;
if(i>1582) // The year from georgian calendar will have the leap year count
{
numdays=numdays+365+getleapyear(i);
}
else if(i == year)
numdays = numdays + getcurrentdays(month);
return numdays;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.