Results 1 to 4 of 4
  1. #1
    jcjons is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Class Associations

    So i have two classes Lecturer and Student .
    Now I need an association isCoordinatedBy between them .
    how do I go about this, am using netbeans.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Class Associations

    Hi jcjons, welcome to the forums.

    If by association you want to say a lecturer has many students then a simple array within the Lecturer class would work. You could then implement get and set methods witih this class.
    If a single student however belongs to more than one Lecturer then this may not be the ideal solution.

    Regards.

  3. #3
    jcjons is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: Class Associations

    Sorry I meant Course not Student. In this association there is only ONE courseCoordinator(Lecturer) per Course and a lecturer can have from ZERO to ONE course to coordinate.
    Am trying to implement such that when i create an instance of course a Lecturer is printed and when I create an instance of Lecture NULL or a course he is coordinating is printed.
    Java Code:
    public class Course  {
    
        private String courseName;
        private String courseCode;
        private String program;
        private String courseCoordinator;
        
        public Course(){
            courseName = "";
         courseCode = "";
         program = "";
         courseCoordinator = "";
        }
    
        
        /**
         *
         * @param courseName
         * @param courseCode
         * @param program
         */
        public Course( String courseName, String courseCode, String program, String courseCoordinator){
            
             this .courseName = courseName;
         this.courseCode = courseCode;
         this.program = program;
        }
    
       
        /**
         * @return the courseName
         */
        public String getCourseName() {
            return courseName;
        }
    
        /**
         * @param courseName the courseName to set
         */
        public void setCourseName(String courseName) {
            this.courseName = courseName;
        }
    
        /**
         * @return the courseCode
         */
        public String getCourseCode() {
            return courseCode;
        }
    
        /**
         * @param courseCode the courseCode to set
         */
        public void setCourseCode(String courseCode) {
            this.courseCode = courseCode;
        }
    
        /**
         * @return the program
         */
        public String getProgram() {
            return program;
        }
    
        /**
         * @param programme the program to set
         */
        public void setProgramme(String program) {
            this.program = program;
        }
         
        /**
         * @return the courseCoordinator
         */
        public String getCourseCoordinator() {
            return courseCoordinator;
        }
    
        /**
         * @param courseCoordinator the courseCoordinator to set
         */
        public void setCourseCoordinator(String courseCoordinator) {
           this.courseCoordinator = courseCoordinator;
        }
        
        
        /**
         *
         * @param courseName
         * @param courseCode
         * @param program
         */
        public void print(String courseName, String courseCode, String program, String courseCoordinator){
            
            System.out.println("Course Name: " + getCourseName() 
                    + "| Course Code: " + getCourseCode() 
                    + "| Program: " + getProgram()
                    + "| Course Coordinator: " + getCourseCoordinator());
        }
       
    public void   addCordinator(Lecturer newLecturer){
       add(newLecturer);
    }
    }
    
    
    public class Lecturer {
    
           private String lecturerName;
        private String lecturerID;
        private int officeNumber;
    
        public Lecturer() {
    
            lecturerName = "";
            lecturerID = "";
            officeNumber = 0;
        }
    public Lecturer(String lecturerID, String lecturerName, int officeNumber){
        this.lecturerName = lecturerName ;
            this.lecturerID = lecturerID ;
            this.officeNumber = officeNumber;
    }
        /**
         * @return the lecturerName
         */
        public String getLecturerName() {
            return lecturerName;
        }
    
        /**
         * @param lecturerName the lecturerName to set
         */
        public void setLecturerName(String lecturerName) {
            this.lecturerName = lecturerName;
        }
    
        /**
         * @return the lecturerID
         */
        public String getLecturerID() {
            return lecturerID;
        }
    
        /**
         * @param lecturerID the lecturerID to set
         */
        public void setLecturerID(String lecturerID) {
            this.lecturerID = lecturerID;
        }
    
        /**
         * @return the officeNumber
         */
        public int getOfficeNumber() {
            return officeNumber;
        }
    
        /**
         * @param officeNumber the officeNumber to set
         */
        public void setOfficeNumber(int officeNumber) {
            this.officeNumber = officeNumber;
        }
        
          /**
         *
         * @param lecturerName
         * @param lecturerID
         * @param officeNumber
         */
        public void print(String lecturerName, String lecturerID, int officeNumber){
            
               System.out.println("Lecturer Name: " + getLecturerName() + "|  lecturer ID: " + getLecturerID() + "|  Office Number: " + getOfficeNumber());
        }
    }
    
    public class SMSMain {
        //private static int i;
    
    
        public static void main(String[] args) {
    
    
            Student sOne = new Student();
            sOne.setStudentNumber("211008214");
            sOne.setStudentName("Himunyanga .J");
            sOne.setGender(false);
    
    
            Lecturer lOne = new Lecturer("Onwordi .M", "521989", 405);
    
    
            Course cOne = new Course();
            cOne.setCourseName("Object Oriented Programming");
            cOne.setCourseCode("OOP210s");
            cOne.setProgramme("Information Tecnology");
            cOne.addCordinator(lOne);
            cOne.setCourseCoordinator("");
    
    
            lOne.print("", "", 0);
            cOne.print("", "", "", "");
    Last edited by JosAH; 04-05-2013 at 05:35 PM. Reason: added [code] ... [/code] tags

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Class Associations

    Something like that would go against OO. I suppose it could be possible but then how would you iterate through the Lecturers / Courses? To do this class performing the iteration would need to know about both Lecturers and Courses which to me would result in two arrays or using the instanceOf keyword within a single array.
    Each time you wanted to update a connection you would need to make two connections, one from Lecturer to Course and one from Course to Lecturer. This could get messy if you perform one connection but not the second.
    If the connection is stored in each instance as the actual object of the second then statements like the following could be a possibility:
    Java Code:
    Course.getLecturer().getCourse().getLecturer()...
    An alternative method would be to store the string value of the second instance within the first. Beyond this you could start looking at DBs.

    To simplify things it is worth reconsidering the overall focus of the application. Does it hold a record of Lecturers with details on which courses they run or does it hold a list of courses with details on which Lecturers run the courses?

    Regards.

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2012, 03:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •