You are on page 1of 6

Abdullah Ayub(002) OOSE

Lab #2

Tool Used:
 Eclipse
Objective:
 To perform java implementation code, with inheritance and polymorphism
Task 1:
A company pays its employees on a weekly basis. The employees are of four types: Salaried
employees are paid a fixed weekly salary regardless of the number of hours worked, hourly
employees are paid by the hour and receive overtime pay for all hours worked in excess of 40
hours, commission employees are paid a percentage of their sales and salaried-commission
employees receive a base salary plus a percentage of their sales. For the current pay period, the
company has decided to reward salaried-commission employees by adding 10% to their base
salaries. The company wants to implement a Java application that performs its payroll
calculations polymorphic-ally.
Derive all four classes and calculate their salary accordingly

Code:
import java.util.Scanner;
public class Emp1 {

protected String name;


protected String id;
protected double Fsalary_hr;
public void getData(String a,String b,double x)
{
name=a;
id=b;
Fsalary_hr=x;
}
public void myCalculation()
{
System.out.println("Employees class\n");
}
class salaryEmployee extends Emp1{
private int whrs;
public void getData(String a,String b,double c,int d)
{
getData(a,b,c);
whrs=d;
Abdullah Ayub(002) OOSE
Lab #2
}
public void myCalculation()
{
double temp;
temp=whrs*Fsalary_hr;
System.out.println("weekly Salary : "+temp);
}
}
class hourlyEmployee extends Emp1{
private int mhrs;
private int overTime;
public void getData(String p,String q,double r,int s,int w)
{
getData(p,q,r);
mhrs=s;
overTime=w;
}
public void myCalculation()
{
double temp;
temp=mhrs*Fsalary_hr+overTime*Fsalary_hr;
System.out.println("Monthly Hourly salary : "+temp);
}
}
class commissionEmployee extends Emp1{
private double sale;
public void getData(String a,String b,double c,double d)
{
getData(a,b,c);
sale=d;
}
public void myCalculation()
{
double temp;
temp=sale*0.10;
System.out.println("Commision of Employee : "+temp);

}
}
class SCemployee extends Emp1{
private double sale2;

public void getData(String a,String b,double c,double d)


{
getData(a,b,c);
sale2=d;
}
public void myCalculation()
{
double temp;
temp=(sale2*0.10)+Fsalary_hr;
System.out.println("Salary_Commision of Employee : "+temp);
}
}
Abdullah Ayub(002) OOSE
Lab #2
public void getData(String name2, String id2, double a, int b) {
// TODO Auto-generated method stub

}
public void getData(String name2, String id2, double a, int b, int r) {
// TODO Auto-generated method stub

}
public void getData(String name2, String id2, double a, double sale) {
// TODO Auto-generated method stub

}
import java.util.Scanner;

/*import Emp.SCemployee;
import Emp.commissionEmployee;
import Emp.hourlyEmployee;
import Emp.salaryEmployee;*/

public class main1 {


public static void main(String[] args) {

Scanner s=new Scanner(System.in);


int x,n;
Emp1 e=new Emp1();
String name,id;
double a;int b;
int r;
double sale;
char ch;
do {
System.out.println("Press 1 For Salary Employeed ");
System.out.println("Press 2 For Houly Employeed");
System.out.println("Press 3 for Commission Employee");
System.out.println("Press 4 for Salary Commission Employee");
int num=s.nextInt();
switch(num) {
case 1:

System.out.println("Enter Employee name :");


name=s.nextLine();
name=s.nextLine();
System.out.println("Enter Employee ID :");
id=s.nextLine();
System.out.println("Enter Fixed base salary :");
a=s.nextDouble();
System.out.println("Enter weekly Hrs :");
b=s.nextInt();

Emp1 obj2=e.new salaryEmployee();


obj2.getData(name,id,a,b);
obj2.myCalculation();
Abdullah Ayub(002) OOSE
Lab #2
break;
case 2:

System.out.println("Enter Employee name :");


name=s.nextLine();
name=s.nextLine();
System.out.println("Enter Employee ID :");
id=s.nextLine();
System.out.println("Enter Fixed base salary :");
a=s.nextDouble();
System.out.println("Enter weekly Hrs :");
b=s.nextInt();
System.out.println("Enter Over Time Hrs :");
r=s.nextInt();
Emp1 h=e.new hourlyEmployee();

h.getData(name,id,a,b,r);
h.myCalculation();
break;
case 3:
System.out.println("Enter Employee name :");
name=s.nextLine();
name=s.nextLine();
System.out.println("Enter Employee ID :");
id=s.nextLine();
System.out.println("Enter Fixed base salary :");
a=s.nextDouble();
System.out.println("Enter Total sale :");
sale=s.nextDouble();
Emp1 c=e.new commissionEmployee();
c.getData(name,id,a,sale);
c.myCalculation();
break;
case 4:
System.out.println("Enter Employee name :");
name=s.nextLine();
name=s.nextLine();
System.out.println("Enter Employee ID :");
id=s.nextLine();
System.out.println("Enter Fixed base salary :");
a=s.nextDouble();
System.out.println("Enter Total sale :");
sale=s.nextDouble();
Emp1 sc=e.new SCemployee();
sc.getData(name,id,a,sale);
sc.myCalculation();
}
System.out.println("Do u want to continue Y/N");
ch=s.next().charAt(0);
}
while(ch=='Y'||ch=='y');
}
Abdullah Ayub(002) OOSE
Lab #2
}
Result:


Conclusion: Program run successfully.
Abdullah Ayub(002) OOSE
Lab #2

You might also like