/* * Created on Sep 27, 2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * @author syucel * * This example is adapted from Deitels' book "Java How To Program". */ import java.text.DecimalFormat; public class AbstractEmployee { public static void main(String[] args) { Employee ref; // superclass reference Boss b; PieceWorker p; HourlyWorker h; b = new Boss( "John", "Smith", 800.00 ); p = new PieceWorker( "Bob", "Lewis", 2.5, 200 ); h = new HourlyWorker( "Karen", "Price", 13.75, 40 ); DecimalFormat precision2 = new DecimalFormat( "#.00" ); ref = b; // superclass reference to subclass object System.out.println( ref + " earned $" + precision2.format( ref.earnings() )); System.out.println( b + " earned $" + precision2.format( b.earnings() )); ref = p; // superclass reference to subclass object System.out.println( ref + " earned $" + precision2.format( ref.earnings() )); ref = h; // superclass reference to subclass object System.out.println( ref + " earned $" + precision2.format( ref.earnings() )); } } abstract class Employee { // Constructor public Employee( String first, String last ) { firstName = first; lastName = last; } // Return the first name public String getFirstName() { return firstName; } // Return the last name public String getLastName() { return lastName; } public String toString() { return firstName + ' ' + lastName; } // Abstract method that must be implemented for each // derived class of Employee from which objects // are instantiated. abstract double earnings(); private String firstName; private String lastName; } final class Boss extends Employee { // Constructor for class Boss public Boss( String first, String last, double s) { super( first, last ); // call superclass constructor setWeeklySalary( s ); } // Set the Boss's salary public void setWeeklySalary( double s ) { weeklySalary = ( s > 0 ? s : 0 ); } // Get the Boss's pay public double earnings() { return weeklySalary; } // Print the Boss's name public String toString() { return "Boss: " + super.toString(); } private double weeklySalary; } final class PieceWorker extends Employee { // Constructor for class PieceWorker public PieceWorker( String first, String last, double w, int q ) { super( first, last ); // call superclass constructor setWage( w ); setQuantity( q ); } // Set the wage public void setWage( double w ) { wagePerPiece = ( w > 0 ? w : 0 ); } // Set the number of items output public void setQuantity( int q ) { quantity = ( q > 0 ? q : 0 ); } // Determine the PieceWorker's earnings public double earnings() { return quantity * wagePerPiece; } public String toString() { return "Piece worker: " + super.toString(); } private double wagePerPiece; // wage per piece output private int quantity; // output for week } final class HourlyWorker extends Employee { // Constructor for class HourlyWorker public HourlyWorker( String first, String last, double w, double h ) { super( first, last ); // call superclass constructor setWage( w ); setHours( h ); } // Set the wage public void setWage( double w ) { wage = ( w > 0 ? w : 0 ); } // Set the hours worked public void setHours( double h ) { hours = ( h >= 0 && h < 168 ? h : 0 ); } // Get the HourlyWorker's pay public double earnings() { return wage * hours; } public String toString() { return "Hourly worker: " + super.toString(); } private double wage; // wage per hour private double hours; // hours worked for week }