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:

- Write down the date numerically. The date consists of a month between 1 and 12, a day of the month between 1 and 31, and the number of years since 1900. For August 5, 1930, (the birth date of the late, great American space pioneer Neil Armstrong) we would represent the month by 8, the day by 5, and the year by 30. For today's date, October 2, 2012, we would represent the month by 10, the day by 2, and the year by 112.
- Compute the sum of the following quantities:
- the month adjustment from the given table (
*e.g.*, 3 for Neil Armstrong's birthday, 1 for today) - don't forget to subtract 1 if it is January or February of a leap year! - the day of the month
- the number of years since 1900 (
*e.g.*, 30 for Neil Armstrong's birthday, 112 for today) - the whole number of times 4 divides the year (
*e.g.*, 7 for Neil Armstrong's "30", and 28 for today's "112")

- the month adjustment from the given table (
- Compute the remainder of the sum of Step 2, when divided by 7. This remainder gives the day of the week, where Saturday is 0, Sunday is 1, etc.

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

- Create a folder for your work on this lab.
`Lab5`might be a good name. - Follow the usual procedure (you can see all the detailed steps
on the Lab 1 page) to create a new BlueJ project called
`Lab5Progs`. We will not create any Java classes just yet.

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 `JOptionPane`s.

Here are some guidelines and suggestions for your program:

- You must perform error checking on your input to ensure the month
entered is valid (in the range 1-12), that the day is valid (1-31),
and that the year is valid (1901-2099). Print an error message then
terminate your program with
`System.exit(1);`if you encounter invalid input. You can earn a bonus point if you further restrict the days correctly based on the year and month. - Avoid long
`if-else`blocks - use`switch`statements where appropriate. - You may use a number to read in the month, but you should print
out the day of the week as a
`String`rather than as a number.

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 |