Project 4: Birthday

posted by: Mr. Bergquist 9 November 2011 No Comment

PDF Version of Project 4: Birthday details below to print out (links don’t work)

This interactive program focuses on if/else statements, return, Scanner, and computing a cumulative sum. Turn in a Java file named

The program prompts for today’s date and the user’s birthday. Each prompt lists the range of values from which to choose. Notice that the range of days printed is based upon the number of days in the month the user typed. The program prints the “absolute day of the year” for today and the birthday. This is a date’s place within the year from 1 to 365 (assume that it is NOT a leap year). January 1st is absolute day #1 and December 31st is #365. Lastly the program prints the number of days until the user’s next birthday. Different messages appear if the birthday is today or tomorrow. The following are four runs of your program and their expected output (user input is bold and underlined):

This program tells you how many days
it will be until your next birthday.

Please enter today's date:
What is the month (1-12)? 7
What is the day   (1-31)? 24
7/24 is day #205 of 365.

Please enter your birthday:
What is the month (1-12)? 11
What is the day   (1-30)? 6
11/6 is day #310 of 365.

Your next birthday is in 105 days.

<< your birthday fact >>

At the end (regardless of what input the user typed), print a fun fact of your choice about your own birthday. Search the web for fun facts about your date of birth. For example, if yours is July 11, you could print:

Did you know July 11, 2010 Spain won the 2010 FIFA World Cup?
It was the first time Spain won the World Cup and became the eighth team to ever win a World Cup.

Since this is an interactive program, it behaves differently when given different input. The examples above do not show all possible cases. Please look at all these example outputs and do your own testing:
Output #1 / Output #2 / Output #3 / Output #4 / Output #5 / Output #6 / Output #7 / Output #8 / Output #9 / Output #10

Grade-It will be picky about output matching exactly, including spacing; use quick diff to be sure.

You may assume that all user input is valid; that when prompted for a date, the user will enter an integer in a valid range. You must also assume that your program is not run in a leap year, so February always has 28 days.

Implementation details:

The 365 days of the year are divided into twelve months as follows:

See lecture notes for a method daysInMonth that when passed a month parameter returns the number of days in it.

Absolute day of the year:

One major task in this program is computing the absolute day of the year on which each given date falls. Jan 1 is absolute day #1. Jan 2 is #2. Jan 31 is #31. Feb 1 is #32. And so on, up to Dec 31, which is #365.

You must compute each absolute day using a cumulative sum loop as shown on the class slides. A given date’s absolute day is the sum of all prior months, plus the days within the current month. For example:

Please NOTE: Part of your grade will come from decomposing your solution into a coherent set of methods. You should, for example, write a parameterized method that computes the absolute day for a given date.

Days until next birthday:

Another task is computing how many days until a person’s next birthday. There are two cases to consider:

1) When the birthday falls after today’s date: This simpler case means that the next birthday is within the current year. In the example below, “today” is 7/24, and the birthday is 11/23. There’s a typo and it should read 122 days to birthday but I’m tired right now so just look at the picture because it’s still useful.

2) When the birthday falls before today’s date: In this case you need to “wrap around,” counting the days from today to the end of the year, and then from the start of next year to the user’s birthday. In the second example output, “today” is 9/22 and the user’s birthday is 3/17:

It may help you to use the number 365, the number of total days in a year, in your wrapping computations.

Development Strategy and Hints:

On this program, many students encounter “cannot find symbol” compiler errors related to scope, parameters, and return values. Common mistakes include forgetting to pass or return a needed value, forgetting to store a returned value into an appropriate variable, and referring to a variable by the wrong name. You should write your code incrementally, repeatedly making small improvements and testing it. You might want to start by writing code to compute the absolute day of the year as described previously. Test this code by running it with several fixed values and debug it until it works, then move on to the rest of the program.

Computing the number of days until the user’s birthday can be challenging. But if you have other parts of the program finished, you can base this computation on those parts. Don’t forget to take advantage of the methods you have already written. Methods can call other methods to help perform their overall task.

For reference, my solution to this assignment (excluding the “birthday fact” code) occupies 90 lines including comments (53 “substantive”), and has 6 methods other than main, though you do not need to match these amounts exactly.

Style Guidelines:

Many of the style points on this assignment are related to how well you procedurally decompose the problem into a set of well-chosen static methods. To receive full credit, you must have at least 4 non-trivial methods in your program besides main. Use methods for structure and to reduce redundancy. You should also utilize parameters and return values appropriately to pass information throughout your code.

Each method should perform a single clear and coherent task. No one method should do too large a share of the overall work. As a very rough estimate, a method whose body (excluding the header and closing brace) has more than 15-20 lines is likely too large and may lose points.

In this assignment it is okay to have a few println statements in the main method, though you should limit this as much as possible. The contents of main should be a concise summary of the overall program; the majority of your program’s behavior should come from its other methods. You should avoid “chaining” long sequences of method calls together without returning to main.

Your code will involve conditional execution with if and if/else statements. Part of your grade will come from using these statements appropriately. You may want to review nested if/else statements.

For this assignment you are limited to language features we have covered in class. You are especially forbidden from using Java’s built-in date libraries such as Date or GregorianCalendar.

Give meaningful names to methods and variables, and use proper indentation and whitespace. Avoid long lines over 100 characters in length. Follow Java’s naming standards as specified in our coding convention. Localize variables when possible; declare them in the smallest scope needed. Include meaningful comment headers at the top of your program and at the start of each method, and it is good to add short comments for any “tricky” parts of your methods for the reader.

[Big Thanks again to UW for sharing assignments with us and Ms Martin for identifying this intriguing project]

1 Star2 Stars3 Stars4 Stars5 Stars (20 votes, average: 3.45 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>