This program 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 meant to be somewhat shorter than other recent assignments and is also worth fewer points.
The first part of this assignment asks you to write a client program that uses an existing Date class written by the instructor. The point of Part A is to give you a bit of practice creating and using Date objects from a client's perspective and to give you an appreciation for the usefulness Date objects in general.
Begin by prompting 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 program's output should match these examples exactly given the same input. See the course web site for more logs with other input.
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
Number of days until birthday 9/9: 309
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
Number of days until birthday 11/30: 103
What is your birthday (month and day)? 9 9 There are 30 days in month #9 Number of days until birthday 9/9: 309
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!
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
Number of days until birthday 10/1: 364
Solve this problem using Date objects. The methods and behavior of each Date object are described on the next page. 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. Absolute days (from Homework #4) should not be used to calculate the days until the users birthday.
You do not have to worry about leap years at all for this assignment. Assume that February always has 28 days and that the year is always 365 days long. (A leap year occurs roughly every 4 years and adds a 29th day to February, making the year 366 days long. But we will ignore that possibility for this assignment.)
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).
The second part of this assignment asks you to implement a class named Date, stored in a second file named Date.java. For all methods/constructors shown below, you may assume that any parameter values passed are valid. Your Date class should implement the following behavior:
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
None of the methods listed above should print any output to the console. You may not utilize any behavior from the instructor-provided Date class to help implement your own Date class, nor 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 might also use the Interactions pane in jGRASP to test constructing Date objects, calling their various methods, and looking at the results.
You may put additional behavior in your Date class if you like, but we will still test your Birthday program with the instructor-provided Date class, so it should still run correctly with that class and not only when used with your Date. In other words, your Birthday.java should not rely on any Date behavior that is not described in this spec.
Complete Part A before Part B, to get a good understanding of how Date objects work from the client's perspective. Write Part B in phases:
Build your Date class incrementally, writing a small amount of code at a time and testing it. It is possible to test an incomplete Date class by writing some of its methods and then creating a small client program to call just those methods, or by playing with Date objects in the Interactions window of jGRASP.
Recall that code in one of an object's methods is able to call any of the object's other methods if so desired. Specifically, when implementing nextDay you may want to consider calling other methods within the Date object to help you.
Since objects can be difficult to visualize and understand, we strongly recommend that you use the jGRASP debugger to step through your code to understand each method's behavior, especially in Part B. You can also use temporary debugging println statements from inside the Date class to see what is going on. For example, printing the state of the current Date object from inside the daysInMonth or nextDay method can help you find bugs.