For my homework, we’ve been tasked with creating:
Person class with variables firstName, lastName, streetAddress, zipCode and phone.
CollegeEmployee extends Person and adds ssn, salary, and deptName.
Faculty extends CollegeEmployee by adding a boolean tenure.
Last but not least, Student extends person by adding GPA and major.
Everything looks good displaying to screen, and I’m moving on to the next part of the assignment which is to create 14 records (7 students, 4 employees and 3 faculty) in an array.
3 different classes, with multiple data types, and I cannot for the life of me figure out how to populate an array with this. This is the first array I’ve created that’s not been completely integer. The Java Tutorials didn’t give me anything, and while Java: Generic Static Multidimensional Arrays has some great information, it’s a little more than I can wrap my head around right now.
I’d initially thought of creating array[14][10] — fourteen variables each for ten objects — but I can’t mix data types. That’s where I got lost.
Anyone have any suggestions on how to design this array and be able to display the values from it afterward?
Any hints and suggestions would be appreciated!
Thanks.
You could also create a
Person[]array, just as you would anint[]array. e.g.You can then add people to the Array like this:
If you want to check what type of person is in each index you will want to use
instanceof. Try looking here for more helpOne example of using
instanceofis:Or try using generics.
You could create an
ArrayList<Person>and can then add any type of person to this ArrayList.e.g.
Again you are able to use
instanceofto check which type of person is in each index!Also if you never write
In your code then consider making your class abstract.