0% found this document useful (0 votes)
15 views

Class Employee

Uploaded by

ayushisingh2186
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Class Employee

Uploaded by

ayushisingh2186
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

class Employee {

String id;
String name;
String designation;
double basicSalary;
double da;
double hra;
double totalSalary;

// Constructor to initialize employee details and calculate total salary


Employee(String id, String name, String designation, double basicSalary) {
this.id = id;
this.name = name;
this.designation = designation;
this.basicSalary = basicSalary;
calculateSalary();
}

// Method to calculate DA, HRA, and Total Salary


void calculateSalary() {
this.da = 0.10 * basicSalary; // DA is 10% of basic salary
this.hra = 0.15 * basicSalary; // HRA is 15% of basic salary
this.totalSalary = basicSalary + da + hra;
}

// Method to display employee information


void display() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Designation: " + designation);
System.out.println("Basic Salary: " + basicSalary);
System.out.println("DA: " + da);
System.out.println("HRA: " + hra);
System.out.println("Total Salary: " + totalSalary);
}

public static void main(String[] args) {


// Creating an employee object with given details
Employee emp = new Employee("E001", "Rahul", "S. Developer", 1500000.0);

// Displaying the employee information


emp.display();
}
}

Explanation:

1. Constructor: The constructor initializes the employee’s ID, name, designation, and basic
salary, then calls calculateSalary() to compute DA, HRA, and total salary.
2. calculateSalary(): This method calculates DA as 10% of the basic salary, HRA as 15% of the
basic salary, and then computes the total salary as the sum of the basic salary, DA, and
HRA.

3. display(): This method prints out all the employee details including ID, name, designation,
basic salary, DA, HRA, and total salary.

You might also like