class Person { public Person( ) { this( "unknown person", -1 ); } public Person( String n, int a ) { name = n; age = a; } public String getName( ) { return name; } public int getAge( ) { return age; } public String toString( ) { return getName( ) + " " + getAge( ); } private String name; private int age; } class Student extends Person { public Student( ) { super( ); gpa = 0.0; } public Student( String n, int a, double g ) { super( n, a ); gpa = g; } public double getGPA( ) { return gpa; } public String toString( ) { return getName( ) + " " + getAge( ) + " " + getGPA( ); } private double gpa; } public class Day11 { public static Person getOlder( Person p1, Person p2 ) { if( p1.getAge( ) > p2.getAge( ) ) return p1; else return p2; } // Assume arr.length >= 1 public static Person getOldest( Person [ ] arr ) { int maxIndex = 0; for( int i = 1; i < arr.length; i++ ) if( arr[ i ].getAge( ) > arr[ maxIndex ].getAge( ) ) maxIndex = i; return arr[ maxIndex ]; } // Not all entries are Students // Option #1: Illegal... IllegalArgumentException // Option #2: Include only Students public static Person getMaxGPA( Person [ ] arr ) { double maxGPA = -1.0; Student max = null; for( Person p : arr ) { if( p instanceof Student ) { Student s = (Student) p; if( s.getGPA( ) > maxGPA ) { max = s; maxGPA = s.getGPA( ); } } } return max; } public static void main( String [ ] args ) { Person p1 = new Person( "Chris", 29 ); Person p2 = new Person( "Fran", 40 ); System.out.println( p1 ); System.out.println( p2 ); System.out.println( "Older of p1, p2: " + getOlder( p1, p2 ) ); Student s1 = new Student( "Billie", 36, 3.8 ); Student s2 = new Student( "Jackie", 42, 3.6 ); System.out.println( s1 ); System.out.println( s2 ); System.out.println( "Older of s1, s2: " + getOlder( s1, s2 ) ); System.out.println( "Older of p1, s1: " + getOlder( p1, s1 ) ); Person [ ] arr = { p1, p2, s1, s2, new Person( "Alex", 30 ) }; System.out.println( "Oldest of arr: " + getOldest( arr ) ); System.out.println( "Max GPA: " + getMaxGPA( arr ) ); } }