Professional Documents
Culture Documents
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.
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:
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.
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)
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.
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.