482 Object-Oriented Programming Chapter (My web server) 9 16 17 //
482 Object-Oriented Programming Chapter 9 16 17 // set PieceWorker’s wage 18 public void setWage( double wage ) 19 { 20 wagePerPiece = ( wage > 0 ? wage : 0 ); 21 } 22 23 // set number of items output 24 public void setQuantity( int numberOfItems ) 25 { 26 quantity = ( numberOfItems > 0 ? numberOfItems : 0 ); 27 } 28 29 // determine PieceWorker’s earnings 30 public double earnings() 31 { 32 return quantity * wagePerPiece; 33 } 34 35 public String toString() 36 { 37 return “Piece worker: ” + super.toString(); 38 } 39 40 } // end class PieceWorker Fig. 9.19 PieceWorkerextends abstractclass Employee(part 2 of 2). Class HourlyWorker (Fig. 9.20) is derived from Employee. The public methods include a constructor that takes a first name, a last name, a wage and the number of hours worked as arguments and passes the first name and last name to the Employee constructor; set methods to assign new values to instance variables wage and hours; an earnings method defining how to calculate an HourlyWorker s earnings; and a toString method that forms a String containing the type of the employee (i.e., “Hourly worker:”) followed by the hourly worker s name. 1 // Fig. 9.20: HourlyWorker.java 2 // Definition of class HourlyWorker 3 4 public final class HourlyWorker extends Employee { 5 private double wage; // wage per hour 6 private double hours; // hours worked for week 7 8 // constructor for class HourlyWorker 9 public HourlyWorker( String first, String last, 10 double wagePerHour, double hoursWorked ) 11 { 12 super( first, last ); // call superclass constructor 13 setWage( wagePerHour ); 14 setHours( hoursWorked ); 15 } Fig. 9.20 HourlyWorkerextends abstractclass Employee(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01