Homework help please Exercise 5.9 (Javadoc) Change the Person class of listing 5.2 so that any outside class can find out the smallest birth year of all the Persons who have been created so far. Use -1 for the answer if no Persons have yet been created. I have no idea how to do this exercise. * Object class and listing is in comments.
Object class and listing public class Person extends Object { private static int theNumPersons = 0; // initialize num private String itsFirstName; private String itsLastName; private int itsBirthYear; public Person (String first, String last, int year) { super(); theNumPersons++; // update num itsFirstName = first; itsLastName = last; // initialize last name itsBirthYear = year; } //======================= /** Tell how many different Persons exist. */ public static int getNumPersons() // access num { return theNumPersons; } //====================== /** Return the birth year. */ public int getBirthYear() { return itsBirthYear; } //======================= /** Return the first name. */ public String getFirstName() { return itsFirstName; } //======================= /** Return the last name. */ public String getLastName() // access last name { return itsLastName; } //======================= /** Replace the last name by the specified value. */ public void setLastName (String name) // update last name { itsLastName = name; } //======================= }
what do you need?
I think something like this might work: 1. Create a new private static int to hold the smallest birth year and initialize it to -1. 2. Add logic in constructor to update this new variable when appropriate. 3. Create a new static getter to allow outside classes to access the value. Static variables and methods are accessible and have the same value in all instances of the class they are defined in.
Thank You :) I'll post other questions if I have any :)
Join our real-time social learning platform and learn together with your friends!