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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
class NetSalary { public static void main(String arg[]) { double grossSalary=10000; double incomeTax=1000; double providentFund=800; double professionalTax=500; double netSalary=grossSalary-(incomeTax+providentFund+professionalTax); System.out.println("Net Salary is="+netSalary); } } |
Output:
1 |
Net Salary is=7700.0 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class NetSalary { public static void main(String arg[]) { double gs,it,pt,pf,netSalary; Scanner sc=new Scanner(System.in); System.out.println("enter Gross salary"); gs=sc.nextDouble(); System.out.println("enter Income Tax %"); it=sc.nextDouble(); System.out.println("enter Professional Tax %"); pt=sc.nextDouble(); System.out.println("enter Provident Fund %"); pf=sc.nextDouble(); pf=pf*(gs/100); it=it*(gs/100); pt=pt*(gs/100); netSalary=gs-it-pt-pf; System.out.println("Net Salary is="+netSalary); } } |
Output:
1 2 3 4 5 6 7 8 9 |
enter Gross salary 2000 enter Income Tax % 10 enter Professional Tax % 10 enter Provident Fund % 10 Net Salary is=1400.0 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Scanner; class NetSalary { public static void main(String arg[]) { double gs,it,pt,pf,netSalary; Scanner sc=new Scanner(System.in); System.out.println("Enter Gross salary"); gs=sc.nextDouble(); System.out.println("Enter Income Tax %"); it=sc.nextDouble(); System.out.println("Enter Professional Tax %"); pt=sc.nextDouble(); System.out.println("enter Provident Fund %"); pf=sc.nextDouble(); netSalary=salary(gs,pf,pt,it); System.out.println("Net Salary is="+netSalary); } static double salary(double gs,double pf,double pt,double it) { pf=pf*(gs/100); it=it*(gs/100); pt=pt*(gs/100); double n=gs-it-pt-pf; return n; } } |
Output:
1 2 3 4 5 6 7 8 9 |
Enter Gross salary 10000 Enter Income Tax % 5 Enter Professional Tax % 5 enter Provident Fund % 5 Net Salary is=8500.0 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class NetSalary { public static void main(String arg[]) { double gs,it,pt,pf,netSalary; gs=Double.parseDouble(arg[0]); System.out.println("Gross salary is = "+gs); it=Double.parseDouble(arg[1]); System.out.println("Income Tax percentage is = "+it); pt=Double.parseDouble(arg[2]); System.out.println("Professional Tax percentage is = "+pt); pf=Double.parseDouble(arg[3]); System.out.println("Provident Fund percentage is = "+pf); pf=pf*(gs/100); it=it*(gs/100); pt=pt*(gs/100); netSalary=gs-it-pt-pf; System.out.println("Net Salary is="+netSalary); } } |