Beginners Programs :

Java Program To Calculate Salary Of An Employee | 3 Ways

Java Program To Calculate Salary of An Employee – In this article, we will detail in on all the methods used to calculate the net salary of an employee in Java Code.  Suitable examples and sample programs have been included in order to make you understand simply. Employee program in Java.

The methods used in this article are as follows:

  • Using Standard Method
  • Using Scanner Class
  • Using Static Method
  • Using Command Line Arguments
  • Using Separate Class

Net Salary, as we all know, is the amount of money that an employee takes home after having all of the deductions cleared and finished at the end of a month.

Usually, people have a few deductions going on with their gross salaries.

java program to calculate employee salary

As you can see in the image, CTC or Cost To Company is the price that a company is spending on you. Out of that, come out several branches. Deducting one after the other we land at the Net Salary.

The formula for Net Salary is as follows:

Net Salary = Gross Salary – Income Tax – Public Provident Fund – Professional Tax

Thus, at the end of the day, the salary in-hand is not exactly what you work for in your workplace.

Hence, the various methods to calculate the net salary in Java Programming are as follows:

Gross Salary Of Employee Java Code

The easiest way to find the net salary of employee is by first taking the values of gross salary, income tax, provident fund and professional tax in the main function and directly use the below formula. 

Net Salary = Gross Salary – Income Tax – Public Provident Fund – Professional Tax

This is a simple arithmetic calculation that will give you your desired result i.e., the net salary or salary-in-hand. This can then be printed in the console screen.

Output:

Using Scanner Class

In the above scenario, we had seen that the values have already been given in the code itself and if one were to change these values they have to go and make changes in the code.

This is not very convenient at a user’s perspective.

So to read the input, we use the Scanner Class. The inputs for this program are gross salary (gs), income tax (it), professional tax (pt) and public provident fund (pf).

So using this, we read the input at run time after which the same calculations are performed.

A small difference here is the income tax, public provident fund and professional tax are taken as percentage of gross salary in the input. So first, the amount is calculated using

Amount=Percentage*GrossSalary/100 and then the below formula is used.

Net Salary = Gross Salary – Income Tax – Public Provident Fund – Professional Tax

This is a simple arithmetic calculation that will give you your desired result i.e., the net salary or salary-in-hand. This can then be printed in the console screen.

Output:

Employee Program in Java – Static Method

Static method is a method in Java which are referenced with the class name itself and do not require separate object to reference it.

For the given problem, static method can be used as a block which will be consisting of the main logic used to derive at the net salary of the employee.

This is the block where the same logic used in the example of Scanner Class is used with the same arithmetic operation to be performed.

This static class named ‘salary’ is then called in the main class. As it can be observed that, the return type of static method is double which is precisely the same as that of our desired net salary.

This method first finds the exact amount of income tax, professional tax and provident fund. After obtaining these, it finds the net salary using

Net Salary = Gross Salary – Income Tax – Public Provident Fund – Professional Tax

Output:

Using Command – Line Arguments

Command line arguments are where we can give input in the form of argument in the command prompt itself while running the java code.

We can directly the values as arguments in command line while running the code with space in between.

The first argument arg[0] is taken as the gross salary, second argument arg[1] is taken as the income tax percentage, third argument arg[2] is taken as the professional tax percentage and the fourth argument arg[3] is taken as the provident fund percentage.

After getting the input, the same operations as above are performed to arrive at our desired result i.e., net salary.