Results 1 to 7 of 7
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Method Accessing

    Exercise 5.9* 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.

    Exercise 5.10* Change the Person class of Listing 5.2 so that any outside class can find out the average birth year of all the Persons who have been created so far. Use 0 for the answer if no Persons have yet been created.

    Java Code:
    public class Person extends Object
    {
         private static int theNumPersons = 0;        // initialize num
         /////////////////////////////////
         private String itsFirstName;
         private String itsLastName;
         private int itsBirthYear;
    
    
         /** Construct a new Person with given names and birth year. */
    
         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;
         }    //=======================
    }



    My question is can't all classes access this class (and therefore it's methods) if they just implement an object for that class?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Method Accessing

    My question is can't all classes access this class (and therefore it's methods) if they just implement an object for that class?
    Yes. With no change necessary to the class.

    More exactly some other code - in some other class - could create a bunch of Person instances (or obtain the collection of people from elsewhere) and then use the already existing public methods to find the smallest or average birth year of those people. But these questions are asking for something a little different...

    Other classes are supposed to be able to find out the smallest (/average) for all the Person instances that have ever been created. Ie not just the instances they know about.

    My guess is that you need other static variables besides theNumPersons so that these statistics can be calculated. And probably other static methods to provide this information. (I have a general prejudice against static anythings, and would strongly consider getting statistics for specific groups of people to be enough. But I guess the questions are meant to illustrate what static *can* do.)

    [edit] note on jargon: "implement" tends to have a specific meaning within Java (and other programming languages). It's the *how* rather than the *what* of a method or class (or interface). So we speak of a concrete class implementing an interface, and, analogously the programmer implements a method when they write the code within the outer curly braces. As far as "person=new Person()" is concerned we would talk of "creating an instance of Person", or of "constructing a Person" or "instantiating a Person".
    Last edited by pbrockway2; 01-11-2013 at 02:36 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Method Accessing

    Quote Originally Posted by Army View Post
    My question is can't all classes access this class (and therefore it's methods) if they just implement an object for that class?
    Yes they can, but often if they do this, the methods will be useless since they can't be used to get data held by all of the Person objects that have been created, only the ones created by your new class.

    My guess is that your task is to create a Person collection class that holds a collection of some sort, be it an array or ArrayList, of Person objects, that this collection class will need to have methods to allow it to iterate through its own collection of Person objects to find the youngest or the oldest, or to average out the ages.

  4. #4
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Method Accessing

    Thanks. Do you have any idea of what needs to be added? This whole thing seems foreign to me since in my mind the whole Object way is blocking it for me.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Method Accessing

    Well, you have two suggestions...

    Both Fubarable and I said what would be added.

    As far as my suggestion is concerned, do you understand what the Person class does at the moment to be able to report how many person instances have been created? It

    (1) Has a static theNumPersons variable to "record" this information.
    (2) Updates the static variable whenever a new Person is created (in the constructor)
    (3) Has a static getNumPersons() method to return the number

    The same three steps are needed to be able to return the smallest birth year. (And the average, but smallest is the place to start.)

  6. #6
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Method Accessing

    Quote Originally Posted by pbrockway2 View Post
    Well, you have two suggestions...

    Both Fubarable and I said what would be added.

    As far as my suggestion is concerned, do you understand what the Person class does at the moment to be able to report how many person instances have been created? It

    (1) Has a static theNumPersons variable to "record" this information.
    (2) Updates the static variable whenever a new Person is created (in the constructor)
    (3) Has a static getNumPersons() method to return the number

    The same three steps are needed to be able to return the smallest birth year. (And the average, but smallest is the place to start.)
    Sorry, I posted the question without refreshing the page. Thank you both for the suggestions! From what you are both saying is I honestly don;t understand what Furable wants me to do lol. I kind of understand but not necessarily. I sort of understand the way your are saying the program works but I still don't understand what is actually needed.
    Last edited by Army; 01-11-2013 at 03:37 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Method Accessing

    It is vital you understand the code before you add to it.

    To have the code return the minimum birth year

    (1) Add a static variable to remember the smallest birth year seen so far
    (2) In the constructor add some code to update this variable if necessary. You won't always need to update it - only when an old person is being created. So, probably, an if statement will be involved.
    (3) Add a static method that returns the smallest birth year.

    Start with (1).

Similar Threads

  1. Accessing a method in another class
    By denno in forum New To Java
    Replies: 14
    Last Post: 04-07-2011, 05:59 AM
  2. Accessing a method in a different class
    By adjit in forum New To Java
    Replies: 4
    Last Post: 04-06-2011, 10:11 PM
  3. Help with accessing static method
    By jazzermonty in forum New To Java
    Replies: 9
    Last Post: 03-07-2011, 09:04 PM
  4. Need help with accessing files in a method
    By t0nydanzuh in forum New To Java
    Replies: 6
    Last Post: 10-03-2010, 06:05 PM
  5. Accessing instance outside of creation method
    By meringue in forum New To Java
    Replies: 6
    Last Post: 03-22-2009, 01:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •