JAVA Program 1
JAVA Program 1
2
AIM: Implementing many-to-many relationships using classes and objects for model class
with an understanding of packages
THEORY: In the realm of object-oriented programming, let's delve into the theoretical
implementation of a many-to-many relationship between teachers and subjects. This
arrangement encapsulates the scenario where a teacher may impart knowledge across
multiple subjects, and conversely, a subject might be taught by various teachers. Here's a
comprehensive breakdown of key aspects involved in this conceptualization:
1. Classes and Objects: Each entity in the many-to-many relationship is represented by a
class. For example, if we have two entities, say Teachers and Subjects, we would create
classes named Teacher and Subject.
2. Many-to-Many Relationship: In a many-to-many relationship between Teachers and
Courses, each teacher can enroll in multiple courses, and each course can have multiple
teachers enrolled. This relationship is typically represented using object references or
collections in the participating classes.
3. Understanding Packages: Packages are used to organize classes into namespaces. They
help in avoiding naming conflicts and make it easier to manage large projects. You can create
packages for your classes related to the many-to-many relationship to keep them organized.
4. Setters and Getters: Setters (mutator methods) and getters (accessor methods) are used
to modify and retrieve the values of the attributes (fields) of objects respectively. They are
crucial for encapsulation, allowing controlled access to the object's data. In the context of
many-to-many relationships, setters and getters might be used to manage the enrollment of
teachers in courses and vice versa.
5. Object Arrays: Object arrays can be used to store multiple instances of objects. In the
context of many-to-many relationships, you might use arrays or other collection types (e.g.,
lists, sets) to store the enrolled teachers in a course and the courses that a teacher is
enrolled in.
/**
* Identification comments:
* Name: Shreya Gokhale
* Experiment No: 02
* Experiment Title: Implementing many-to-many relationships using classes and objects for
model class with an understanding of packages
* Experiment Date:
*
*
* Beginning comments:
* Filename: Teacher.java
* @author: Shreya Gokhale
* Overview: This is the Teacher class. In this file we have achieved the following
* - Created Attributes
* --- int teacher_id
* --- String teacher_name
* --- String email_id
* --- String address
* --- String contact_number
* ---int age;
package Model.Teachers;
/**
* Identification comments:
* Name: Shreya Gokhale
* Experiment No: 02
* Experiment Title: Implementing many-to-many relationships using classes and objects for
model class
* with an understanding of packages
* Experiment Date:
*
*
* Beginning comments:
* Filename: Subject.java
* @author: Shreya Gokhale
* Overview: This is the Subject class. In this file we have achieved the following
* - Created Attributes
* --- int subj_id
* --- String sub_name
* --- String sub_duration
* --- String sub_credits
*
* - Created Setters and Getters
* - Created a Display Function to call the getters and display Instance data
*
*/
package Model.Subjects;
/**
* Identification comments:
* Name: Shreya Gokhale
* Experiment No: 02
* Experiment Title: Implementing many-to-many relationships using classes and objects for
model class
* * with an understanding of packages
* Experiment Date:
*
* Beginning comments:
* Filename: Main.java
* @author: Shreya Gokhale
* Overview: This is the main class used to created objects for Student Class and Course
Class. In this file we have achieved the following
* - Object Creation
* - Calling Setters and Getters
* - Creating Object Arrays
* - Looping through Object Array
* - Displaying contents of Object Array
*
*/
import Model.Teachers.Teacher;
import Model.Subjects.Subject;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Using For Loop to Take user input for each teacher object
for (int i=1;i<=arr.length;i++)
{
System.out.println("Enter Teacher's Id: ");
int id = sc.nextInt();
arr[i].setTeacher_id(id);
// To avoid unnecessary skip of input
sc.nextLine();
//Using For Loop to Take user input for each Subject object
for (int j = 1; j <= Subject_arr.length; j++) {
System.out.println("Enter Subject Id: ");
int id = sc.nextInt();
Subject_arr[i].setSub_id(id);
// To avoid unnecessary skip of input
sc.nextLine();