Page 559 - Introduction to Programming with Java: A Problem Solving Approach
P. 559

                Output:
4
4
13.7 Polymorphism with Arrays 525
  Employee
 -name : String
 +Employee(name : String)
+getPay() : double +printPay(date : int) : void
     Salaried
 -salary : double
 +Salaried(name : String, salary : double) +getPay() : double
  Hourly
 -hourlyRate : double -hours : double = 0.0
 +Hourly(name : String, rate : double) +getPay() : double
+addHours(hours : double) : void
    Payroll
  +main() : void
  main :
employees : Employee[]
Apago PDF Enhancer
Figure 13.8 Class diagram for Payroll program
other classes—inheritance or composition/aggregation? The UML class diagram’s diamonds indicate a composition/aggregation association between the Payroll container and the Salaried and Hourly components. That should make sense when you realize that the Payroll class “has a” heterogeneous array of Salaried and Hourly objects. Assuming the Payroll class has exclusive control over these objects, its association with them is a composition, and the diamonds should be solid.
Suppose Anna and Donovan are hourly employees paid at $25 per hour and $20 per hour, respectively, Simon is a salaried employee paid at $4,000 per month, and all three start work at the beginning of the month, which is a Tuesday. When the program runs, it should output the date of the month, the employee name, and the amount paid on the indicated date, like this:
 11
11
15
Anna:
Donovan:
Anna:
Donovan:
Simon:
800.00
640.00
1000.00
800.00
2000.00
Let’s begin implementation with the main method in the driver in Figure 13.9. Note main’s local variable, employees. It’s declared to be a 100-element array of Employee objects. That’s what it’s declared as, but that’s not exactly what it holds. As you can see from the assignment statements, the first three employees elements are an Hourly, a Salaried, and another Hourly. This is a heterogeneous array. All of the elements in the array are instances of classes derived from the array’s class, and none of them is an instance






























































   557   558   559   560   561