Computer Science 202
Introduction to Programming
Fall 2012, The College of Saint Rose
In this lab, you will write a program to compute the day of the week of a given date between January 1, 1900, and December 31, 2099.
You are strongly encouraged, but not required, to work with a partner on this lab.
There are a few lab questions in this assignment. You should answer these in a comment at the end of your Java program.
Calculating the Day of the Week
During the period from January 1, 1900, and December 31, 2099, leap years occur every 4 years. We restrict to this range as 1900 and 2100 are not leap years and including a wider range would complicate our algorithm.
The algorithm will compute the number of days since the first day of the 20th century, modulo 7. That is, the remainder when we divide that number of days by 7. This gives us the day of the week, where 0 is Saturday, 1 is Sunday, etc. We will not compute the actual number of days, rather we will use a mathemetical formula to compute a number we can use in its place.
We will use the following adjustment table in our algorithm.
Month | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
Adjustment | 1 | 4 | 4 | 0 | 2 | 5 | 0 | 3 | 6 | 1 | 4 | 6 |
Additionally, if the year is a leap year and the date is in January or February, you must subtract 1 from the adjustment.
Our algorithm proceeds as follows:
Side note: This particular technique is due to John Conway, of Princeton University. Professor Conway answers 10 day of the week problems before gaining access to his computer. His record is at the time of this writing well under 15 seconds for 10 correctly answered questions. See "Scientist at Work: John H. Conway; At Home in the Elusive World of Mathematics," The New York Times, October 12, 1993.
Let's practice on Neil Armstrong's birthday. The sum we compute is
3 + 5 + 30 + 7 = 45
Then we compute the remainder when we divide 45 by 7, obtaining 3. This corresponds to Tuesday.
Now compute some by hand using the above, showing your work. You may
wish to do this on paper to start, but don't forget to add your
answers, including your work to obtain those answers, in a comment at
the bottom of your Java source file (after the last }
). Verify
your answers with an online calendar of your choice. These are worth
1/2 point each toward your grade for this lab.
Getting Set Up
Lab Procedure
Write a Java program in a class called DayOfWeek that solves the above problem. You may choose to develop a Visual Logic flowchart first, but you need not turn that in. For your Java program, you may use either terminal I/O with a Scanner and System.out.println calls or dialog boxes with JOptionPanes.
Here are some guidelines and suggestions for your program:
Additional Lab Questions
Here are a few more lab questions regarding output formatting, in honor of the upcoming Major League Baseball playoffs. Add these responses to the comment at the bottom of your Java program. You do not need to compile and run the Java code for these. Don't worry if you don't follow baseball - all of the information you need is contained in the questions. (1 point each)
Your responses to both of these questions should take the form:
DecimalFormat someName = new DecimalFormat(...);
where someName will be an appropriate variable name to hold the DecimalFormat object you are creating, and ... will be replaced with an appropriate format string.
Style and Documentation Reminders
Before you submit your programs, make sure they conform to our guidelines for style and documentation.
In particular, you should have a comment at the top of each class that describes your program and has your name (and that of your partner if you are working with someone), the course number and section (02 for 11:15, E1 for 4:10). You should have comments throughout your programs describing your variables and any non-obvious Java statements or groups of statements.
All identifiers (class names and variable names) should be meaningful and conform to Java's naming conventions.
Your code should be nicely formatted, with new lines after any
{
or }
, and indented as done in class examples.
Submitting Your Work
Before 11:59 PM, Tuesday, October 9, 2012, submit your Java program to Blackboard for grading. Please upload only your Java source file DayOfWeek.java - the one with the .java file extension, not the .class, .ctxt, package.bluej or README files).
Grading
This assignment is worth 35 points, which are distributed as follows:
Feature | Value | Score |
Reading inputs | 3 | |
Error checking inputs | 3 | |
Correct day of week computation | 10 | |
Appropriate output (including day of week as String) | 4 | |
Using correct filename | 1 | |
Comments | 4 | |
Naming conventions | 3 | |
Formatting | 2 | |
Lab questions | 5 | |
Bonus for better error checking based on month and year | 1 |