Employee

Define a class with following specification.

Class Name: Employee

Member Variables:

n – Employee ID (Contains only numbers)

name – Name of the employee

age – Age of the employee

basic – Basic Salary

Member Methods:

Employee() – Default constructor to initialize member variables.

void getData() – Accept the details using Scanner class.

void getSalary() –

To calculate the net salary as per given specifications.

hra – 15% of basic salary.

da – 17.5% of basic salary.

pf – 7.5% of basic salary.

net salary = basic+hra+da-pf.

Note: If age of employee is above 50 he/she gets additional ₹5000.

void printData() –

To print the details as per given format.

ID Name Age Basic Net

Invoke all the above methods in main() function using object of the class.

Java
import java.util.*;
public class Employee
{
    int n,age;
    String name;
    double basic,net;
    Employee()
    {
        n=0;
        age=0;
        name="";
        basic=0.0;
        net=0.0;
    }
    public void getData()
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Employee ID: ");
        n=sc.nextInt();
        sc.nextLine();
        System.out.print("Name: ");
        name=sc.nextLine();
        System.out.print("Age: ");
        age=sc.nextInt();
        System.out.print("Basic: ");
        basic=sc.nextDouble();
    }
    public void getSalary()
    {
        double hra=0.15*basic;
        double da=0.175*basic;
        double pf=0.075*basic;
        net=basic+hra+da-pf;
        if(age>50)
        {
            net=net+5000;
        }
    }
    public void printData()
    {
        System.out.println("ID\t\tName\t\t\tAge\t\tBasic\t\t\tNet");
        System.out.println(n+"\t\t"+name+"\t\t\t"+age+"\t\t"+basic+"\t\t\t"+net);
    }
    public static void main(String args[])
    {
        Employee ob=new Employee();
        ob.getData();
        ob.getSalary();
        ob.printData();
    }
}
Java