Lab 8

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

NAME: Muhammad Shayan Ahmed ROLL NO: 088 SECTION: B

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

public void setGPA(double GPA) {


this.GPA = GPA;
}
}
class Teacher {
private String name;
private String address;
private String telephoneNumber;
private String[] degrees;
public Teacher(String name, String address, String telephoneNumber, String[] degrees) {
this.name = name;
this.address = address;
this.telephoneNumber = telephoneNumber;
this.degrees = degrees;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(String telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public String[] getDegrees() {
return degrees;
}
public void setDegrees(String[] degrees) {
this.degrees = degrees;
}
}
class Course {
private String name;
private Teacher teacher;
private Student[] students;
public Course(String name, Teacher teacher, Student[] students) {
this.name = name;
this.teacher = teacher;
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

String[] degrees = {"Bachelor of Education", "Master of Science"};


Teacher teacher = new Teacher("Sir Tauseef", "SSUET", "+92 336 2121330", degrees);
Course course = new Course("Java Programming", teacher, students);
course.printDetails();
}
}
2. Create a class called computers and two classes MyComputer and YourComputer which inherits
computer class. Define appropriate features of their processor in the classes. Create another class
processor as a composite class of computer. Write a method which prints the differences between the
processors of two computers.
Code: Output:
class Processor {
private String type;
private double bandwidth;
private String speed;
private String processing;
public Processor(String type, double bandwidth, String speed, String processing) {
this.type = type;
this.bandwidth = bandwidth;
this.speed = speed;
this.processing = processing;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getBandwidth() {
return bandwidth;
}
public void setBandwidth(double bandwidth) {
this.bandwidth = bandwidth;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
public String getProcessing() {
return processing;
}
public void setProcessing(String processing) {
this.processing = processing;
}
}
class Computer {
private Processor processor;
NAME: Muhammad Shayan Ahmed ROLL NO: 088 SECTION: B

public Computer(Processor processor) {


this.processor = processor;
}
public Processor getProcessor() {
return processor;
}
public void setProcessor(Processor processor) {
this.processor = processor;
}
}
class MyComputer extends Computer {

public MyComputer(Processor processor) {


super(processor);
}
}
class YourComputer extends Computer {
public YourComputer(Processor processor) {
super(processor);
}
}
public class ComputerComparison {
public static void printProcessorDifferences(MyComputer myComputer, YourComputer yourComputer) {
Processor myProcessor = myComputer.getProcessor();
Processor yourProcessor = yourComputer.getProcessor();
System.out.println("Processor differences:");
System.out.println("Type:");
System.out.println("MyComputer: " + myProcessor.getType());
System.out.println("YourComputer: " + yourProcessor.getType());
System.out.println("Bandwidth:");
System.out.println("MyComputer: " + myProcessor.getBandwidth() + "GByte/s");
System.out.println("YourComputer: " + yourProcessor.getBandwidth() + "GByte/s");
System.out.println("Speed:");
System.out.println("MyComputer: " + myProcessor.getSpeed());
System.out.println("YourComputer: " + yourProcessor.getSpeed());
System.out.println("Processing:");
System.out.println("MyComputer: " + myProcessor.getProcessing());
System.out.println("YourComputer: " + yourProcessor.getProcessing());
}
public static void main(String[] args) {
Processor myProcessor = new Processor("single core", 125, "slow", "sequentially");
Processor yourProcessor = new Processor("multi core", 250, "fast", "parallel");
MyComputer myComputer = new MyComputer(myProcessor);
YourComputer yourComputer = new YourComputer(yourProcessor);
printProcessorDifferences(myComputer, yourComputer);
}
}

You might also like