480 Object-Oriented Programming Chapter 9 20 // get (Web design templates)
480 Object-Oriented Programming Chapter 9 20 // get Boss’s pay 21 public double earnings() 22 { 23 return weeklySalary; 24 } 25 26 // get String representation of Boss’s name 27 public String toString() 28 { 29 return “Boss: ” + super.toString(); 30 } 31 32 } // end class Boss Fig. 9.17 Bossextends abstractclass Employee. Class CommissionWorker (Fig. 9.18) is derived from Employee. The public methods include a constructor that takes a first name, a last name, a salary, a commission and a quantity of items sold as arguments and passes the first name and last name to the Employee constructor; set methods to assign new values to instance variables salary, commission and quantity; an earnings method to calculate a Commission- Worker s earnings; and a toString method that forms a String containing the employee type (i.e., “Commissionworker:”) followed by the worker s name. 1 // Fig. 9.18: CommissionWorker.java 2 // CommissionWorker class derived from Employee 3 4 public final class CommissionWorker extends Employee { 5 private double salary; // base salary per week 6 private double commission; // amount per item sold 7 private int quantity; // total items sold for week 8 9 // constructor for class CommissionWorker 10 public CommissionWorker( String first, String last, 11 double salary, double commission, int quantity ) 12 { 13 super( first, last ); // call superclass constructor 14 setSalary( salary ); 15 setCommission( commission ); 16 setQuantity( quantity ); 17 } 18 19 // set CommissionWorker’s weekly base salary 20 public void setSalary( double weeklySalary ) 21 { 22 salary = ( weeklySalary > 0 ? weeklySalary : 0 ); 23 } 24 Fig. 9.18 CommissionWorkerextends abstractclass Employee(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01