21.3 Problem 1:

// Note the following code has been modified to include an age field in order to work with the second part of the problem
// but will produce the same output.

class Person {
    private String name;
    private int age;
    
    public Person(String n) {
        name = n;
    }
    public Person(int a, String n) {
        name = n;
        age = a;
    }
    
    public String toString() {
        return name;
    }

    public void setName(String n) {
        name = n;
    }
    
    public int getAge() { return age; }
}

public class AliasTest {    
    public static void main(String[] args) {
        Person boss;
        Person p1 = new Person("A");
        Person p2 = new Person("B");
        boss = p1;
        p2 = boss;
        p2.setName("C");
        System.out.println(p1);
        System.out.println(p2);
    }
}

/*  The above code outputs:
 *  C
 *  C
 *
 *  since boss is assigned to p1 then p2 is assigned to boss (which is currently p1)
 *  so as a result, p2.setName("C") changes all three names to "C"
 */

21.4 Problem 2:

class oldestPerson {
    public static void main(String[] args) {
        Person p[] = {new Person(10, "Bill"), new Person(51, "Bob"), new Person(20, "Dave")};                
        Person oldest = p[0];
        
        // Perform a linear search for the oldest person
        for(int i = 0; i < p.length; i++)
        {
            if(p[i].getAge() > oldest.getAge())
                oldest = p[i];
        }
        System.out.println("The oldest person is " + oldest);
    }
}
