Define a class employee with the following specification :
- Members are : empno of type integer, ename of type String, basic, hr and da are of type float, netpay of type float.
- Member functions :
- Calculate() : A function to find basix+hra+da with the float return type.
- havedata() : function to accept values for empno, ename, basic, hra, da and invoke calculate() to calculate netpay.
- dispdata() : function to display all the data members.
Solution
import java.util.*;
class EMPLOYEE
{
int empno;
String ename;
float basic, hra, da, netpay;
public float Calculate(){
return basic+hra+da;
}
public void havedata(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Employee Number : ");
empno=sc.nextInt();
System.out.println("Enter Name : ");
ename=sc.next();
System.out.println("Enter Basic, HRA and DA : ");
basic=sc.nextFloat();
hra=sc.nextFloat();
da=sc.nextFloat();
netpay=Calculate();
}
public void dispdata(){
System.out.println("Employee Number : "+empno);
System.out.println("Employee Name : "+ename);
System.out.println("Basic : "+basic);
System.out.println("HRA : "+hra);
System.out.println("DA : "+da);
System.out.println("Net Pay : "+netpay);
}
}
class EmpDetails
{
public static void main(String args[])
{
EMPLOYEE ob=new EMPLOYEE();
ob.havedata();
ob.dispdata();
}
}
Save the above Program with “EmpDetails.Java” then compile it with the following commands:
javac EmpDetails.java //to compile the code java EmpDetails // to run the code
Output is
Enter Employee Number : 105 Enter Name : Sunny Enter Basic, HRA and DA : 14000 5000 7500 Employee Number : 105 Employee Name : Sunny Basic : 14000.0 HRA : 5000.0 DA : 7500.0 Net Pay : 26500.0