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

OOP Lab Sessional

This document contains Java code for creating Student and Department classes with getters and setters. It also includes code in the main method to create Department objects, add Student objects to departments, and print out the student details for each department. The code takes department and student details as input from the user, creates Department and Student objects, adds students to the relevant department, and finally prints out the details of all departments and their students.

Uploaded by

Adnan Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

OOP Lab Sessional

This document contains Java code for creating Student and Department classes with getters and setters. It also includes code in the main method to create Department objects, add Student objects to departments, and print out the student details for each department. The code takes department and student details as input from the user, creates Department and Student objects, adds students to the relevant department, and finally prints out the details of all departments and their students.

Uploaded by

Adnan Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

COMSATS University Islamabad (Lahore Campus)

Department of Electrical Engineering

Objected Oriented
Programming

Bachelor of Sciences and


Computer Engineering
(BS CE)
Session 2018-22

Submitted To:
Ma’am Muntaha Iqbal

Submitted By:
Adnan Ahmad

Registration No:
FA18-BCE-030

Lab Session 02

Task 01
Student Class:
package studenttestt;

public class Student {


private String id;
private String name;
private Gender gender;
private double CGPA;

public Student(String id, String name, Gender gender, double CGPA) {


this.id = id;
this.name = name;
this.gender = gender;
this.CGPA = CGPA;
}

public enum Gender{


MALE ,
FEMALE;
}

public String getId() {


return id;
}

public void setId(String id) {


this.id = id;
}

public String getName() {


return name;
}
public void setName(String name) {
this.name = name;
}

public Gender getGender() {


return gender;
}

public void setGender(Gender gender) {


this.gender = gender;
}
public double getCGPA() {
return CGPA;
}

public void setCGPA(double CGPA) {


this.CGPA = CGPA;
}
}

Department Class:
package studenttestt;
import java.util.ArrayList;
import java.util.Arrays;
public class Department {

private String departmentid;


private String departmentName;

ArrayList<Student> student;
public Department(String departmentid, String departmentName) {
this.departmentid = departmentid;
this.departmentName = departmentName;
}

public String getDepartmentid() {


return departmentid;
}

public void setDepartmentid(String departmentid) {


this.departmentid = departmentid;
}

public String getDepartmentName() {


return departmentName;
}

public void setDepartmentName(String departmentName) {


this.departmentName = departmentName;
}
}

Task 2:
package studenttestt;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentTestt {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);

//Creating Arrays
int NumberOfDepartments=2;
Department[] departments=new Department[NumberOfDepartments];

for(int i=0 ; i< NumberOfDepartments; i++) {


Department temp = new Department();
System.out.print("Enter Department ID : ");
temp.setDepartmentid(sc.nextLine());
sc.nextLine();
System.out.print("Enter Department Name : ");
temp.setDepartmentName(sc.nextLine());
departments[i] = temp;

ArrayList<Student> tempAl = new ArrayList<>();


System.out.print("Enter number of students : ");
int noOfStudents = sc.nextInt();

for(int j=0 ; j<noOfStudents ; j++) {


System.out.println();
Student tempStudent = new Student();
System.out.print("Enter Student ID : ");
tempStudent.setId(sc.nextLine());
sc.nextLine();
System.out.print("Enter Student Name : ");
tempStudent.setName(sc.nextLine());
System.out.print("Enter Student Gender (Male/Female): ");
String gen = sc.next();
if(gen.equals("male")) {
tempStudent.setGender(Student.Gender.MALE);

} else if(gen.equals("female")) {
tempStudent.setGender(Student.Gender.MALE);

}
System.out.print("Enter Student CGPA : ");
tempStudent.setCGPA(sc.nextDouble());
tempAl.add(tempStudent);
}
departments[i].student = tempAl;
System.out.println("\n");
}

System.out.println(" ");

for (int i = 0; i < NumberOfDepartments; i++) {


System.out.println("Department ID: " + departments[i].getDepartmentid());

System.out.println("Department Name: " + departments[i].getDepartmentName());

System.out.println("Students : ");
for (Student student : departments[i].student) {
System.out.println("\t"+student);

}
System.out.println(" ");

}
}

You might also like