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:
- Dearness Allowance = 15% of salary
- House Rent Allowance = 10% of salary
- Provident Fund = 12% of salary
- Gross Salary = Salary + Dearness Allowance + House Rent Allowance
- 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.
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
Related Questions
DTDC a courier company charges for the courier based on the weight of the parcel. Define a class with the following specifications:
Class name: courier
Member variables:
name – name of the customer
weight – weight of the parcel in kilograms
address – address of the recipient
bill – amount to be paid
type – 'D'- domestic, 'I'- international
Member methods:
void accept ( ) — to accept the details using the methods of the Scanner class only.
void calculate ( ) — to calculate the bill as per the following criteria:
Weight in Kgs Rate per Kg First 5 Kgs Rs.800 Next 5 Kgs Rs.700 Above 10 Kgs Rs.500 An additional amount of Rs.1500 is charged if the type of the courier is I (International)
void print ( ) — To print the details
void main ( ) — to create an object of the class and invoke the methods