Lab 12
Version 1

 

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.

  1. 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.
    1. 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}; 
      
    2. A default constructor that produces a date set to 1/1/1997.
    3. A boolean-valued method named equals which takes another Date as a parameter and returns true if the two Dates are equal.
    4. 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.
    5. 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.
    6. A void method of no parameters named print which prints a reasonable presentation of the object (i.e., mm/dd/yyyy) using HMCSupport.out.

  2. 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.
    1. A constructor which takes Strings for the name and ssn fields, and three ints for the bithdate.
    2. 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.
    3. 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.
    4. A void method of no parameters named print which prints a reasonable presentation of the object on one line using HMCSupport.out.

  3. 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:
    1. Print out all the records.
    2. 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.
    3. 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.

Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1997 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Sun, Dec 7, 1997 at 12:20:54 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_12/lab.html