ulrail.gif

Homework 1

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

Purpose

A computer program represents a useful computation (or, to be precise, a group of possible computations) in a human-readable form. The computer, so programmed, is valuable in its ability to automate useful computations at high speed. One important class of programs are those that accept input values from the person using the computer (colloquially “the user”), that compute a useful result, and that then present the result to the user. The steps in such a computation are thus (1) get input values, (2) compute result, (3) output result.

In this homework assignment, you will write a computer program representing a simple but useful computation in this style. You can download the skeleton code here.

The Problem

Given a specific date, what is the day of the week?  Computers keep track of the current date and time, but often need to compute the day of the week.  For example, in Microsoft Windows, when you hover over the time (in the lower-right of the screen), the day of the week is displayed, as seen in the image to the right.

The method we are using is called Zeller's congruence.  We have modified the formula shown at that link -- more on this below.  Note that this algorithm only works for dates after October 15, 1582 -- before that, a different calendar system was used (the old one was the Julian calendar; the new one was the Gregorian calendar).  The difference between the two calendars is the computation of leap days.

Note that there are other means of computing the day of the week (such as the Doomsday rule), but we are using Zeller's congruence for this homework.

Algorithm

The program you will write will compute the day of the week for a given date. The original formula is from here; we have modified it a bit, to make it easier for this homework.  Thus, the formula we are using is:

Where:

  • day is the day of the month (1, 2, 3, etc.)
  • month is the month of the year (see note below!)
  • centruy is the century ().  For 2007, the century would be 20.
  • yearpart is the year within the century ().  For 1994, the century would be 94.

A number of things will make this much easier to compute.  We are only dealing with ints here, so all of the division with floor operators that is needed (for example, ) is just simple division -- remember that Java, when given two ints, will automatically perform integer division (i.e. the floor operation).  And the % operator is the mod function.

The program should ask for three values: year, month, and day (IN THAT ORDER!).  Note that your program is going to be automatically run by another program.  So if you ask for more input, or those inputs in a different order, you will lose points.

The month entered is a bit tricky.  Due to the way the formula works, January is entered as month number 13 of the previous year.  Likewise, February is entered as month 14 of the previous year.  So 2 February 2007 (the due date of this assignment) would need to be entered as year 2006, month 14, and day 2.  There are ways to avoid requiring the user to enter January as month 13 and February as month 14, but we haven't seen them in the course yet.

Sample execution

To help you understand what is required for this homework, we have included an execution run of this program.  Your program needs to print out similar information, but the format does not have to be the same. However, the values computed should be the same. Note that the text in red is what was input by the user.

Welcome to the weekday calculator!

Note that January should be entered as month 13 of the previous year, and
February as month 14 of the previous year.  For example, to enter
2 February 2007 (the due date of this assignment), it's year 2006, month 14,
and day 2

Please enter the 4-digit year:
2006
Please enter the month as a number (Mar is 3, Dec is 12, etc.):
14
Please enter the day of the month:
2

You entered the date (in m/d/y format): 14/2/2006

Weekdays are numbered from 0 and start from Saturday.
So Saturday is 0, Sunday is 1, Monday is 2, Friday is 6, etc.

The date 14/2/2006 is day 6

To help you test your program, here are a few great dates in history, and their respective days of the week:

Program Design

One of the key problems that software developers face is to determine what sequences of operations are needed to solve a specified problem. In this case, we’re going to get you off to a good start by giving you a sequence of steps that work. Your primary task is to refine this program by converting each of these high-level steps into an appropriate statement or sequence of statements in the Java language. In the software development field, we call such a task step-wise refinement -- meaning that we take program steps expressed in a high-level, easy-to-understand form (such as the 5 steps below that are listed in English), and we refine them into one or more steps in a lower-level, more concrete language: in this case, Java. This is an incredibly important skill for the software developer -- to be able to sketch programs at a high level before refining them into statements in a formal programming language, such as Java. The list below expresses the essence of the solution -- it's an abstract solution. Refining it into a language such as Java is relatively straightforward, once you know the language.

  1. Print an appropriate legend (see below).  For this program, it also includes the month/year explanation, as shown in the execution above (i.e. that January is month 13 of the previous year).
  2. Set up an input stream (Hint: create a new Scanner object. Only create one for your whole program!)
  3. Prompt the user for the three inputs described above, read in those input values and store them in appropriately named variables.  Make sure you read them in in the appropriate order: year, month, day!
  4. Compute the desired results.
  5. Print out the computed results with an explanatory message. The output should appear something like what is shown above.

Remember that if you want to print out multiple things with a System.out.println() statement, you need to separate them with a + (a plus sign).  Feel free to swing by any office hours if you are unsure about any part of this program. When asking for input, please only ask for the required 3 values in the specified order (year, month, day). We will test your program automatically, so if you ask for more input values, your program will not execute correctly for the graders.

While we have provided most of the design for this program above, you need to supply some details -- the variables needed to store inputs and the result, and how to express all of these ideas in the Java programming language, etc.

How and where to start

We have provided a skeleton Weekday.java file for you to start with. Please don't change the class name -- doing so will cause it not to compile correctly and result in a loss of lots of points.

The best way to proceed is to go through the five steps described above, testing the program after each step or two. This process is called incremental development. If you take a small step and it doesn’t work, it’s easy to step back and do it again. If you take lots of steps and end up with something that doesn’t work, you can easily get completely lost. Take small steps then test before proceeding! For example, after you write the code to read in the input from the user, you should print those values out to the screen, so that you can be sure that your input routines work. And after you make some partial calculations (such as the century and the year in the century), print those out to the screen to test the program. You are welcome to try to write the whole program and debug it all at once – however, this will very likely take you more time than developing and testing it incrementally. Of course, if you have lots of print statements in there, you should probably remove them when the program is working properly, so that the output looks similar to what is shown above. Lastly, be sure you fully understand how to do the formula before you try to program that in Java.

Lastly, you may want to look at the BMICalculator.java program (discussed in lecture). This program, while it computes something different, uses a lot of the same steps described above.

Help!  My program is not computing the correct results!

When developing this homework, we realized that a lot of people would be running into the problem of the program outputting a value, but not the correct value.  Here are a number of things to check:

  • Are you computing the century correctly?
  • Are you computing the year in the century correctly?
  • Did you implement the formula properly?
  • When doing a date in January and February, did you add 12 to the month AND subtract 1 from the year?

As always, feel free to swing by any of the office hours to ask for help

Good programming practices

The following are considered good programming practices. All homeworks must include these whenever possible. Not including them will receive points off from the grade. The BMICalculator.java program is an example of a program that follows most of these practices (all but the test code part).

  • Header: The comments at the very beginning of the file, and need to include your name, e-mail ID, 101 section (or 101-E, if appropriate), and a line or two about what this program does.
  • Comments: A line or two before each major "block" (section of code, class, method, variable, etc.) describing what it does.  Each of the 5 steps listed above can be considered a "block" of code.
  • Whitespace: A line or so between separate elements of the code (methods, code segments, etc.) and good indentation.
  • Legend: The program should print out a line or two that states what the purpose of the program is (i.e. "This is a BMI calculator", or "Let's compute the day of the week"). This should be the first line(s) printed to the screen.
  • Variable names: All final variables should be in all caps with underscores between the words, such as BOX_WIDTH. Non-final variable names should have the firstLetterOfEachWordCaptilized in the variable name (excepting the very first letter). Variable names should be relevant and informative, such as mileage and not m.
  • Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not go past the end of the screen. You can have a println() statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator tells you the column that the cursor is currently in (at the bottom on the status bar), so you can use that to gauge how long your lines are.
  • Test code: ALL programs must include proof that you tested the program. For this program, the test is just required as comments -- enter what values you used to test your program, and what the output is. You do not need to copy-and-paste the output run. When we test your program, we will only use positive values (i.e., greater than zero) for the input values, as well as only dates after 15 October 1582. You only need to test with these types of values as well.

Grading

The following lists the criteria that the graders will be look at when the homework is graded. Note that these criteria will not be repeated on the other homeworks, only on this first one. Also, the points for each of the following criteria have not yet been determined.

  • Program header (includes name, e-mail ID, and purpose)
  • Good commenting and use of whitespace
  • Prints out an appropriate legend
  • Proper use of Scanner for the input stream
  • Properly prompts user for input
  • Properly computes the necessary values
  • Prints out the necessary values
  • Variable declaration (good names, proper initialization, etc.)
  • Test cases shown in comments

If your program does not compile, you will receive 25 points off, in addition to any other penalties. If you do not understand a compilation error, see a TA or professor (thus, you probably don't want to wait until the night before it's due to start this homework). We have lots of office hours available, please make use of them!

Submission

When you are finished, submit the Weekday.java file. You will notice that we ask more survey questions for the homeworks.

 

spacer.gif
spacer.gif footer-middle.gif spacer.gif