Arrays of Objects


Goals


Part 1: Complete the Person Database class (File: Person.java, PersonDB.java)

Download: You will write a few methods and a constructor for the PersonDB class such that they behave as shown in the supplied interactions interactions.
  1. printDB method prints all the entries in the Person database. This method is completed for you.
    >PersonDB people = new PersonDB();  
    >people.printDB()
    Name Age
    jo 25
    flo 18
    mo 19
  2. Complete the getAverageAge(...) method. Note that although this method is a "getter" there is no corresponding instance variable.
    > PersonDB people = new PersonDB();
    > people.getAverageAge()
    20.666666666666668
  3. Add a constructor which has one argument: an array of Persons.
    Sample Interactions:
    > Person[] kids = {new Person("al",2), new Person("sal",4), new Person("val",6)};   
    > PersonDB kidDB = new PersonDB(kids);
    > kidDB.printDB()
    Name Age
    al 2
    sal 4
    val 6
    > kidDB.getAverageAge() 4.0
  4. Complete the isInDatabase(...) method
    In this method, two Strings need to be compared to see if they are equal. However if the == operator is used, Java compares the addresses where the String objects are stored, not the letters in the String. Use the String class' equals or equalsIgnoreCase method to compare two Strings for equality. Here is String class documentation. For example:
    Sample Interactions:
    > PersonDB people = new PersonDB();
    > people.isInDatabase("flo")
    true
    > people.isInDatabase("rex")
    false
    > people.isInDatabase("moses")
    false
    > people.isInDatabase("mo")
    true
    > Person[] kids = {new Person("al",2), new Person("sal",4), new Person("val",6)};   
    > PersonDB kidDB = new PersonDB(kids);
    > kidDB.isInDatabase("valerie")    
    false    
    > kidDB.isInDatabase("s")    
    false    
    > kidDB.isInDatabase("sal")    
    true
    
  5. Add methods getOldest and getYoungest, which return the oldest and youngest person in the database. Make sure you use the correct return type. Note that the interactions below call the getName() method so that we can see the name of the Person being returned. The interactions are as follows:
    Sample Interactions:
    > Person[] kids = {new Person("al",2), new Person("sal",4), new Person("val",6)};   
    > PersonDB kidDB = new PersonDB(kids);
    > PersonDB people = new PersonDB();
    > people.getOldest().getName()   
    "jo" 
    > people.getYoungest().getName()   
     "flo"  
    > kidDB.getOldest().getName()    
    "val"  
    > kidDB.getYoungest().getName()
    "al"