Lab 8
TASK 1:
SOOURCE CODE:
public class Task1 {
public static void main(String[] args) {//Main method to display working of code
//book object creation
Book b1 = new Book();
Book b2 = new Book("My Book",new Author("Jamal","[email protected]",'m'),200,100);
//book attributes output
System.out.println(b1.toString());
System.out.println(b2.toString());
class Book{
//attributes of book
private String bookName;
private Author author;
private double price;
private int quantity;
//constructors
public Book() {
this.bookName = "Book name";
this.author = new Author();
this.price = 0;
this.quantity = 0;
public Book(String name, Author author, double price) {
this.bookName = name;
this.author = author;
this.price = price;
this.quantity = 0;
public Book(String name, Author author, double price, int qty) {
this.bookName = name;
this.author = author;
this.price = price;
this.quantity = qty;
//getters
public String getName() {
return bookName;
}
public Author getAuthor() {
return author;
public int getQuantity() {
return quantity;
public double getPrice() {
return price;
//setters
public void setPrice(double price) {
this.price = price;
public void setQuantity(int qty) {
this.quantity = qty;
public String toString(){//tostring method to show output
return "Book:\nName = " + this.getName() + "\tAuthor = " + this.author.getName() + "\tPrice = " +
this.getPrice() + "\tQuantity = " + this.getQuantity() + "\n";
class Author{
//attributes of authors
private String name;
private String email;
private char gender;
//constructors
public Author() {
this.name = "jamal";
this.email = "[email protected]";
this.gender = 'm';
public Author(String name, String email, char gender) {
this.name = name;
this.email = email;
this.gender = gender;
// getter
public String getName() {
return name;
}
OUTPUT:
TASK 2:
SOOURCE CODE:
public class Task2 {
public static void main(String[] args) {
//facult & student initialize
Faculty faculty = new Faculty("Jamal","xxxxx-xxxx-xxxx-x","[email protected]",03001123321,new
Date(),32,new JobDescription("Profssor",99999,99));
Student student = new Student("Student","xxxxx-xxxx-xxxx-x","[email protected]",0354543234,new
Date(),2019,293988);
//faculty and student output
System.out.println(faculty.toString());
System.out.println(student.toString());
class Date{
//atributes of date
private int date;
private int month;
private int year;
//contructors
public Date() {
this.date = 1;
this.month = 1;
this.year = 2000;
public Date(int date, int month, int year) {
this.date = date;
this.month = month;
this.year = year;
//getters
public int getDate() {
return date;
public int getMonth() {
return month;
public int getYear() {
return year;
}
//tostring method to display output
public String toString() {
return date + "-" + month + "-" + year;
class JobDescription{
//attrivbutes
private String designation;
private int salary;
private double teachingLoad;
//constructors
public JobDescription() {
this.designation = "";
this.salary = 0;
this.teachingLoad = 0;
public JobDescription(String designation, int salary, double teachingLoad) {
this.designation = designation;
this.salary = salary;
this.teachingLoad = teachingLoad;
}
//getters
public String getDesignation() {
return designation;
public int getSalary() {
return salary;
public double getTeachingLoad() {
return teachingLoad;
//setters
public void setDesignation(String designation) {
this.designation = designation;
public void setSalary(int salary) {
this.salary = salary;
public void setTeachingLoad(double teachingLoad) {
this.teachingLoad = teachingLoad;
}
//tostring method to display output
public String toString() {
return "designation=" + designation + '\n'+'\t' +
"\t salary=" + salary +'\n'+'\t'+
"\t teachingLoad=" + teachingLoad
class Person{
//attributes
private String name;
private String cnic;
private String email;
private int contactNumber;
private Date date;
//constructors
public Person() {
this.name = "";
this.cnic = "";
this.email = "";
this.contactNumber = 0;
this.date = new Date();
}
public Person(String name, String cnic, String email, int contactNumber, Date date) {
this.name = name;
this.cnic = cnic;
this.email = email;
this.contactNumber = contactNumber;
this.date = date;
//getters
public String getName() {
return name;
public String getCnic() {
return cnic;
public String getEmail() {
return email;
public int getContactNumber() {
return contactNumber;
public String getDate() {
return date.toString();
}
//setters
public void setName(String name) {
this.name = name;
public void setCnic(String cnic) {
this.cnic = cnic;
public void setEmail(String email) {
this.email = email;
public void setContactNumber(int contactNumber) {
this.contactNumber = contactNumber;
public void setDate(Date date) {
this.date = date;
class Faculty extends Person{
//attributes
private int officeNumber;
private JobDescription jobDescription;
//constructors
public Faculty() {
this.officeNumber = 0;
this.jobDescription = new JobDescription();
public Faculty(int officeNumber, JobDescription jobDescription) {
this.officeNumber = officeNumber;
this.jobDescription = jobDescription;
public Faculty(String name, String cnic, String email, int contactNumber, Date date, int officeNumber,
JobDescription jobDescription) {
super(name, cnic, email, contactNumber, date);
this.officeNumber = officeNumber;
this.jobDescription = jobDescription;
//getters
public int getOfficeNumber() {
return officeNumber;
}
public String getJobDescription() {
return jobDescription.toString();
//setter
public void setOfficeNumber(int officeNumber) {
this.officeNumber = officeNumber;
//toString for output
public String toString() {
return "Faculty:\nName = " + this.getName() + "\nCnic = " + this.getCnic() + "\nEmail = " + this.getEmail() +
"\nContact Number = " + this.getContactNumber() + "\nHiring Date = " + this.getDate() + "\nOffice Number = " +
this.getOfficeNumber() + "\nJob Description = " + this.getJobDescription() + "\n";
class Student extends Person{//child class of class Person
//attributes
private int batch;
private int regNumber;
//constructors
public Student() {
this.batch = 0;
this.regNumber = 0;
}
public Student(int batch, int regNumber) {
this.batch = batch;
this.regNumber = regNumber;
public Student(String name, String cnic, String email, int contactNumber, Date date, int batch, int regNumber)
{
super(name, cnic, email, contactNumber, date);
this.batch = batch;
this.regNumber = regNumber;
//getters
public int getBatch() {
return batch;
public int getRegNumber() {
return regNumber;
//setters
public void setBatch(int batch) {
this.batch = batch;
}
public void setRegNumber(int regNumber) {
this.regNumber = regNumber;
//toString for output
public String toString() {
return "Student:\nName = " + this.getName() + "\nCnic = " + this.getCnic() + "\nEmail = " + this.getEmail() +
"\nContact Number = " + this.getContactNumber() + "\nAdmission Date = " + this.getDate() + "\nBatch = " +
this.getBatch() + "\nRegNo = " + this.getRegNumber() + "\n";
OUTPUT: