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

I\'m pretty stuck in this java project January 1987 Sun Mon Tue Wed Thu Fri Sat

ID: 672242 • Letter: I

Question

I'm pretty stuck in this java project

January 1987

Sun Mon Tue Wed Thu Fri Sat

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

The program must:

1. Prompt the user for a specific month with a meaningful message.

2. If the input value is a valid date (i.e. 3 characters designating the desired month (i.e. JAN, FEB, ..., in any combination of lower and upper case letters, followed by a space and an integer between 1900 and 2099), then the calendar for the given date must be displayed

2.1. If the input value is not a valid date, the user must be informed that date was not acceptable, and then be prompted again for another date. If more than three consecutive erroneous dates are given, the program must be terminated with an appropriate error message.

2.2. After displaying the calendar, the program must ask whether or not the user wants to enter another date (Do you want to continue?)

2.3. The user must answer either yes, no, y or n, in any combination of lower and upper case letters

2.4. If an invalid answer is given, the user must be informed that the answer was not acceptable, and then be prompted again for another answer. If more than three consecutive erroneous answers are given, the program must be terminated with an appropriate error message

2.5. If the answer in step 2.2, is Yes (in any allowed form) the program must continue from step 1. If the answer is No (in any allowed form) the program must terminate with a message informing that the program is terminated normally.

3. All input must be done using an Scanner object i.e.

Scanner kb = new Scanner (System.in);

4. All output should be done using System.out.print or System.out.println

Explanation / Answer

This is a method to find the day of the week for any date between Julius Caesar and the apocalypse, or at least until someone changes the Western calendrical system. You can also use this system, for example, to find the next Friday the 13th. I learnt it by heart years ago, and I never needed to use a calendar since that time.

You need to compute the sum of 4 numbers: one for the century, one for the year in the century, one for the month, and one for the day in the month.

All these numbers are defined modulo 7. For example, 5 is equivalent to 12 and to -2.

Step 1: the Century Number

The Century Number is not really a century number, since it considers that 1900 belongs to the 20th century, that 2000 belongs to the 21st century, and so on.
The computation mode for the Century Number is different in the Gregorian calendar and the Julian Calendar. The Gregorian calendar was introduced by Pope Gregory XIII in the year 1582, but it was adopted by other countries later. For example, the British adopted it only in September 1752, which you can check by typing cal 9 1752 under Unix.

Step 2: the Year Number

For the Year Number, the easiest way is to start from years which Year Number is 0. Then walk to your year, and add 1 for each non-leap year you encounter, and 2 for each leap year.
The following years have a Year Number equal to (hint: find the pattern if you want to learn this list by heart):

Year 1399 has a Year Number equal to 5 = 99 - 94 + 1 (because there is one leap year, 96, in the interval)
Year 1956 has a Year Number equal to 2 = 56 - 55 + 1 (because 56 is a leap year)
Year 44 BC has a Year Number equal to 3 because 44 BC is equivalent to 57 in 1st century BC, and 57 - 55 + 1 = 3.

Step 3: The Month Number

This is the Month Number for each month:

Step 4: the Day Number

Congratulations, this is the end. The Day Number is simply, well, the day number in the month: 17 if this is November 17th, for instance.

Putting it all together

The sum of the previous numbers, modulo 7, gives the day of the week. means Sunday, 1 means Monday, and so on.

A few examples:

Another example: how many Friday the 13ths are there in year 2000?
Solution: Friday is 5. Subtract the day number 13, the year number 0 and the century number 1 from 5. The result is:

The corresponding month is October (not January because 2000 is a leap year.)

An increasingly important survival skill in the modern world is the ability to mentally calculate on which day of the week a particular date falls. Most of us have at some time been hospitalised, caused the death of loved ones or suffered the embarrassment of public nudity due to our inability to perform this simple task. But anyway, enough about me: let us consider how best for you to avoid a similar fate.

The technique described below is due to the famous mathematician John Horton Conway, who refers to it as the Doomsday algorithm. Conway delights in asking new acquaintances their birthdates and almost instantaneously telling them on which day of the week they were born; he claims (perhaps not unreasonably) to be the fastest human date-calculator in the world.

Firstly, a little memorisation is an essential investment for the budding Doomsday algorithmist. The heart of the technique is a list of what Conway calls "Doomsdays"--one date in each month--which conveniently all fall on the same day of the week each year. (For example, in 2000 each Doomsday was a Tuesday.) The dates are as follows:

January 31st (or 32nd in a leap year--this apparent absurdity is explained below)
February 28th (or 29th in a leap year)
March 0th (see below for explanation)
April 4th
May 9th
June 6th
July 11th
August 8th
September 5th
October 10th
November 7th
December 12th

Do not give up in disgust at this point. With a couple of tricks, this list is very easy to memorise. For example, note that for all the even months except February, the month number equals the day number: 4/4, 6/6, 8/8, 10/10, 12/12. As for May, July, September and November, Conway recommends using the mnemonic "I work from 9 to 5 at the 7-11" which yields the dates 9/5, 5/9, 7/11 and 11/7. Next, remember that the last day of February is always a Doomsday--whether or not this happens to be the 28th or 29th. Since this is one day before March 1st, we list it again as "March 0th", a somewhat redundant entry but a convenient mental device when calculating dates in March. A similar illegal date is given to the January Doomsday in leap-years: "January 32nd" obviously refers to the day after January 31st, even though this is not its proper name.

Having memorised this list, you're already able to calculate any date in 2000, since we know that Doomsday 2000 is Tuesday. For example, the 4th of July in 2000 is seven days before the Doomsday (Tuesday) July 11, and is therefore also a Tuesday. January 30th 2000 comes two days before a Doomsday (2000 was a leap year so "January 32nd" is a Doomsday) and is therefore a Sunday.

The remainder of the technique involves calculating the Doomsday for any given year. The procedure is very simple and with practice will take a matter of seconds:

First, what century are we talking about? For the 2000s, as we have seen, the first Doomsday of the century is a Tuesday. In 1900, it was a Wednesday. In practice this is all you'll need to remember--everyday usage rarely requires calculation of dates outside 1900-2099. (You can easily enough find the Doomsday for other centuries yourself if necessary.)

Next comes the only bit of actual mathematics involved. Take the last two digits of the year you're working with, and do the following:

Count the number of dozens in this number (i.e., divide by 12)
Add on the remainder left over when dividing by 12
Add on the number of 4s in this remainder (i.e., divide it by 4)

For example, if we're talking about 1995, then the last two digits are 95, into which twelve divides 7 times with a remainder of 11, into which four divides 2 times. Thus the number calculated is 7+11+2=20.

This number you've just found represents the offset between that year's Doomsday and the turn-of-the-century Doomsday. In the example above, we've found that Doomsday 1995 is 20 days later than Doomsday 1900 (which was Wednesday). For our purposes multiples of seven obviously don't make any difference, so we can throw away 14 days and simply say that it's 6 days later than Wednesday, i.e., Tuesday. What this means is that in 1995, all of the dates January 31, February 28, April 4, etc., fell on Tuesdays. This makes it easy to find any other date via simple arithmetic.

There's nothing more you need to know. Practice by finding on what day of the week you were born. Here's one example to clarify: let's say you were born on October 7th, 1968. Remember that the Doomsday for 1900 was Wednesday, and we need to find by how much 1968 differs from that. Twelve divides 5 times into 68, with remainder 8. Four goes twice into this remainder. Add the three results: 5+8+2=15, which (throwing away a couple of multiples of seven, i.e., 14 days) is an offset of 1 day from 1900's Doomsday. So Doomsday 1968 is Thursday, which in particular means the 10th of October was a Thursday. Therefore the 7th of October, coming three days earlier, was a Monday.

I like it! 3 C!s