// Program: Lab 12 solution
// Author: Joshua S. Hodas
// Date: December 6, 1998
// Purpose: To solve the problems in lab 12
import HMC.HMCSupport;
class Date {
public int month;
public int day;
public int year;
// A constructor that takes values for each field, and checks that
// they are valid. If either the month or day is out of range it is
// set to 1.
Date(int month, int day, int year) {
int[] monthDays = {31,28,31,30,31,30,31,31,30,31,30,31};
if (month < 1 || month > 12) {
this.month = 1;
}
else {
this.month = month;
}
if (day < 1 || day > monthDays[month-1]) {
this.day = 1;
}
else {
this.day = day;
}
this.year = year;
}
// The default constructor, it sets the date to 1/1/97.
Date() {
this(1,1,1997);
}
// A field-value-based equality tester. In this and the following
// method, the use of "this." is actually extraneous. I happen to
// find it less confusing, though that is a matter of taste.
public boolean equals(Date d) {
return (this.month == d.month &&
this.day == d.day &&
this.year == d.year);
}
// A field-value-based comparison method
public int compareTo(Date d) {
int result = 0; // Assume they are equal.
if (this.year < d.year) {
result = -1;
}
else if (this.year > d.year) {
result = 1;
}
else if (this.month < d.month) {
result = -1;
}
else if (this.month > d.month) {
result = 1;
}
else if (this.day < d.day) {
result = -1;
}
else if (this.day > d.day) {
result = 1;
}
else {
result = 0;
}
return result;
}
// Return the day number in the year. I.e. March 2 = 31+28+2 = 61.
public int dayInYear() {
int[] monthDays = {31,28,31,30,31,30,31,31,30,31,30,31};
int result = 0;
for (int i = 0; i < month-1 ; i++) {
result += monthDays[i];
}
result += this.day;
return result;
}
// A print function
public void print() {
HMCSupport.out.print(month + "/" + day + "/" + year);
}
}
class Person {
String fname, lname;
String ssn;
Date dob;
// A constructor method
Person(String fname, String lname, String ssn,
int month, int day, int year) {
this.fname = fname;
this.lname = lname;
this.ssn = ssn;
this.dob = new Date(month,day,year);
}
// An age comparison method
public boolean youngerThan(Person p) {
// you're younger if your date of birth is later
return (dob.compareTo(p.dob) == 1);
}
// A birthday (not date of birth) equality checker
public boolean sameBirthday(Person p) {
return (dob.dayInYear() == p.dob.dayInYear());
}
// A print method
public void print() {
HMCSupport.out.setWidth(30);
HMCSupport.out.setLeftJustify();
HMCSupport.out.print(lname + ", " + fname);
HMCSupport.out.setWidth(11);
HMCSupport.out.setRightJustify();
HMCSupport.out.print(ssn + " ");
HMCSupport.out.setWidth(0);
dob.print();
HMCSupport.out.println();
}
}
class LAB12 {
public static void main(String args[]) {
Person[] people;
int numPeople;
int index;
HMCSupport.out.print("Please enter the number of " +
"people which will be used to test " +
"this program: ");
numPeople = HMCSupport.in.nextInt();
people = new Person[numPeople];
for (int i = 0 ; i < numPeople ; i++) {
HMCSupport.out.println("Please enter data for person " +
(i+1) + ": ");
people[i] = getPerson();
}
HMCSupport.out.println();
HMCSupport.out.println("Please enter the index of a " +
"person, and I will print out the " +
"people younger than");
HMCSupport.out.print("that person: ");
index = HMCSupport.in.nextInt();
HMCSupport.out.println();
HMCSupport.out.println("That person is: ");
people[index].print();
HMCSupport.out.println();
HMCSupport.out.println("The people younger are: ");
for (int i = 0 ; i < people.length ; i++) {
if (people[i].youngerThan(people[index])) {
people[i].print();
}
}
HMCSupport.out.println();
HMCSupport.out.println();
HMCSupport.out.println("Please enter the index of a " +
"person, and I will print out the " +
"people with the");
HMCSupport.out.print("same birthday as that person: ");
index = HMCSupport.in.nextInt();
HMCSupport.out.println();
HMCSupport.out.println("That person is: ");
people[index].print();
HMCSupport.out.println();
HMCSupport.out.println("The people with that birthday are: ");
for (int i = 0 ; i < people.length ; i++) {
if (i != index && // make sure it is not that person
people[i].sameBirthday(people[index])) {
people[i].print();
}
}
}
public static Person getPerson() {
String fname, lname;
String ssn;
int month, day, year;
HMCSupport.out.print("First Name: ");
fname = HMCSupport.in.nextWord();
HMCSupport.out.print("Last Name: ");
lname = HMCSupport.in.nextWord();
HMCSupport.out.print("Social Security Number: ");
ssn = HMCSupport.in.nextWord();
HMCSupport.out.print("Month of Birth: ");
month = HMCSupport.in.nextInt();
HMCSupport.out.print("day of Birth: ");
day = HMCSupport.in.nextInt();
HMCSupport.out.print("Year of Birth: ");
year = HMCSupport.in.nextInt();
return new Person(fname,lname,ssn,month,day,year);
}
}
|