Saturday, 8 August 2015

S5P1

Write a program which defines base class Employee having three data members, namely name[30], emp_numb and gender and two methods namely input_data() and show_data(). Derive a class SalariedEmployee from Employee and adds a new data member, namely salary. It also adds two member methods, namely allowance (if gender is female HRA=0.1 *salary else 0.09* salary. DA= 0.05*salary) and increment (salary = salary+0.1*salary). Display the gross salary in main class. (Tip: Use super to call base class’s constructor).


Code:
 class Employee  
 {  
      int emp_numb;  
      String name;  
      String gender;  
      Employee(int e, String n, String g)  
      {  
           input_data(e,n,g);  
      }  
      void input_data(int e1, String n1, String g1)  
      {  
           emp_numb = e1;  
           name = n1.toUpperCase();  
           gender = g1.toUpperCase();  
           show_data();  
      }  
      void show_data()  
      {  
           System.out.println("---------------------------------------------------");  
           System.out.println("Emp_num: "+emp_numb+"\tName: "+name+"\tGender: "+gender);  
      }  
 }  
 class SalariedEmployee extends Employee  
 {  
      double salary,HRA,DA;  
      SalariedEmployee(int e3, String n3, String g3, double s)  
      {  
           super(e3,n3,g3);  
           salary = s;  
      }  
      double allowance()  
      {  
           DA = (0.05)*salary;  
           if(gender.equals("FEMALE"))  
           {  
                HRA = (0.1)*salary;  
           }  
           else  
           {  
                HRA = (0.09)*salary;  
           }  
           return (HRA+DA);  
      }  
      double increment()  
      {  
           salary = salary + (0.1*salary);  
           return salary;  
      }  
 }  
 class S5P1_Employee  
 {  
      public static void main(String args[])  
      {  
           SalariedEmployee emp1 = new SalariedEmployee(1,"Dhruv","male",30000);  
           System.out.println("\nGross Salary for "+emp1.name+ ": "+(emp1.salary+emp1.allowance()+emp1.increment()));            
      }  
 }  

Output:

No comments:

Post a Comment