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
Write a program to accept 10 different decimal numbers (double data type) in a Single Dimensional Array (say, A). Truncate the fractional part of each number of the array A and store their integer part in another array (say, B).
Write a program to print the following patterns.
(i)
5 4 5 3 4 5 2 3 4 5 1 2 3 4 5(ii)
J I H G F E D C B ADefine a class StringMinMax to find the smallest and the largest word present in the string.
E.g.
Input:
Hello this is wow worldOutput:
Smallest word: is
Largest word: HelloWrite a program to accept the year of graduation from school as an integer value from the user. Using the binary search technique on the sorted array of integers given below, output the message "Record exists" if the value input is located in the array. If not, output the message "Record does not exist".
Sample Input:n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] 1982 1987 1993 1996 1999 2003 2006 2007 2009 2010