19 Nov 2014
Association in java classes
Association :
Association is a general binary relationship that describes an activity between two classes. For example, a student taking a course is an association between the Student class and the Course class, and a faculty member teaching a course is an association between the Faculty class and the Course class. The following Student1 class and Class are designed to associate them with each other in such a way that Course class can handle the information of all students registered in a course by creating an array of Student class object.
Student Class
public class Student1 { private String name; //data fields private int id; Student1(){} // Constructor // Methods public void setName(String name){ this.name= name; } public String getName(){ return this.name; } public int getId(){ return this.id; } public void setId(int id){ this.id=id; } }
Course Class :
public class Course { private String courseName; private Student1[] students; private int numberOfStudents; public Course(String courseName) { this.courseName= courseName; this.numberOfStudents = 0; this.students = new Student1[20]; } public void addStudent(Student1 student) { this.students[numberOfStudents] = student; this.numberOfStudents++; } public Student1[] getStudents() { return students; } public int getNumberOfStudents() { return numberOfStudents; } public String getCourseName() { return courseName; } public void dropStudent(Student1 student) { int indx = findStudent(student); for(int i=indx; i