Write a program with an "Employee" class having two variables one for the named employee (String) and other for base salary (float). Create a function that calculates the total salary by adding 50% HRA to the salary and returns. Create an objectof this class in main function and set the name of the employee, call the salary function and print the calculated salary returned by the function with name of the employee.
Answers
Answer:
Include appropriate getters and setters method in Employee class. Write the following method in the Employee class: public void calculateNetSalary(int pfpercentage) - This method should take PF percentage as argument. Deduct the PF amount from the salary and set the netSalary.
Create a Main class which has the main method which invokes the method to get the input and prints the details as shown in the sample.
Also write a method :
public static Employee getEmployeeDetails() - which gets the employee details - id, name and salary, and returns the employee object.
public static int getPFPercentage() - which gets the PF percentage and returns the same
In the main method invoke the above two methods, and then call the calculateNetSalary method in Employee class and print the output as shown below.
Sample Input 1:
Enter Id: 101 Enter Name: Vivek Enter salary: 20000 Enter PF percentage: 7
Sample Output 1:
Id : 101
Name : Vivek
Salary : 20000.0
Net Salary : 18600.0
public class Employee {
String name;
float basicSalary;
Employee(String name, float basicSalary) {
this.name = name;
this.basicSalary = basicSalary;
}
float calculateSalary(float percentageHRA) {
float HRA = (percentageHRA / 100f) * basicSalary;
return (0.5f * HRA) + basicSalary;
}
public static void main(String[ ] args) {
Employee employee = new Employee("Om", 25_000);
System.out.printf("Employee Name - %s%nNet Salary - ₹%f", employee.name, employee.calculateSalary(12.7f));
}
}