Person.
java
public class Person {
protected String name;
protected String address;
// Default Constructor
public Person() {
[Link] = "Unknown";
[Link] = "Unknown";
}
// Parameterized Constructor
public Person(String name, String address) {
[Link] = name;
[Link] = address;
}
// Getters & Setters
public String getName() { return name; }
public void setName(String name) { [Link] = name; }
public String getAddress() { return address; }
public void setAddress(String address) { [Link] = address; }
public void displayInfo() {
[Link]("Name : " + name);
[Link]("Address : " + address);
}
}
[Link]
public class Employee extends Person {
protected int emp_no;
protected double pay;
protected double house_rent; // decimal e.g. 0.10 = 10%
protected double medical_allow; // decimal e.g. 0.05 = 5%
// Default Constructor
public Employee() {
super();
this.emp_no = 0;
[Link] = 0;
this.house_rent = 0;
this.medical_allow = 0;
}
// Parameterized Constructor
public Employee(String name, String address, int emp_no,
double pay, double house_rent, double medical_allow) {
super(name, address);
this.emp_no = emp_no;
[Link] = pay;
this.house_rent = house_rent;
this.medical_allow = medical_allow;
}
// calcSalary: pay + house_rent% * pay + medical_allow% * pay
public double calcSalary() {
return pay + (house_rent * pay) + (medical_allow * pay);
}
// Getters & Setters
public int getEmp_no() { return emp_no; }
public void setEmp_no(int emp_no) { this.emp_no = emp_no; }
public double getPay() { return pay; }
public void setPay(double pay) { [Link] = pay; }
public double getHouse_rent() { return house_rent; }
public void setHouse_rent(double hr) { this.house_rent = hr; }
public double getMedical_allow() { return medical_allow; }
public void setMedical_allow(double ma) { this.medical_allow = ma; }
@Override
public void displayInfo() {
[Link]();
[Link]("Emp No : " + emp_no);
[Link]("Basic Pay : " + pay);
[Link]("House Rent % : " + (house_rent * 100) + "%");
[Link]("Medical Allow % : " + (medical_allow * 100) + "%");
[Link]("Net Salary : " + calcSalary());
}
}
[Link]
public class Faculty extends Employee {
protected String designation;
protected String department;
protected int years_of_education;
// Default Constructor
public Faculty() {
super();
[Link] = "Unknown";
[Link] = "Unknown";
this.years_of_education = 0;
}
// Parameterized Constructor
public Faculty(String name, String address, int emp_no,
double pay, double house_rent, double medical_allow,
String designation, String department, int years_of_education) {
super(name, address, emp_no, pay, house_rent, medical_allow);
[Link] = designation;
[Link] = department;
this.years_of_education = years_of_education;
}
// Some amount = years_of_education * 5000
public double getSomeAmount() {
return years_of_education * 5000;
}
// Override calcSalary: Employee salary + some amount
@Override
public double calcSalary() {
return [Link]() + getSomeAmount();
}
// Getters & Setters
public String getDesignation() { return designation; }
public void setDesignation(String designation) { [Link] =
designation; }
public String getDepartment() { return department; }
public void setDepartment(String department) { [Link] = department;
}
public int getYears_of_education() { return years_of_education; }
public void setYears_of_education(int yoe) { this.years_of_education =
yoe; }
@Override
public void displayInfo() {
[Link]();
[Link]("Designation : " + designation);
[Link]("Department : " + department);
[Link]("Years of Education : " + years_of_education);
[Link]("Education Bonus : " + getSomeAmount());
[Link]("Total Salary : " + calcSalary());
}
}
[Link]
public class Main {
public static void main(String[] args) {
// Employee object
Employee emp = new Employee(
"Ali Hassan", "Lahore, Pakistan",
1001, 50000, 0.10, 0.05
);
[Link]("========== EMPLOYEE INFO ==========");
[Link]();
[Link]();
// Faculty object
Faculty fac = new Faculty(
"Dr. Sarah Khan", "Islamabad",
2001, 80000, 0.10, 0.05,
"Associate Professor", "Computer Engineering", 16
);
[Link]("========== FACULTY INFO ==========");
[Link]();
[Link]();
// Calculate salary for faculty instance
[Link]("===== SALARY CALCULATION =====");
[Link]("Employee Salary : " + [Link]());
[Link]("Faculty Salary : " + [Link]());
}
}