CSE142—Computer Programming I
Programming Assignment #8 (12 points)
Due: Tuesday, 5/26/09, 9 pm
This assignment focuses on classes and objects. Turn in two files named Birthday.java and Date.java. You will also need the support file Date.class from the course web site; place it in the same folder as your program.
The assignment has two parts: a client program that uses Date objects, and a Date class of your own whose objects represent calendar dates. It is a little shorter than recent assignments and is worth fewer points.
The Date class defines objects with the following methods and constructor. In Part A, you will use these features as a client. In Part B you will implement this class. In the implementation, you will need some fields.
·
public
Date(int month, int day)
Constructs a new Date
representing the given month and day.
Assumes that the parameter values passed are valid (month between 1 and
12, day appropriate for the month).
·
public
int getMonth()
Returns a Date object's
month of the year, between 1 and 12. For
example, if the Date object represents August 17, this method should return 8.
·
public
int getDay()
Returns a Date object's day
of the month, between 1 and the number of days in that month (which will be
between 28 and 31). For example, if the
Date object represents August 17, this method should return 17.
·
public
String toString()
Returns a String
representation of a Date
in a month/day format. The day
should always be shown as two digits; if the day is between 1 and 9, it should
be preceded by a 0. For example, if the Date object represents
May 24, it returns "5/24". If this Date
object represents December 3, it returns "12/03". Note that this method returns the string; it does not print any output.
·
public
int daysInMonth()
Returns the number of days in the month represented by a Date object. The following table lists the number of days
in each of the twelve months of the year:
1 2 3 4 5 6 7 8 9 10 11 12
Name Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Days 31 28 31 30 31 30 31 31 30 31 30 31
For example, if the Date object represents
August 17, this method should return 31.
If the Date
object represents February 14, it should return 28. You do not need to worry about leap years.
·
public
void nextDay()
Modifies the state of a Date
object by advancing it 1 day in time.
For example, if the Date
object represents September 19, a call to this method should modify the Date object's state so
that it represents September 20. Note
that depending on the date, a call to this method might advance the Date object into the next
month or year. For example, the next day
after August 17 is August 18; the next day after February 28 is March 1; and
the next day after December 31 is January 1.
In Part A you will write a client program that uses an existing Date class provided to you. The purpose of Part A is to practice creating and using Date objects from a client's perspective and to give you an appreciation for the usefulness of Date objects.
First prompt the user for today's date and for his/her birthday, each as a month-day pair. Use this information to print the number of days in the month the user was born, and the number of days from today to the user's birthday. If the user's birthday is today, print a Happy Birthday message.
Below are several example logs of execution from the program; user input is bold and underlined. Your output should match these examples exactly.
What is today's date (month and day)? 11 4 What is your birthday (month and day)? 9 9 There are 30 days in month #9 Your birthday 9/09 is in 309 days. << your birthday fact >> |
What is today's date (month and day)? 12 15 What is your birthday (month and day)? 12 15 There are 31 days in month #12 Happy birthday! << your birthday fact >> |
What is today's date (month and day)? 8 19 What is your birthday (month and day)? 11 30 There are 30 days in month #11 Your birthday 11/30 is in 103 days. << your birthday fact >> |
What is today's date (month and day)? 10 2 What is your birthday (month and day)? 10 1 There are 31 days in month #10 Your birthday 10/01 is in 364 days. << your birthday fact >> |
The end of the program's output prints a "fun fact" about your own birthday. This can be any message you like, as long is it is nonempty, of reasonable length, and not offensive or hateful. Try searching Wikipedia and Google for facts about your date of birth. For example, if your birthday is Jan. 20, you could print the following:
Did you know? January 20th is the day American presidents are inaugurated, as required by the 20th amendment to the U.S. Constitution.
Solve this problem using Date objects. The methods and behavior of each Date object are described earlier in this document. For Part A you can use an instructor-provided version of Date by downloading the file Date.class from the web site and saving it to the same folder as your Birthday.java file. You can construct a Date object as follows:
Date name = new Date(month, day);
To figure out the number of days until the user's birthday, represent
today and the birthday as Date
objects. By advancing one date until it
reaches the other and counting, you can determine the number of days between
them. Use this approach.
Do not worry about leap years at all for this assignment: Assume February always has 28 days and that the year is always 365 days long.
Assume valid input: that the user will always type a month between 1-12 and a day between 1 and the end of that month.
Your program can in some cases say, “in 1 days” even though this grammar is incorrect.
In Part B you will implement a class named Date, stored in a second file named Date.java. Your Date class should implement the behavior described earlier in this handout.
None of the methods you implement should produce any output. You may not use the Date class provided to you. You may not use any of Java's date-related classes such as java.util.GregorianCalendar or java.util.Date.
You can test your Date program by running your Birthday.java program from Part A with it. By compiling your Date.java file you will overwrite our provided Date.class with your own. (If necessary you can always revert to our Date.class by re-downloading it or backing it up.) Birthday.java is not a great testing program; it might not call all of your Date methods or may not call them in a very exhaustive way that tests all cases and combinations. Therefore you may want to create another small client program of your own to help test other aspects of your Date class's behavior.
You may put additional behavior in your Date class if you like, but we will still test your Birthday program with the Date class we provided you, so your answer to Part A should still run correctly with that class and not only when used with your Date class.
For Part A, you are to solve the problem by creating and using Date objects as much as possible. This is because a major goal of this assignment is to demonstrate understanding of using objects and defining new classes of objects. Part A is not required to have any static methods besides main, though you may have additional methods if you like.
In Part A, you will want to see if two Date objects represent the same date. You cannot use == for this. Instead you should manually see if both objects have the same month and the same day.
For Part B, implement your Date as a new type of objects, using non-static methods, non-static data fields, constructors, etc. as appropriate. You should also properly encapsulate your Date objects by making their methods and constructors public and their fields private. As much as possible you should avoid redundancy and repeated logic within the Date class. Avoid unnecessary fields: Use fields to store the important data of your Date objects but not to store temporary values that are only used within a single call to one method.
In Part B, you can avoid redundancy by having one method call another method.
On both parts of the assignment, you should follow general
past style guidelines such as: appropriately using control structures like
loops and if/else statements; avoiding redundancy using techniques such as
methods, loops, and if/else
factoring; properly using indentation, names, types, variables; and not having
lines longer than 100 characters. You
should properly comment your code with a proper heading in each file, a
description on top of each method, and on any complex sections of your
code. Specifically, place a comment heading at the top of each method of the Date class, written in your own words,
describing that method's behavior, parameters, and return values if any.
You are limited to features in Chapters 1 through 8. As a rough guide, our Birthday.java solution is around 30-40 lines including comments, and our Date.java solution is around 50-60 lines.