Lecturer 65374 has 2 students:
Student number 1 has name of Patrick
Student number 2 has name of Jay
Below are the Lecturer and Student scripts for which I need to create a TestProgram:
Expand|Select|Wrap|Line Numbers
- import java.util.*;
- public class Lecturer // Lecturer class to illustrate 1 to many association with Student
- { // attributes................................................(iii)
- private int id;
- private String subject;
- private Vector students; // implement student association with Vector class
- private Lecturer(int anId, String aSubject) // constructor
- { setId(anId);
- setSubject(aSubject);
- students = new Vector(10); //start with Vector for 10 students
- } //.......................................................(iv)
- public void addStudentToLecturer(Student aStudent) // custom method addStudentToLecturer
- { students.addElement(aStudent); //........................(ii)
- aStudent.setLecturer(this); // connect student to lecturer (1..1)
- }
- public Vector getStudents() // custom method to return vector of students.......(i)
- { return students;}
- public void setId(int anId) // set accessor methods
- { id = anId;}
- public void setSubject(String aSubject)
- { subject = aSubject;}
- public int getId() // get accessor methods
- { return id;}
- public String getSubject()
- { return subject;}
- public class Student // Student with reference variable and accessors
- { private int no; //attributes
- private String name;
- private Lecturer lecturer;
- public Student(int aNo, String aName, Lecturer aLecturer) //constructor with 2 parameters plus lecturer reference
- { setNo(aNo); //invoke accessors to populate attributes
- setName(aName);
- setLecturer(aLecturer);
- lecturer.addStudentToLecturer(this); //tell lecturer to associate with this student
- }
- public void setNo(int aNo) //set accessor methods
- { no = aNo;}
- public void setName(String aName)
- { name = aName;}
- public void setLecturer(Lecturer aLecturer)
- { lecturer = aLecturer;}
- public int getNo() //get accessor methods
- { return no;}
- public String getName()
- { return name;}
- public Lecturer getLecturer()
- { return lecturer;}
- }