// Define a Person class, version 3. public class Person { // Declare instance variables. private String name; private char gender; private int age; // Define noarg constructor. public Person( ) { name = "Unknown"; gender = 'U'; age = -1; } // Define parameterized constructor. public Person(String theName, char theGender, int theAge) { // Validate theName. if (!theName.equals("")) name = theName; else name = "Unknown"; // Validate theGender. if (theGender == 'F' || theGender == 'M') gender = theGender; else gender = 'U'; // Validate theAge. if (theAge >= 0) age = theAge; else age = -1; } public String getName( ) { return name; } public char getGender( ) { return gender; } public int getAge( ) { return age; } public void hasBirthday( ) { if (age != -1) age++; } public void display( ) { System.out.println("Name: " + name); System.out.println("Gender: " + gender); System.out.println("Age: " + age); } public String toString( ) { return name + " " + gender + " " + age; } // Two Person objects are equal if // their names are the same. public boolean equals(Object other) { String otherName = ((Person) other).getName( ); if (name.equals(otherName)) return true; else return false; } // One Person object is greater than // another if its age is greater. public int compareTo(Object other) { int otherAge = ((Person) other).getAge( ); if (age > otherAge) return 1; else if (age < otherAge) return -1; else return 0; } }