Algorithm to Generate Pay Slip for Employees
Step 1: Start
Step 2: Create the Base Class Employee
Members:
o Emp_name (String)
o Emp_id (String)
o Address (String)
o Mail_id (String)
o Mobile_no (String)
Method:
o readDetails() – to input the employee details.
o displayDetails() – to display the employee details.
Step 3: Create the Derived Class Programmer (inherits from Employee)
Member: BasicPay (float or double)
Constants for percentages:
o DA = 97%, HRA = 10%, PF = 12%, Staff Club Fund = 1%
Method:
o calculateSalary() – compute:
DA = BasicPay * 97 / 100
HRA = BasicPay * 10 / 100
PF = BasicPay * 12 / 100
StaffClub = BasicPay * 1 / 100
Gross = BasicPay + DA + HRA
Net = Gross - (PF + StaffClub)
o generatePaySlip() – display salary details
Step 4: Create Derived Class AssistantProfessor (inherits from Employee)
Member: BasicPay
Constants: DA = 110%, HRA = 20%, PF = 12%, Staff Club = 5%
Use similar calculateSalary() and generatePaySlip() as above
Step 5: Create Derived Class AssociateProfessor (inherits from Employee)
Member: BasicPay
Constants: DA = 130%, HRA = 30%, PF = 12%, Staff Club = 10%
Use similar calculateSalary() and generatePaySlip() as above
Step 6: Create Derived Class Professor (inherits from Employee)
Member: BasicPay
Constants: DA = 140%, HRA = 40%, PF = 12%, Staff Club = 15%
Use similar calculateSalary() and generatePaySlip() as above
Step 7: In the Main Class
Create objects for each designation
Accept employee details and basic pay
Call the respective calculateSalary() and generatePaySlip() functions
Step 8: Stop
import java.util.Scanner;
// Base class
class Employee {
String empName, empId, address, mailId, mobileNo;
double basicPay;
Scanner sc = new Scanner(System.in);
void readDetails() {
System.out.print("Enter Employee Name: ");
empName = sc.nextLine();
System.out.print("Enter Employee ID: ");
empId = sc.nextLine();
System.out.print("Enter Address: ");
address = sc.nextLine();
System.out.print("Enter Mail ID: ");
mailId = sc.nextLine();
System.out.print("Enter Mobile No: ");
mobileNo = sc.nextLine();
System.out.print("Enter Basic Pay: ");
basicPay = sc.nextDouble();
}
void displayDetails() {
System.out.println("\n--- Employee Details ---");
System.out.println("Name : " + empName);
System.out.println("ID : " + empId);
System.out.println("Address : " + address);
System.out.println("Mail ID : " + mailId);
System.out.println("Mobile No : " + mobileNo);
System.out.println("Basic Pay : " + basicPay);
}
}
// Derived class: Programmer
class Programmer extends Employee {
void generatePaySlip() {
double da = basicPay * 97 / 100;
double hra = basicPay * 10 / 100;
double pf = basicPay * 12 / 100;
double staffClub = basicPay * 1 / 100;
double gross = basicPay + da + hra;
double net = gross - (pf + staffClub);
displayDetails();
System.out.println("Designation: Programmer");
System.out.println("DA : " + da);
System.out.println("HRA : " + hra);
System.out.println("PF : " + pf);
System.out.println("Staff Club : " + staffClub);
System.out.println("Gross Pay : " + gross);
System.out.println("Net Pay : " + net);
}
}
// Derived class: Assistant Professor
class AssistantProfessor extends Employee {
void generatePaySlip() {
double da = basicPay * 110 / 100;
double hra = basicPay * 20 / 100;
double pf = basicPay * 12 / 100;
double staffClub = basicPay * 5 / 100;
double gross = basicPay + da + hra;
double net = gross - (pf + staffClub);
displayDetails();
System.out.println("Designation: Assistant Professor");
System.out.println("DA : " + da);
System.out.println("HRA : " + hra);
System.out.println("PF : " + pf);
System.out.println("Staff Club : " + staffClub);
System.out.println("Gross Pay : " + gross);
System.out.println("Net Pay : " + net);
}
}
// Derived class: Associate Professor
class AssociateProfessor extends Employee {
void generatePaySlip() {
double da = basicPay * 130 / 100;
double hra = basicPay * 30 / 100;
double pf = basicPay * 12 / 100;
double staffClub = basicPay * 10 / 100;
double gross = basicPay + da + hra;
double net = gross - (pf + staffClub);
displayDetails();
System.out.println("Designation: Associate Professor");
System.out.println("DA : " + da);
System.out.println("HRA : " + hra);
System.out.println("PF : " + pf);
System.out.println("Staff Club : " + staffClub);
System.out.println("Gross Pay : " + gross);
System.out.println("Net Pay : " + net);
}
}
// Derived class: Professor
class Professor extends Employee {
void generatePaySlip() {
double da = basicPay * 140 / 100;
double hra = basicPay * 40 / 100;
double pf = basicPay * 12 / 100;
double staffClub = basicPay * 15 / 100;
double gross = basicPay + da + hra;
double net = gross - (pf + staffClub);
displayDetails();
System.out.println("Designation: Professor");
System.out.println("DA : " + da);
System.out.println("HRA : " + hra);
System.out.println("PF : " + pf);
System.out.println("Staff Club : " + staffClub);
System.out.println("Gross Pay : " + gross);
System.out.println("Net Pay : " + net);
}
}
// Main class
public class EmployeePaySlipApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Employee Designation:\n1. Programmer\n2. Assistant Professor\n3.
Associate Professor\n4. Professor");
int choice = sc.nextInt();
sc.nextLine(); // consume newline
switch (choice) {
case 1:
Programmer p = new Programmer();
p.readDetails();
p.generatePaySlip();
break;
case 2:
AssistantProfessor ap = new AssistantProfessor();
ap.readDetails();
ap.generatePaySlip();
break;
case 3:
AssociateProfessor asp = new AssociateProfessor();
asp.readDetails();
asp.generatePaySlip();
break;
case 4:
Professor prof = new Professor();
prof.readDetails();
prof.generatePaySlip();
break;
default:
System.out.println("Invalid choice!");
}
sc.close();
}
}