Computer Applications

Define a class named Pay with the following description:

Instance variables /Data members:

String name — To store the name of the employee

double salary — To store the salary of the employee

double da — To store Dearness Allowance

double hra — To store House Rent Allowance

double pf — To store Provident Fund

double grossSal — To store Gross Salary

double netSal — To store Net Salary

Member methods:

Pay(String n, double s) — Parameterized constructor to initialize the data members.

void calculate() — To calculate the following salary components:

  1. Dearness Allowance = 15% of salary
  2. House Rent Allowance = 10% of salary
  3. Provident Fund = 12% of salary
  4. Gross Salary = Salary + Dearness Allowance + House Rent Allowance
  5. Net Salary = Gross Salary - Provident Fund

void display() — To display the employee name, salary and all salary components.

Write a main method to create object of the class and call the member methods.

Java

Java Classes

29 Likes

Answer

import java.util.Scanner;

public class Pay
{
    String name;
    double salary;
    double da;
    double hra;
    double pf;
    double grossSal;
    double netSal;
    
    public Pay(String n, double s) {
        name = n;
        salary = s;
        da = 0;
        hra = 0;
        pf = 0;
        grossSal = 0;
        netSal = 0;
    }
    
    void calculate() {
        da = salary * 15.0 / 100;
        hra = salary * 10.0 / 100;
        pf = salary * 12.0 / 100;
        grossSal = salary + da + hra;
        netSal = grossSal - pf;
    }
    
    void display() {
        System.out.println("Employee Name: " + name);
        System.out.println("Salary: " + salary);
        System.out.println("Dearness Allowance: " + da);
        System.out.println("House Rent Allowance: " + hra);
        System.out.println("Provident Fund: " + pf);
        System.out.println("Gross Salary: " + grossSal);
        System.out.println("Net Salary: " + netSal);
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Employee Name: ");
        String empName = in.nextLine();
        System.out.print("Enter Salary: ");
        double empSal = in.nextDouble();
        
        Pay obj = new Pay(empName, empSal);
        obj.calculate();
        obj.display();
    }
}

Output

Answered By

12 Likes


Related Questions