Day of the Week (Due 04 July 2014)
In this program you will prompt the user to enter the year, month, and
day. Your program will print out the day of the week for that date.
Here is a sample output for the program:
Enter year: 1960
Enter month: 12
Enter day: 12
That day was Monday.
Your program will check the following:
- The year is between 1900 and 2100.
- The month is in the range 1 and 12.
- The day is in the range appropriate for that month. For months
other than February, the range of days will be from 1 to 30 or
31 days. The range of days for February is between 1 and 28 or 29
depending on whether the year is a leap year or not.
All of the ranges are inclusive. You will first prompt the user to
enter the year and then check whether the year is in the range specified.
If it is not, then you will keep prompting the user to enter the correct
year. Use a do-while loop in this context. After that you will
prompt the user to enter the month and run a similar check. Lastly,
you will prompt the user to enter the day and run a check. For example,
the input 29, 2, 2012 is valid, but the input 29, 2, 1900)
is not, because 2012 is a leap year and 1900 is not a leap year.
This algorithm was developed by Rev. Zeller. Let us define the quantities
a, b, c, and d as follows:
- a ≡ the month of the year, with March = 1, April = 2, …
with January = 11, and February = 12 of the preceding year
- b ≡ the day of the month
- c ≡ the year of the century
- d ≡ the century, though not in the conventional sense. For
example, after making adjustments for the months of January and February,
for all years like 1910, 1949, 1984, the value of d = 19. And for years
like 2003, 2010, 2025, the value of d = 20.
Important: In our calendar, the year begins in January and ends in
December. In the calendar, used in the algorithm, the year begins in
March and ends in February. Your program should internally make the
adjustment and not expect the user to know this. For example, if in our
calendar we have January 2009 (month = 1 and year = 2009), the program
will make the adjustment so that month = 11 and year = 2008. If you do
not make the adjustment you will not get the right result.
For example, 31 July 1929, gives a = 5, b = 31, c = 29, and d = 19. Similarly,
3 January 1988, gives a = 11, b = 3, c = 87, and d = 19.
Now compute the following quantities:
- w = (13 * a - 1 ) / 5
- x = c / 4
- y = d / 4
- z = w + x + y + b + c - 2 * d
- r = z % 7
- r = (r + 7) % 7 [to care of negative values of r]
r gives the day of the week. r = 0 represents Sunday, r = 1
represents Monday, and so on.
The class that you will be writing will be called Day. We will be
looking at good documentation, design, and adherence to the coding convention
mentioned below. You may use the same variable names used in the problem
statement or come up with your own. You must use Scanner for your
input. If the day is in the future the tense of the verb should be changed
accordingly. Your file Day.java will have the following header:
/*
File: Day.java
Description:
Student Name:
Student UT EID:
Course Name: CS 312
Unique Number:
Date Created:
Date Last Modified:
*/
There is a modification that I would like to make to the standard
Java coding conventions. Please align the opening and closing braces
vertically so that you can easily make out the blocks of code. For
example:
Do this:
if ( x > 5 )
{
a = b + c;
}
Not this:
if ( x > 5 ) {
a = b + c;
}
Use the turnin
program to submit your .java file.
We should receive your work by 11 PM on Friday 04 July 2014.
There will be substantial penalties if you do not adhere to the
guidelines.
- You must submit the .java file and not the .class file.
- Your .java file should have the header with the proper documentation.
- You should be submitting your .java file through the web based
turnin program. We will not accept files e-mailed to us.
- Compile and run your code on the command line.
- Your code must compile before submission.
- Here is the Grading Criteria.
References
- Formula
for the Day of the Week