Lab 8
Lab 8
Lab 8
Lab no:08
INHERITANCE
1. Develop a registration system for a University. It should consist of three classes namely Student,
Teacher, and Course. For example, a student needs to have a name, roll number, address and GPA to be
eligible for registration. Therefore choose appropriate data types for encapsulating these properties in a
Student objects. Similarly a teacher needs to have name, address, telephone number, and a degree (or a
list of degrees) he/she has received. Finally courses must have a name, students (5 students) registered for
the course, and a teacher assigned to conduct the course. Create an object of Course with 5 Students and a
Teacher. A call to a method, say printDetails(), of the selected course reference should print name of the
course, details of the teacher assigned to that course, and names and roll numbers of the students enrolled
with the course.
Code: Output:
class Student {
private String name;
private int rollNumber;
private String address;
private double GPA;
public Student(String name, int rollNumber, String
address, double GPA) {
this.name = name;
this.rollNumber = rollNumber;
this.address = address;
this.GPA = GPA;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNumber() {
return rollNumber;
}
public void setRollNumber(int rollNumber) {
this.rollNumber = rollNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getGPA() {
return GPA;
}
NAME: Muhammad Shayan Ahmed ROLL NO: 088 SECTION: B
this.students = students;
}
public void printDetails() {
System.out.println("Course Name: " + name);
System.out.println("Teacher Details:");
System.out.println("Name: " + teacher.getName());
System.out.println("Address: " + teacher.getAddress());
System.out.println("Telephone Number: " + teacher.getTelephoneNumber());
System.out.println("Degrees: ");
for (String degree : teacher.getDegrees()) {
System.out.println("- " + degree);
}
System.out.println("Students Enrolled:");
for (Student student : students) {
System.out.println("Name: " + student.getName() + ", Roll Number: " +
student.getRollNumber()+
", Address: " + student.getAddress() + ", GPA: " + student.getGPA());
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
}
public class UniversityRegistrationSystem {
public static void main(String[] args) {
Student[] students = {
new Student("Ali", 1, "Karachi", 3.5),
new Student("Ahmed", 2, "Karachi", 3.2),
new Student("Shezad", 3, "Karachi", 3.8),
new Student("Dawood", 4, "Karachi", 3.6),
new Student("Abdullah", 5, "Karachi", 3.9)
};
NAME: Muhammad Shayan Ahmed ROLL NO: 088 SECTION: B