Fall 2012, The College of Saint Rose

Lab 5: What Day of the Week Is That?
Due: 11:59 PM, Tuesday, October 9, 2012

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:

1. 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.
2. 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")
3. 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.

Question 1: What day of the week was June 28, 1920, the founding day for the College of Saint Rose?

Question 2: What day of the week was February 29, 1932, the birth date of mathematician Gene Golub?

Question 3: What day of the week was Y2K day, January 1, 2000?

Question 4: What day of the week will it be on January 19, 2038, the day that the number of seconds since January 1, 1970, will exceed 232?

Question 5: What day of the week will it be on July 28, 2061, the predicted perihelion of the next visit by Halley's Comet?

Question 6: What is your birthday? What day of the week was that?

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 JOptionPanes.

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.

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)

Question 7: Write a DecimalFormat constructor that could be used to format a baseball batting average. By convention, baseball batting averages, which must be in the range 0-1, are wrtitten with three decimal places to the right, and only have a digit before the decimal point when the batting average is 1.000. Valid formats would include .000, .333, .500, and 1.000.

Question 8: Write a DecimalFormat constructor that could be used to format a baseball earned average. By convention, baseball earned run averages, which can be any nonnegative value, are wrtitten with two decimal places to the right, and always have at least one digit before the decimal point, even when the value is less than 1. Valid format would include 0.76, 1.00, 3.21, 18.00, and 124.23.

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.

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).

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