Hey guys, I'm stuck on this problem and I'm getting absolutely nowhere with it.
Our Person class has several subclasses, namely CollegeKid and Oldie. Suppose there is yet another subclass: Programmer. Notice that, like the other subclasses, a Programmer also has an additional data member: caffeinatedBeveragesConsumed. Also notice that we provide Programmer with a print method. All of these subclasses may be referred to as members of the Person class. This is an example of polymorphism.
Suppose we have an array containing a number of Person objects. We want to print information for all of the objects in the array. We can do this by looping through the array and calling each object's print method. The objects in the array can be instances of any of the above classes.
For this exercise, enter code in the box below that will allow the printPeople method to produce a printout to the console of all of the "people" in the array.
public void printPeople (Person[] people) { //code goes here }
I'm not sure how to even begin writing this code. I know I need a for loop and at every increment call the print method of the subclass.. so I have
public void printPeople (Person[] people){ for (int i=0; i<people.length; i++){ print(); } }
Here are all the subclasses:
/** * This class contains data and methods pertaining * to a person. The data contained in this class are: * name and age. */ public class Person { private String name; private int age; public Person(String n, int a) { this.name = n; this.age = a; } public String getName() { return name; } public int getAge() { return age; } public void setName(String n) { name = n; } public void setAge(int a) { age = a; } public void print( ) { System.out.println("Name: " + name); System.out.println("Age: " + age); } } /** * This class contains data and methods pertaining * to a college kid. The datum contained in this class is: * grade point average- gpa. This class derives from Person. */ public class CollegeKid extends Person { private double gpa; public double getGPA() { return gpa; } public void setGPA(double g) { gpa = g; } public void print( ) { /* print code here */ } } /** * This class contains data and methods pertaining * to an oldie- i.e. someone over the age of, oh say 30. * The datum contained in this class is: * whether or not this person is a Perry Como fan. * This class derives from Person. */ public class Oldie extends Person { private boolean perryComoFan; public boolean isPerryComoFan() { return perryComoFan; } public void setPerryComoFan(boolean f) { perryComoFan = f; } public void print( ) { /* print code here */ } } /** * This class contains data and methods pertaining * to a programmer. The datum contained in this class is: * caffeinatedBeveragesConsumed. This class derives from Person. */ public class Programmer extends Person{ private int caffeinatedBeveragesConsumed; public int getCaffeinatedBeveragesConsumed() { return caffeinatedBeveragesConsumed; } public void setCaffeinatedBeveragesConsumed(int cbc) { caffeinatedBeveragesConsumed = cbc; } public void print( ) { /* print code is provided */ }