Java Reference
In-Depth Information
b)
Assume that the following line of code appears before a method declaration:
@Override
c)
Assume that the following line of code appears as the first statement in a constructor's
body:
super (firstArgument, secondArgument);
9.10 (Write a Line of Code) Write a line of code that performs each of the following tasks:
a) Specify that class PieceWorker inherits from class Employee .
b) Call superclass Employee 's toString method from subclass PieceWorker 's toString
method.
c) Call superclass Employee 's constructor from subclass PieceWorker 's constructor—as-
sume that the superclass constructor receives three String s representing the first name,
last name and social security number.
9.11 (Using super in a Constructor's Body) Explain why you would use super in the first state-
ment of a subclass constructor's body.
9.12 (Using super in an Instance Method's Body) Explain why you would use super in the body
of a subclass's instance method.
9.13 (Calling get Methods in a Class's Body) In Figs. 9.10-9.11 methods earnings and to-
String each call various get methods within the same class. Explain the benefits of calling these get
methods within the classes.
9.14 ( Employee Hierarchy) In this chapter, you studied an inheritance hierarchy in which class
BasePlusCommissionEmployee inherited from class CommissionEmployee . However, not all types of
employees are CommissionEmployee s. In this exercise, you'll create a more general Employee superclass
that factors out the attributes and behaviors in class CommissionEmployee that are common to all Em-
ployee s. The common attributes and behaviors for all Employee s are firstName , lastName , socialSe-
curityNumber , getFirstName , getLastName , getSocialSecurityNumber and a portion of method
toString . Create a new superclass Employee that contains these instance variables and methods and a
constructor. Next, rewrite class CommissionEmployee from Section 9.4.5 as a subclass of Employee .
Class CommissionEmployee should contain only the instance variables and methods that are not de-
clared in superclass Employee . Class CommissionEmployee 's constructor should invoke class Employee 's
constructor and CommissionEmployee 's toString method should invoke Employee 's toString method.
Once you've completed these modifications, run the CommissionEmployeeTest and BasePlusCommis-
sionEmployeeTest apps using these new classes to ensure that the apps still display the same results for
a CommissionEmployee object and BasePlusCommissionEmployee object, respectively.
9.15 (Creating a New Subclass of Employee ) Other types of Employee s might include Salaried-
Employee s who get paid a fixed weekly salary, PieceWorker s who get paid by the number of pieces
they produce or HourlyEmployee s who get paid an hourly wage with time-and-a-half—1.5 times the
hourly wage—for hours worked over 40 hours.
Create class HourlyEmployee that inherits from class Employee (Exercise 9.14) and has
instance variable hours (a double ) that represents the hours worked, instance variable wage (a dou-
ble ) that represents the wages per hour, a constructor that takes as arguments a first name, a last
name, a social security number, an hourly wage and the number of hours worked, set and get meth-
ods for manipulating the hours and wage , an earnings method to calculate an HourlyEmployee 's
earnings based on the hours worked and a toString method that returns the HourlyEmployee 's
String representation. Method setWage should ensure that wage is nonnegative, and setHours
should ensure that the value of hours is between 0 and 168 (the total number of hours in a week).
Use class HourlyEmployee in a test program that's similar to the one in Fig. 9.5.
Search WWH ::




Custom Search