| |
The first two problems ask you to define a couple of classes. The last problem
specifies the design of a simple driver program to test the classes.
- Define a class named
Date with three integer fields for storing
the month (1-12), the day (1-31), and the year (use full years, not two-digit dates).
The class should include at least the following methods.
- A constructor, which takes three integer values as parameters. It should insure
that the date is valid. I.e., it should only allow
the month to be 1-12, and the day to be in the appropriate range for the month.
If either is invalid, just set it to 1. You may ignore leap years and assume that February
always has 28 days. You may find it helpful to declare the following array in the method:
int[] monthDays = {31,28,31,30,31,30,31,31,30,31,30,31};
- A default constructor that produces a date set to 1/1/1997.
- A
boolean-valued method named equals which takes another
Date as a parameter and returns true if the two Dates are equal.
- An
int-valued method named compareTo which takes another
Date as a parameter and returns -1 if the Date executing the method is
earlier, 0 if the two Dates are equal, and 1 if the Date executing the
method is later.
- An
int-valued method named dayInYear of no parameters
which returns a value between 1 and 365 inclusive giving the position of that Date
in its year. I.e. March 2nd of any year would return 61 (since there are 31 days in January, 28 in February,
and this is the second day of March). You may ignore leap years and assume that February
always has 28 days. You may, again find it helpful to employ the monthDays
array, or some similar device.
- A
void method of no parameters named print which
prints a reasonable presentation of the object (i.e., mm/dd/yyyy) using HMCSupport.out.
- Define a class named
Person with fields for first and last name, social
security number (a nine-digit value best stored as a String), and date of birth (use the
Date class defined above. The class should include at least the following methods.
- A constructor which takes
Strings for the name and ssn fields, and three
ints for the bithdate.
- A
boolean-valued method youngerThan which takes a
Person as a parameter and returns true if the Person executing the method
is younger than the Person received as a parameter.
- A
boolean-valued method sameBirthday which takes a
Person as a parameter and returns true if the two people celebrate their birthdays
on the same date. (Hint: send dayInYear messages to each Person's
birthdate.
- A
void method of no parameters named print which
prints a reasonable presentation of the object on one line using HMCSupport.out.
- Write a program which creates an array of a user-specified number of
Person
records and prompts the user to fill in the data for the array. It should then:
- Print out all the records.
- Ask the user for an array index and print out all the records for people younger than
the person in that position in the array.
- Ask the user for an array index and print out all the records for people with
the same birthday as the person in that position in the array.
|