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.

associating java classes

Student Class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

publicclassStudent1{

privateStringname;//data fields

privateintid;

Student1(){}// Constructor

// Methods

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetName(){

returnthis.name;

}

publicintgetId(){

returnthis.id;

}

publicvoidsetId(intid){

this.id=id;

}

}

Course Class :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

publicclassCourse{

privateStringcourseName;

privateStudent1[]students;

privateintnumberOfStudents;

publicCourse(StringcourseName){

this.courseName=courseName;

this.numberOfStudents=0;

this.students=newStudent1[20];

}

publicvoidaddStudent(Student1 student){

this.students[numberOfStudents]=student;

this.numberOfStudents++;

}

publicStudent1[]getStudents(){

returnstudents;

}

publicintgetNumberOfStudents(){

returnnumberOfStudents;

}

publicStringgetCourseName(){

returncourseName;

}

publicvoiddropStudent(Student1 student){

intindx=findStudent(student);

for(inti=indx;i<students.length-1;i++){

students[i]=students[i+1];

}

numberOfStudents--;

}

publicintfindStudent(Student1 std){

for(inti=0;i<this.students.length;i++)

if(std.equals(this.students[i]))

returni;

return-1;

}

publicStringtoString(){

Stringstr=" Course Name "+this.courseName+"\n Number of Students "+this.numberOfStudents;