Back Button
Back to Chapter
Introduction
No items found.
Collapse_Icon
Table of Contents

Problem Statement-1:

Write a Java program to perform employee payroll processing using packages. In the java file, Emp.java creates a package employee and creates a class Emp. Declare the variables name,empid, category, bpay, hra, da, npay, pf, grosspay, incometax, and allowance. Calculate the values in methods. Create another java file Emppay.java. Create an object e to call the methods to perform and print values.

Test Data:

Pass name as ANU, id as 23, category as Female and bpay as 12000.

Solution:

//SAVE AS Emp.java
package employee;
public class Emp{
 String name,empid, category;
 int bpay;
 double hra,da,npay,pf,grosspay,incometax,allowance;
 public Emp(String n, String id, String c, int b)
 {
   name = n;
   empid = id;
   category = c;
   bpay = b;
 }
 public void call()
 {

   da = bpay*0.05;
   hra = bpay*0.09;
   pf = bpay*0.11;
   allowance = bpay*0.10;
   grosspay = bpay+da+hra+allowance-pf;
   incometax = 0.75*grosspay;
   npay = grosspay- incometax;
  }

 public void display()
 {
   System.out.println("/n/n Employee Details");
   System.out.println("/n/n Name:"+name);
   System.out.println("/n/n Empid:"+empid);  
   System.out.println("/n/n Category:"+category);      
   System.out.println("/n/n bpay:"+bpay);
   System.out.println("/n/n da:"+da);
   System.out.println("/n/n hra:"+hra);
   System.out.println("/n/n pf:"+pf);
   System.out.println("/n/n all:"+allowance);
   System.out.println("/n/n gs:"+grosspay);
   System.out.println("/n/n Incometax:"+incometax);
   System.out.println("/n/n npay:"+npay);
   }
 }

//SAVE as Emppay.java
import java.io.*;
import employee.Emp;
class Emppay
{

 public static void main(String args[])
 {

   Emp e = new Emp("ANU","23","Female",12000);
   e.call();
   e.display();
   }
 }

/*OUTPUT:
Employee Details
Name: ANU
Empid: 23
Category: Female
bpay: 12000
da: 600.0
hra: 1080.0
pf: 1320.0
allowance: 1200.0
grosspay: 13560.0
Incometax: 10170.0
npay: 3390  */

Explanation:

The package employee is created in the Emp.java file. The class Emp consists of fields name,empid, category, bpay, hra, da, npay, pf, grosspay, incometax, and allowance. The methods call() and display are used to perform calculations and display values. The class Emppay.java is created and the package employee is imported. The object e is created and the values are passed in parameters. The println() method is used to print the content.