0

I've created class Person, which is extended by classes Student and Employee (which is extended by other Employee type classes). The person class looks like:

String name;
 int ssn;
 int age;
 String gender;
 String address;
 String PNumber;
 static int count;

//empty constructor
public Person(){
    count++;
}

//print count
public static void printCount(){
    System.out.println("The number of people is: "+ count);
}

//constructor with name
public Person(String name){
    this.name = name;
    count++;
}

/*constructor to create default person object*/
public Person(String name, int ssn, int age, String gender, String address, String PNumber)
{

    this.name = name;
    this.ssn = ssn;
    this.age = age;
    this.gender = gender;
    this.address = address;
    this.PNumber = PNumber;
    count++;
}

I'm currently trying to create a method that will display all Persons if they're gender = "Male". I have:

//display Males
public void print(String gender){ 
    if(this.gender.contentEquals(gender)){
        //print out person objects that meet this if statement
    }
}

I'm not sure how to refer to the objects (students and employees that are all persons) within the method to return them. And I also don't know how to refer to this method in the main method. I can't use Person.print, but if I use

Person james = new Person(); 

and then use

james.print("Males"); 

I'm only returning james (and the method doesn't make sense in that context).

Any help appreciated.

Leo
  • 149
  • 4
  • 12

1 Answers1

1

First, the print method should be made into a static method. It is independent of each individual Person object, so making it static will allow you to call it in the main method as

Person.print("Male");

To refer to Person objects in the print method, you will need to pass it a collection of Person objects as a parameter. You should keep all instances of Person in an array and pass that into the print method when you call it. Then the print method can be

public static void print(String gender, Person[] people) {
    for(Person x : people)
        if (x.gender.equals(gender))
            //print the person
}

With this modification you should call it from the main method as

Person.print("Male", people);

where people is the array you keep all Person objects in.

tlent
  • 94
  • 1
  • 6
  • Thanks so much. I have a follow-up comment/question. Could you explain (Person x : people)? I'm a bit of a beginner. Does it create an instance of the Person class called people? I had also made the print method static at one point, but I was unable to use gender as it was not a static variable. – Leo Feb 13 '13 at 01:57
  • Either way I've got the hang of it now. Again, thank you very much – Leo Feb 13 '13 at 01:58
  • 1
    No problem. for(Object variableName : collection) is an alternative method of iterating through a collection. Basically what it means is for each instance of an Object (Person in your program) in the collection (people), set variableName equal to it and do whatever is in the following block. It is called a foreach loop and you can read what is probably a better explanation of it [here.](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) – tlent Feb 13 '13 at 02:08