You are on page 1of 4

P7.1: K tha Xy dng lp Employee l lp c s, 2 lp SalaryEmployee v WageEmployee k tha t lp c s Employee. /* * Employee.java * Copyright 2009 - http://ljbe-course.googlecode.com.

. */ package maxsoft; /** * Employee class that act as the base class for other classes. */ public class Employee { /** * Instance variable to store the employee's name. */ String empName; /** Creates a new instance of Employee */ public Employee() { } /** * Creates a new instance of Employee class and stores the name * in the instance variable. * @param name Parameter containing the name of the employee */ public Employee(String name) { empName = name; } /** * Method to display the name. */ void displayDetails() { System.out.printf("Employee Name: %s", empName); } } /* * SalaryEmployee.java * * This class is a derived class inherited from Employee class, used to store * the salary details and display the same.

* * Copyright 2009 - http://ljbe-course.googlecode.com. */ package maxsoft; /** * SalaryEmployee class contains all the details of salaried employees. */ public class SalaryEmployee extends Employee { /** * Instance variable salary to store salary for salaried employees. */ double salary; /** Creates a new instance of SalaryEmployee */ public SalaryEmployee() { } /** * Creates a new instance of SalaryEmployee and stores salary * in the instance variable ans calls the base constructor. * @param newSalary Parameter containing the salary of the employee * @param name Parameter containing the name of the employee */ public SalaryEmployee(String name, double currentSalary) { super(name); salary = currentSalary; } /** * Method to display the details of salaried employees. */ void displayDetails() { super.displayDetails(); System.out.printf("\n" + empName + "'s Salary: %.2f\n", salary); } } /* * * * * * *

WageEmployee.java This class is a derived class of Employee class, used to store the salary details of the waged employees and display the same. Copyright 2009 - http://ljbe-course.googlecode.com.

*/ package maxsoft; /** * WageEmployee class contains all the details of waged employees. */ public class WageEmployee extends Employee { /** * Instance variable to store rate for one hour. */ double rate; /** * Instance variable to store the employee's working hours. */ int hours; /** Creates a new instance of WageEmployee */ public WageEmployee() { } /** * Creates a new instance of WageEmployee and stores details * in the instance variables. * @param name Parameter containing the name of the employee * @param wageRate Parameter containing the rate for an hour * @param wageHours Parameter containing the number of hours worked */ public WageEmployee(String name, double wageRate, int wageHours) { super(name); rate = wageRate; hours = wageHours; } /** * Method to display the details of waged employees. */ void displayDetails() { super.displayDetails(); System.out.printf("\n" + empName + "'s Salary: %.2f\n", rate * hours); } } /* * TestEmployee.java *

* This class contains the main method, where the objects are created. * * Copyright 2009 - http://ljbe-course.googlecode.com. */ package maxsoft; /** * TestEmployee class to create objects and call the methods. */ public class TestEmployee { /** * This is the entry point of the application. * @param args the command line arguments */ public static void main(String[] args) { // Create an instance of salaried employee, John Employee objJohn = new SalaryEmployee("John", 2300.50); // Display the details of John objJohn.displayDetails(); // Create an instance of waged employee, David Employee objDavid = new WageEmployee("David", 34.50, 10); // Display the details of David objDavid.displayDetails(); } }

You might also like