Computer Applications

A class Employee contains the following member:

Class name : Employee

Data members/Instance variables
String ename : To store the name of employee
int ecode : To store the employee code
double basicpay : To store the basic pay of employee

Member functions/Methods
Employee( - - - - ) : An argumented constructor to assign name, employee code and basic salary to data members/instance variables
double salCalc( ) : To compute and return the total salary of an employee
void display( ) : To display ename, ecode, basicpay and calculated salary

The salary is calculated according to the following rules :

Salary = Basic pay + HRA + DA + TA
where, HRA = 20% of basic pay
DA = 25% of basic pay
TA = 10% of basic pay

if the ecode <= 100, then a special allowance (20% of salary) will be added and the maximum amount for special allowance will be 2500.

if the ecode > 100 then the special allowance will be 1000.

Hence, the total salary for the employee will calculated as :

Total Salary = Salary + Special Allowance

Specify the class Employee giving the details of the constructor, double salCalc() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.

Java

Java Classes

15 Likes

Answer

import java.util.Scanner;

public class Employee
{
    private String ename;
    private int ecode;
    private double basicpay;
    
    public Employee(String name,
                    int code, 
                    double sal) {
        ename = name;
        ecode = code;
        basicpay = sal;
    }
        
    public double salCalc() {
        double hra = 20.0/100.0 * basicpay;
        double da = 25.0/100.0 * basicpay;
        double ta = 10.0/100.0 * basicpay;
        double allowance;
        double sal = basicpay + hra + da + ta;
        if (ecode <= 100)   {
            allowance = 20.0/100.0 * sal;
            if(allowance > 2500)
                allowance = 2500;
        }
        else {
            allowance = 1000;
        }
        
        double tsal = sal + allowance;
        return tsal;
    }
        
    public void display()   {
        double totalsal = salCalc();
        System.out.println("Name : " + ename);
        System.out.println("Employee Code : " + ecode);
        System.out.println("Basic Pay : Rs. " + basicpay);
        System.out.println("Salary : " + totalsal);   
    }
        
    public static void main()   {
        Scanner in = new Scanner(System.in);
        double totalsal;
        System.out.print("Enter Name : ");
        String name = in.nextLine();
        System.out.print("Enter Employee Code : ");
        int empcode = in.nextInt();
        System.out.print("Enter Basic Pay : ");
        double basicsal = in.nextDouble();
        Employee obj = new Employee(name, 
                                    empcode, 
                                    basicsal);
        obj.display();
    }
}

Output

Answered By

8 Likes


Related Questions