Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Command line arguments: A frequent need in many computer applications is to be able to compute the day of the week that a Gregorian calendar

Command line arguments:

A frequent need in many computer applications is to be able to compute the day of the week that a Gregorian calendar date falls on, given a month, day, and year. Most programming languages include functions to perform these calculations already, but you will write the calculations yourself to see how basic arithmetic and decisions are performed in Java. You will implement a computation called Zeller's congruence, developed by German mathematician Christian Zeller (18221899), which can compute the day of the week given a month, day of the month, and year.

Create a new class called PerpetualCalendar.

This program should take exactly three command-line arguments: one representing the month (1 = January, 2 = February, etc.), one representing the day of the month, and one representing the (four-digit) year, in that order. Declare variables to hold these values and then convert the values that have been read from the command-line to integers. Zeller realized that the February 29th leap day would cause problems in his calculations since it is sandwiched in the middle of the calendar year. To get around this, he devised a clever trick: instead of treating January and February as the 1st and 2nd months of the year, he would treat them as the 13th and 14th months of the previous year. This would make February 29th, if it occurred, the final day of his calendar year! So, we need to modify our month and year variables. If the month is January, set the month to be 13 and subtract one from the year. If the month is February, set the month to be 14 and subtract one from the year. In all other cases, we want to leave the month and year as they are.

Now it's time to compute the first part of Zeller's congruence. The first part of the formula is:

Z=d+((26(m+1))/10)+y+y/4+6(y/100)+y/400

where m is the month (after the adjustment above), d is the day of the month, y is the year (after the adjustment above), and Z is the result so far. The half-brackets around parts of the formula mean "floor", or in other words, round down. Since integer arithmetic does this by default in Java, this is one place where we can take advantage of that fact instead of having it cause us problems. But remember: x * (y / z) is not the same as x * y / z when you're doing integer arithmetic (because the rounding happens at different times), so use parentheses where necessary to ensure the correct order!

Declare a variable to hold this result, then convert the formula into a Java expression and assign it to your variable.

Now that we have the first part of Zeller's congruence computed, we can use this value to determine the day of the week. Declare an integer variable to hold the day of the week, and then compute its value in the following way: add 5 to the result Z above, compute the remainder of this divided by 7, and then add 1.

Your day of the week variable will now hold a value between 1 and 7, where 1 corresponds to Monday, 2 to Tuesday, 3 to Wednesday, and so on, up to 7 for Sunday.

Now we want to print a message telling the user the day of the week that the date falls on. We don't want to just print the number thoughwe want to print the name. For this part, use the enumeration that you wrote previously. Iterate over the values of the enumeration (use the values() method), find the enum value that has an associated integer field for the day of the week that matches the value of the day of the week variable that you computed in the previous step, and print the day of the week.

Compile and run your program from command-line. Try it with this date: November 4, 2016 is a Friday. But don't stop therepull up a calendar and try some other dates. You should try some "edge cases" too, such as leap days. Here are a few examples: J

anuary 29, 2000 Saturday

July 4, 1776 Thursday September 9, 2009

February 29, 2008 Friday

side note: //This is question from a java class I am taking

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

What must a creditor do to become a secured party?

Answered: 1 week ago

Question

When should the last word in a title be capitalized?

Answered: 1 week ago

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago