Java Reference
In-Depth Information
number of hours worked and an hourly rate. Suppose that you want to define a class to keep
track of a part-time employee's information, such as the name, pay rate, and hours worked.
You can then print the employee's name, together with his or her wages. Recall that
Example 8-8 (Chapter 8) defined the class Person to store the first name and the last
name together with the necessary operations on name . Because every employee is a person,
we can define a class PartTimeEmployee derived from the class Person . You can also
override the method toString of the class Person to print the appropriate information.
The members of the class PartTimeEmployee are as follows:
Instance Variables:
private double payRate;
//store the pay rate
private double hoursWorked; //store the hours worked
Instance Methods:
public void setNameRateHours(String first, String last,
double rate, double hours)
//Method to set the first name, last name, payRate,
//and hoursWorked according to the parameters.
//The parameters first and last are passed to the
//superclass.
//Postcondition: firstName = first; lastName = last;
//
payRate = rate; hoursWorked = hours;
public double getPayRate()
//Method to return the pay rate
//Postcondition: The value of payRate is returned
1
0
public double getHoursWorked()
//Method to return the number of hours worked
//Postcondition: The value of hoursWorked is returned
public double calculatePay()
//Method to calculate and return the wages
public String toString()
//Method to return the string consisting of the
//first name, last name, and the wages in the form:
//firstName lastName wages are $$$$.$$
public PartTimeEmployee(String first, String last,
double rate, double hours)
//Constructor with parameters
//Set the first name, last name, payRate, and
//hoursWorked according to the parameters.
//Parameters first and last are passed to the
//superclass.
//Postcondition: firstName = first; lastName = last;
//
payRate = rate; hoursWorked = hours;
Search WWH ::




Custom Search