PurposeA 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 ProblemGiven 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. AlgorithmThe 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:
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 executionTo 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 DesignOne 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.
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 startWe 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:
As always, feel free to swing by any of the office hours to ask for help Good programming practicesThe 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).
GradingThe 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.
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! SubmissionWhen you are finished, submit the Weekday.java file. You will notice that we ask more survey questions for the homeworks.
|