1.
Many-To-Many Association:-
It is a relationship between entity
classes, where multiple instance of an entity should mapped with multiple
instance of another entity.
Ex:-
Multiple students have joined with multiple
Courses.
Ex code:-
class Course
{
String
cid;
String
cname;
int
ccost;
Course(String
cid, String cname, int ccost)
{
this.cid=cid;
this.cname=cname;
this.ccost=ccost;
}
}
class Student
{
String
sid;
String
sname;
String
saddr;
Course[]
crs;
Student(String
sid, String sname, String saddr, Course[] crs)
{
this.sid=sid;
this.sname=sname;
this.saddr=saddr;
this.crs=crs;
}
public
void getStudentDetails()
{
System.out.println("Student
Details");
System.out.println("--------------------");
System.out.println("Student
Id :"+sid);
System.out.println("Student
Name :"+sname);
System.out.println("Student
Address :"+saddr);
System.out.println("CID\tCNAME\tCCOST");
System.out.println("----------------------");
for(int
i=0;i<crs.length;i++)
{
Course
c=crs[i];
System.out.println(c.cid+"\t"+c.cname+"\t"+c.ccost);
}
System.out.println();
}
}
class ManyToManyEx
{
public
static void main(String[] args)
{
Course
c1=new Course("C-111","C",1000);
Course
c2=new Course("C-222","C++",2000);
Course
c3=new Course("C-333","JAVA",5000);
Course[]
crs={c1,c2,c3};
Student
std1=new Student("S-111","AAA","Hyd",crs);
Student
std2=new Student("S-222","BBB","Hyd",crs);
Student
std3=new Student("S-333","CCC","Hyd",crs);
std1.getStudentDetails();
std2.getStudentDetails();
std3.getStudentDetails();
}
}
Output :-
Student Details
---------------
Student Id :S-111
Student Name :AAA
Student Address:Hyd
CID CNAME CCOST
---------------
C-111 C 1000
C-222 C++ 2000
C-333 JAVA5000
Student Details
---------------
Student Id :S-222
Student Name :BBB
Student Address:Hyd
CID CNAME CCOST
---------------
C-111 C 1000
C-222 C++ 2000
C-333 JAVA5000
Student Details
---------------
Student Id :S-333
Student Name :CCC
Student Address:Hyd
CID CNAME CCOST
---------------
C-111 C 1000
C-222 C++ 2000
C-333 JAVA5000
Data Flow of
execution :-


No comments:
Post a Comment