You are on page 1of 5

CS101 Spring 2014 Homework #15

Introduction to inheritance, dynamic binding, and polymorphism.


1. Aim
This assignment will introduce you to creating new classes by deriving them from existing classes. You will also gain experience in
the advantages of dynamic binding.

2. Files Needed
You will be provided with the following file for this assignment: Hw15.java. You will also need your Date.java file from homework
#14 (with corrections made from grading). Details about these files are given below.
Create a project in Eclipse for this assignment. Add the supplied Hw15.java, and your Date.java file to the src folder in the project
folder. Refresh your project in Eclipse by pressing the F5 key. You will then add all your new files to this same project. Be sure to
include the standard header comments at the top of each file.

3. To be Handed In
Once your exercises below have been completed, you should submit all four files associated with our experiments with Java
inheritance (Hw15.java, Date.java, Reminder.java, & ReminderTest.java). All files should be submitted via the homework #15
page on Oak.
Note: Do not remove any previous functionality/methods from the Date class, even though they dont appear necessary in this
assignment. We will be re-running our test of that class and it should work as specified in homework #14.

4. Exercises
Part I: Deriving a new class from an existing class
A. Getting Started
In Eclipse, create a new project for this part of the assignment. After the project is created, copy the provided Hw15.java file and your
Date.java file (from hw#14) into the src directory of the project, and then refresh the project. Note that Eclipse will flag some errors
in the Hw15.java file this is expected as you will need to supply more code before the file will compile without errors. Before
proceeding, open the Date.java file and correct any errors in your class that were identified in the grading of hw#14. Seek help from
the instructor or a TA if you do not know how to fix a problem identified during grading. [If your hw#14 is not yet graded, please
continue on with the rest of this project, but be sure to correct any errors in your Date class once it has been graded.] Note: your
Date.java class will be re-graded as a part of this assignment, so make sure you fix all problems found during grading.
B. Extending the Date class by deriving a new class
We will experiment with inheritance by deriving a Reminder class from the Date class. A Reminder is simply a Date that also has an
attached message. Thus a Reminder object contains all the information & capabilities of a Date object, and in addition it also contains
the following:
A reminder message in the form of a string.
A flag indicating whether the reminder is annual or one-time.
As you work on the assignment below, you are encouraged to review the examples of inheritance in the lecture notes; in particular the
Person class and the derived Student class.

Class name: Reminder extends Date


Instance variables:
-message: String
-annual: boolean
Methods:
+Reminder()
// default constructor
+Reminder(String msg)
// constructor
+Reminder(String dateStr, String msg)
// constructor
+Reminder(int yy, int mm, int dd, String msg)
// constructor
+Reminder(Reminder otherReminder)
// constructor
+setReminder(String dateStr, String msg): void
+setReminder(int yy, int mm, int dd, String msg): void
+setReminder(Reminder otherReminder): void
+setMessage(String msg): void
+getMessage(): String
+makeAnnual(): void
+makeOneTime(): void
+isAnnual(): boolean
+toString(): String
+equals(Object other): boolean
+trigger(): void
Create this class by following these steps. Note: do not let this description intimidate you. Almost all of these methods are only 2-3
lines of code if you do it correctly. The largest is 6-10 lines of code.
1.

Add a new class to your project. Name this class Reminder and have this class be derived from (extends) the Date class. Since
the Reminder class is derived from the Date class it automatically inherits all the capabilities of the Date class (e.g., setDate(),
getDay(), setLongDisplay(), incrementYear(), etc.).

2.

Add two private instance variables to the Reminder class: message (of type String) and annual (of type boolean).

3.

Create the following set of public constructors. In all cases, the constructors set the annual flag to false. Remember to use other
methods defined in the class whenever possible while defining your constructors. Use super() to invoke the constructors in the
base class.
a. public Reminder() the default constructor which sets the date to the default date and the message to No message. (note
the required period).
b. public Reminder(String msg) which sets the date to the default date and the message to the parameter msg.
c. public Reminder(String dateStr, String msg) which set the date to the dateStr parameter and the message to the parameter
msg.
d. public Reminder(int yy, int mm, int dd, String msg) which set the date to mm/dd/yy and the message to msg.
e. public Reminder(Reminder otherReminder) which sets the date and the message to be the same as those in otherReminder.
Note that since the Reminder class is derived from the Date class, the parameter otherReminder is also a Date object so
take advantage of this polymorphism.

4.

Create the following set of public accessors and mutators (you must name the methods exactly as specified). Only the headers are
specified here; your job is to write the body of each method (remember to use other defined methods, whether defined here or
inherited, whenever possible):
a. public void setReminder (String dateStr, String msg) change the date and message of the Reminder to match the input
parameters. Do not change the annual flag.
b. public void setReminder (int yy, int mm, int dd, String msg) -- change the date and message of the reminder to match the input
parameters. Do not change the annual flag.
c. public void setReminder (Reminder otherReminder) -- change the date and message of the reminder to match those in
otherReminder. Do not change the annual flag.
d. public void setMessage (String msg) set the message of the reminder.
e. public String getMessage() get the message of the reminder.
f. public void makeAnnual () set the annual flag to true.
g. public void makeOneTime () set the annual flag to false.
h. public boolean isAnnual () return the status of the annual flag.

5.

Override the toString() method of the Date class by defining your own toString() method in the Reminder class. The toString()
method of the Reminder class should return a string in the following format:

Date ==> The reminder message (Annual|One Time)


The date should be printed out in either long form or short form depending upon the current settings of the object. After the date,
the string " ==> " should be printed (thats a space, two equal signs, a greater than sign, and another space). That should be
followed by the message of the reminder, which is then followed by a space and then by either (Annual) or (One Time)
depending upon whether the reminder is annual or not. See the sample execution below for examples of properly formatted
Reminder strings.
6.

Next we are going to override the equals() method. When we override a method, the heading of the method must be the same
(same method name and same parameter list). If the parameter list is different, then we have overloading and not overriding. Thus
our Reminder class will contain the following equals method:
public boolean equals (Object other) this method will take any object as a parameter. If we had specified that the parameter was
a Reminder object, then we would have been using overloading and not overriding.
Our definition of the equals() method should work as follows:
a. If the parameter other is an instance of the Reminder class, then we will cast it to a Reminder object and compare
both the date and the message of the two reminders to see if they are equal. Do a case insensitive compare when
comparing the two messages. Reminder: you can use super.equals() to access the equals() method of the Date class.
Reminder 2: every Reminder object is also a Date object.
b. In all other cases (the parameter is not a Reminder object), then the method returns false.

7.

The last method that we will add to the Reminder class is a method to trigger a reminder. The method header is: public void
trigger (). This method will perform the following actions: (1) It prints out the reminder information on the screen via println (use
the toString() method). (2) If the reminder is an annual reminder, it then increments the year appropriately so that the reminder is
set for the next year (remember that every Reminder object is also a Date object, and thus you can invoke any Date method on it).
If the reminder is a one-time reminder, no additional actions are taken. See the sample execution below for an example.

C. Testing your Reminder class


Create another class ReminderTest.java which has a main method. In the main method, instantiate some Reminder objects and fully
test your Reminder class. How well you test your Reminder class will be a part of your grade.

D. Having fun with polymorphism and late binding


Once you have completed your ReminderTest class, all the compiler errors in the Hw15 program should disappear. Lets take a
moment to look at the contents of this file. The main method declares an array of Reminder objects and initializes the array by calling
the static method makeReminderArray(). After the array is created, the writeDateArray() method is called. In this case we are calling
the method and passing an array of Reminder objects rather than an array of Date objects as specified in the parameter list. This
method still works correctly, since all Reminder objects are Date objects as well (ah, the beauty of inheritance).
Next, the program allows the user to change some of the reminders to become annual reminders. It finds the reminders by using a
search() method for Date arrays.
Finally, the program simulates the passing of time by setting a Date object to 1/1/2014, and then stepping through all dates for the
next 5 years. For each date, we see if there are any Reminders for that date, and if there are we trigger each one. The current date is
printed on the first of each month to show the passing of time.

E. Adding documentation
As a last step, add javadoc comments before all the methods in the Reminder class. These comments should specify what each
method does, describe all the parameters, and state any pre- & post-conditions.

Example:
Heres a sample execution of Hw15.java:
How many reminders do you want?
-2
You must have at least one reminder. Try again.
How many reminders do you want?
5
Enter each reminder on a single line as "mm/dd/yyyy message".
example: 4/23/2014 CS101 final exam.
Enter reminder #1: 4/23/2014 CS101 Final Exam
Enter reminder #2: 7/4/2014 Fourth of July family reunion at Grandma's house

Enter reminder #3: 2/24/2014 Mom's birthday -- call and apologize for not sending a gift
Enter reminder #4: 8/20/2014 First day of classes in fall semester
Enter reminder #5: 10/3/2014 Dentist appointment at 2:00pm
Your reminders
04/23/2014 ==>
07/04/2014 ==>
02/24/2014 ==>
08/20/2014 ==>
10/03/2014 ==>

are:
CS101 Final Exam (One Time)
Fourth of July family reunion at Grandma's house (One Time)
Mom's birthday -- call and apologize for not sending a gift (One Time)
First day of classes in fall semester (One Time)
Dentist appointment at 2:00pm (One Time)

You may now make selected reminders annual.


Enter the date for any reminder you want to annualize; enter a blank line to quit.
7/4/2014
That reminder is now annual.
Enter the date for any reminder you want to annualize; enter a blank line to quit.
7/5/2014
That date was not found in the list of reminder events
Enter the date for any reminder you want to annualize; enter a blank line to quit.
02/24/2014
That reminder is now annual.
Enter the date for any reminder you want to annualize; enter a blank line to quit.

Here are your final reminders:


04/23/2014 ==> CS101 Final Exam (One Time)
07/04/2014 ==> Fourth of July family reunion at Grandma's house (Annual)
02/24/2014 ==> Mom's birthday -- call and apologize for not sending a gift (Annual)
08/20/2014 ==> First day of classes in fall semester (One Time)
10/03/2014 ==> Dentist appointment at 2:00pm (One Time)
Beginning the simulation of 5 years (1/1/2014 to 12/31/2018).
The current date is printed on the first of each month.
01/01/2014... 02/01/2014...
02/24/2014 ==> Mom's birthday -- call and apologize for not sending a gift (Annual)
03/01/2014... 04/01/2014...
04/23/2014 ==> CS101 Final Exam (One Time)
05/01/2014... 06/01/2014... 07/01/2014...
07/04/2014 ==> Fourth of July family reunion at Grandma's house (Annual)
08/01/2014...
08/20/2014 ==> First day of classes in fall semester (One Time)
09/01/2014... 10/01/2014...
10/03/2014 ==> Dentist appointment at 2:00pm (One Time)
11/01/2014... 12/01/2014... 01/01/2015... 02/01/2015...
02/24/2015 ==> Mom's birthday -- call and apologize for not sending a gift (Annual)
03/01/2015... 04/01/2015... 05/01/2015... 06/01/2015... 07/01/2015...
07/04/2015 ==> Fourth of July family reunion at Grandma's house (Annual)
08/01/2015... 09/01/2015... 10/01/2015... 11/01/2015... 12/01/2015... 01/01/2016... 02/01/2016...
02/24/2016 ==> Mom's birthday -- call and apologize for not sending a gift (Annual)
03/01/2016... 04/01/2016... 05/01/2016... 06/01/2016... 07/01/2016...
07/04/2016 ==> Fourth of July family reunion at Grandma's house (Annual)

08/01/2016... 09/01/2016... 10/01/2016... 11/01/2016... 12/01/2016... 01/01/2017... 02/01/2017...


02/24/2017 ==> Mom's birthday -- call and apologize for not sending a gift (Annual)
03/01/2017... 04/01/2017... 05/01/2017... 06/01/2017... 07/01/2017...
07/04/2017 ==> Fourth of July family reunion at Grandma's house (Annual)
08/01/2017... 09/01/2017... 10/01/2017... 11/01/2017... 12/01/2017... 01/01/2018... 02/01/2018...
02/24/2018 ==> Mom's birthday -- call and apologize for not sending a gift (Annual)
03/01/2018... 04/01/2018... 05/01/2018... 06/01/2018... 07/01/2018...
07/04/2018 ==> Fourth of July family reunion at Grandma's house (Annual)
08/01/2018... 09/01/2018... 10/01/2018... 11/01/2018... 12/01/2018...
My how time flies when you are having fun.

5. Additional requirements
A. You must start your program with header comments that provide your name, VUnetID, email address, the date the program
was last modified, an honor statement (I have neither given nor received unauthorized help on this assignment), and a short
description of the program (see the examples distributed with homework #2).
B. Each method that you write (except main), should be preceded by a block of Javadoc comments which describe what the
method does, what the parameters are, and what it returns. Any required preconditions should also be clearly stated. Keep
them simple.
C. None of your methods should be more than 25 lines long, preferably shorter (most of our methods are 12 lines or less). Lines
that are blank, or contain only comments or a single curly brace do not count. This does not apply to the main method of
your ReminderTest program.
D. You should use a consistent programming style. This should include the following.
a. Meaningful variable & method names
b. Consistent indenting
c. Use of "white-space" and blank lines to make the code more readable
d. Use of comments to explain pieces of tricky code
e. Lines that do not extend beyond column 100 (or column 80 is even better)
See the code examples in the class text for a good formatting style.

6. Submission for grading


Once you have completed the exercise, submit the files Hw15.java, Date.java, Reminder.java, & ReminderTest.java for grading
by visiting the homework #15 page in Oak. It is highly recommended that you carefully review this project specification one last time
before submitting your code make sure that all the methods you wrote match the specifications in this document.
NOTE: Since this assignment is due on the last day of classes, no late submissions will be accepted even if you have free late days
remaining.
NOTE: Your Date.java class will be re-graded, so be sure to fix all errors that were identified during the grading of hw#14.

7. Grading
This lab is worth 30 points. Your grade will be based on whether your solution is correct or not, and on how closely you followed the
directions above. As usual, programming style will also affect your grade.

You might also like