I don't understand what this homework is asking me to do...
In our last project, we created a person.java class that made an array of people (or just instances of people)...and it had their ID, their mom, their dad, birthdate, number of children, and an array of their children. It had predictable methods like get or set mom and dad, add child, remove child, unset mom or dad, boolean to see if another instance is or is not a child of this instance, get sex, get id...i think that was it.
Anyway...the point of this project is to make small changes to our last project to allow unlimited number of children for each person class. The last project had a static 开发者_JAVA技巧int variable for the maximum number of allowable children set to 10.
To do this, we're to implement a list. And create another class called personlist.java, which is to be a sublcass of java.util.LinkedList. We have to make sure to save Person.java in the same project where we create PersonList.java so that the compler can recognize java.util.LinkedList. The PersonList class should transparently handle all methods related to adding/removing/finding the children of a person. Since PersonList is a subclass of LinkedList, we will have all the LinkedList methods available to us.
I don't even know where to start. I still have my Person.java class. I'm using Eclipse...so I made a new project. Made a class called PersonList.java. Made another class called Person.java and copied my old project into it.
How do I declare the new class so that it "communicates" with Person.java?
import java.util.LinkedList;
public class PersonList extends LinkedList<Person> {
}
Is that right? I'm also confused as to why we weren't told to just modify Person.java to utilize linked lists. I can do that. Why create another class called PersonList.java? And what the heck goes in there? How do I call methods in PersonList.java from Person.java?
Any insight would be greatly appreciated!
Switch you're children variable in the Person class to be of type LinkedList.
public class Person {
....
private LinkedList<Person> children;
....
}
Unless you're leaving out part of the problem there's no need whatsoever to extend the LinkedList class. In this instance you want composition rather than inheritance.
Oh and when you submit your assignment tell your professor why you picked your solution.
LinkedList
is not designed for inheritance. Forwarding to better than inheritance.
精彩评论