Friday 25 October 2013

Inheritance Program 1

/*A class Employee contains employee details and another class Salary calculates the employee’s net
salary. The details of the two classes are given below:
Class name : Employee
Data members
empNo : stores the employee number.
empName : stores the employee name
empDesig : stores the employee’s designation.

Member functions:
Employee() : default constructor.
Employee(…) : parameterized constructor to assign values to data
members.
void display() : display the employee details.

Class name : Salary
Data members
basic : float variable to store the basic pay.

Member functions
Salary(…) : parameterized constructor to assign values to data
members.
void calculate() : calculates the employee’s net salary according to the
following rules:

DA = 10% of basic
 HRA = 15% of basic
 Salary = basic + DA +H RA
 PF= 8 % of Salary
 Net Salary = Salary –PF
 Display the employee details and the Net salary.*/
import java.util.*;
class Employee
{
int empno;String empname,empdesig;
Employee()
{
empno=0;
empname=null;
empdesig=null;
}
Employee(int a,String b,String c)
{
empno=a;
empname=b;
empdesig=c;
}
void display()
{
System.out.println("Employee name:"+empname);
System.out.println("Employee number:"+empno);
System.out.println("Employee designation:"+empdesig);
}
}
class Salary extends Employee
{
float basic;
Salary(float b,int n,String name,String d)
{
super(n,name,d);
basic=b;
}
void calculate()
{
double da=0.1*basic,hra=0.15*basic,gross=basic+da+hra,pf=0.08*gross,netpay=gross-pf;
System.out.println("\nEmployee Details");
System.out.println("----------------");
super.display();
System.out.println("\nPayment Details");
System.out.println("----------------");
System.out.println("Basic="+basic+"\nDA="+da+"\nHRA="+hra+"\nGross Pay="+gross+"\nNet Pay="+netpay);
}
public static void main(String arg[])
{
Scanner ob=new Scanner(System.in);
System.out.print("Enter employee name:");
String name=ob.nextLine ();
System.out.print("Enter employee number:");
int n=ob.nextInt();
System.out.print("Enter employee designation:");
String d=ob.next();
System.out.print("Enter basic pay:");
float b=ob.nextFloat ();
Salary call=new Salary(b,n,name,d);
call.calculate();
}
}

1 comment: