array split newbie help needed
Hi, I need some help. I have removed seperate add.member sections into one but want to be able to choose between whether student or teacher gets added and not have to input both. I assume I have to use a List Array but I have done a lot of reading and don't know where to start. Any help would be great, am very new to Java. Thanks.
Code:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Committee
{
private String name;
private List<Object> members;
public Committee (String name)
{
this.name = name;
members = new ArrayList<Object>();
}
public void addMember(Student student, Teacher teacher)
{
members.add(student);
members.add(teacher);
}
public void printMembership()
{
System.out.println("Membership of the "+name+" Committee ");
Iterator<Object> it = members.iterator();
while (it.hasNext()) {
Object member = it.next();
System.out.println(member);
}
}
}
Re: array split newbie help needed
[When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#']
Cheers.
Re: array split newbie help needed
Quote:
want to be able to choose between whether student or teacher gets added and not have to input both.
Use method overloading for that: One method for student and one for teacher.
Quote:
Method overloading In a class, there can be several methods with the same name. However they must have a different signature.
Re: array split newbie help needed
Quote:
Originally Posted by
bobhope
don't know where to start
Since you've declared the List (and its ArrayList implementation) to hold objects of type Object you can store anything in it. It's because the Object class is the superclass of every other Java class. It's the very top class of the whole Java class hierarchy. It means that technically you could do this,
Code:
public void addMember(Object studentOrTeacherOrWhatever) { // stores anything and everything
members.add(studentOrTeacherOrWhatever);
}
This will work but is not very good Object Oriented (OO) programming. Instead Student and Teacher should share (extend or implement) a common supertype (a class or an interface), say SchoolPerson. The SchoolPerson type would then replace Object in your code. So this is the proper OO way to do it (known as subtype polymorphism),
Code:
private List<SchoolPerson> members;
// ...
members = new ArrayList<SchoolPerson>();
// ...
public void addMember(SchoolPerson person) { // stores any subtype object of SchoolPerson
members.add(person); // such as Student and Teacher
}
When you use the System.out.println() method on an object, Java will use the toString() method of class Object to figure out what to actually print. Then what gets printed is some obscure internal representation of the object and that usually isn't what you want. One way out of this is to override toString() in subclasses and return something more appropriate of your own choosing. But the better OO approach is to define a print method in SchoolPerson which is then overridden in the Student and Teacher subclasses.
Finally rather than using an Iterator to scan a List have a look at the enhanced for-loop. It's much more convenient.