0% found this document useful (0 votes)
9 views

JAVA Program 1

JAVA Program

Uploaded by

Shreya Gokhale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

JAVA Program 1

JAVA Program

Uploaded by

Shreya Gokhale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

EXPERIMENT NO.

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;

* - Created Setters and Getters


* - Created a Display Function to call the getters and display Instance data
*
*/

package Model.Teachers;

public class Teacher {


String teacher_name,address,email_id,qualification,contact_number;
int age,teacher_id;

public void setTeacher_id(int teacher_id) {


this.teacher_id = teacher_id;
}
public void setTeacher_name(String teacher_name) {
this.teacher_name = teacher_name;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public void setEmail_id(String email_id) {
this.email_id = email_id;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}

public void setContact_number(String contact_number) {this.contact_number =


contact_number;}

public int getTeacher_id() {return teacher_id;}


public String getTeacher_name() {
return teacher_name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
public String getEmail_id() {
return email_id;
}
public String getQualification() {
return qualification;
}

public String getContact_number() {


return contact_number;
}

public void display()


{
System.out.println("Teacher ID: "+getTeacher_id());
System.out.println("Name: "+getTeacher_name());
System.out.println("Age: "+getAge());
System.out.println("Address: "+getAddress());
System.out.println("Email id: "+getEmail_id());
System.out.println("Qualification: "+getQualification());
System.out.println("Contact number: "+getContact_number());
}
}

/**
* 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;

public class Subject {


int sub_id;
String sub_name;
String sub_duration;
int sub_credits;
public void setSub_id(int sub_id) {
this.sub_id = sub_id;
}
public void setSub_name(String Sub_name) {
this.sub_name = sub_name;
}
public void setSub_duration(String Sub_duration) {
this.sub_duration = sub_duration;
}
public void setSub_credits(int Sub_credits) {
this.sub_credits = sub_credits;
}

public int getSub_id() {


return sub_id;
}

public String getSub_name() {


return sub_name;
}

public String getSub_duration() {


return sub_duration;
}

public int getSub_credits() {


return sub_credits;
}

public void display()


{
System.out.println("Subject Id: "+getSub_id());
System.out.println("Subject Name: "+getSub_name());
System.out.println("Subject Duration: "+getSub_duration());
System.out.println("Subject Credits: "+getSub_credits());
}
}

/**
* 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) {

//Creating a single Object


Teacher t1 = new Teacher();

// Using setters to set the values


t1.setTeacher_id(1);
t1.setTeacher_name("Jock Zonfrillo");
t1.setAge(30);
t1.setAddress("Mumbai");
t1.setEmail_id("[email protected]");
t1.setQualification("B.Ed");
t1.setContact_number("8879548898");

//Display teacher's data


t1.display();

//Creating teacher object array


Teacher arr[] = new Teacher[3];
arr[0] = new Teacher();
arr[1] = new Teacher();
arr[2] = new Teacher();

//Creating a Scanner Object to take input


Scanner sc = new Scanner(System.in);

//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();

System.out.println("Enter Teacher's Name: ");


String name = sc.nextLine();
arr[i].setTeacher_name(name);

System.out.println("Enter Teacher's age: ");


int age = sc.nextInt();
arr[i].setAge(age);
// To avoid unnecessary skip of input
sc.nextLine();

System.out.println("Enter Email ID: ");


String email = sc.nextLine();
arr[i].setEmail_id(email);

System.out.println("Enter Address: ");


String address = sc.nextLine();
arr[i].setAddress(address);

System.out.println("Enter Qualification: ");


String qualification = sc.nextLine();
arr[i].setQualification(qualification);
System.out.println("Enter Contact number: ");
String contact_number = sc.nextLine();
arr[i].setContact_number(contact_number);

//Using For Loop to display each teacher object data


for (int i=1;i<=arr.length;i++) {

System.out.println("Teacher No: " + i);


arr[i].display();
System.out.println("");

Subject s1 = new Subject();

// Using setters to set the values


s1.setSub_id(1);
s1.setSub_name("Programming");
s1.setSub_duration("35 Hours");
s1.setSub_credits(3);

//Display subject data


s1.display();

//Creating Subject object array


Subject Subject_arr[] = new Subject[3];
Subject_arr[0] = new Subject();
Subject_arr[1] = new Subject();
Subject_arr[2] = new Subject();

//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();

System.out.println("Enter Subject Name: ");


String name = sc.nextLine();
Subject_arr[i].setSub_name(name);

System.out.println("Enter Subject Duration: ");


String dura = sc.nextLine();
Subject_arr[i].setSub_duration(dura);

System.out.println("Enter Subject Credits: ");


int cred = sc.nextInt();
Subject_arr[i].setSub_credits(cred);

//Using For Loop to display each Subject object data


for (int j = 1; j <= Subject_arr.length; j++) {
System.out.println("Subject No: " + i);
Subject_arr[i].display();
System.out.println("________________________");
}
}}}
OUTPUT:

You might also like