









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










1 // Boss.java 2 // Boss class derived from Employee. 3 4 public final class Boss extends Employee { 5 private double weeklySalary; 6 7 // constructor for class Boss 8 public Boss( String first, String last, double salary ) 9 { 10 super( first, last ); // call superclass constructor 11 setWeeklySalary( salary ); 12 } 13 14 // set Boss's salary 15 public void setWeeklySalary( double salary ) 16 { 17 weeklySalary = ( salary > 0? salary : 0 ); 18 } 19 20 // get Boss's pay
Example: A Payroll System Using
Polymorphism (CommissionWorker)
1 // 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 )
Example: A Payroll System Using
Polymorphism (CommissionWorker)
22 salary = ( weeklySalary > 0? weeklySalary : 0 ); 23 } 24 25 // set CommissionWorker's commission 26 public void setCommission( double itemCommission ) 27 { 28 commission = ( itemCommission > 0? itemCommission : 0 ); 29 } 30 31 // set CommissionWorker's quantity sold 32 public void setQuantity( int totalSold ) 33 { 34 quantity = ( totalSold > 0? totalSold : 0 ); 35 } 36 37 // determine CommissionWorker's earnings 38 public double earnings() 39 { 40 return salary + commission * quantity;
Example: A Payroll System Using
Polymorphism (PieceWorker)
1 // PieceWorker.java 2 // PieceWorker class derived from Employee 3 4 public final class PieceWorker extends Employee { 5 private double wagePerPiece; // wage per piece output 6 private int quantity; // output for week 7 8 // constructor for class PieceWorker 9 public PieceWorker( String first, String last, 10 double wage, int numberOfItems ) 11 { 12 super( first, last ); // call superclass constructor 13 setWage( wage ); 14 setQuantity( numberOfItems); 15 } 16 17 // set PieceWorker's wage 18 public void setWage( double wage ) 19 { 20 wagePerPiece = ( wage > 0? wage : 0 );
Example: A Payroll System Using
Polymorphism (PieceWorker)
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
Example: A Payroll System Using
Polymorphism (HourlyWorker)
23 // Set the hours worked
24 public void setHours( double hoursWorked ) 25 { 26 hours = ( hoursWorked >= 0 && hoursWorked < 168? 27 hoursWorked : 0 );
28 } 29 30 // Get the HourlyWorker's pay 31 public double earnings() { return wage * hours; }
32 33 public String toString() 34 { 35 return "Hourly worker: " + super.toString();
36 } 37 38 } // end class HourlyWorker
1 // Test.java 2 // Driver for Employee hierarchy 3 4 // Java core packages 5 import java.text.DecimalFormat; 6 7 // Java extension packages 8 import javax.swing.JOptionPane; 9 10 public class Test { 11 12 // test Employee hierarchy 13 public static void main( String args[] ) 14 { 15 Employee employee; // superclass reference 16 String output = ""; 17 18 Boss boss = new Boss( "John", "Smith", 800.0 ); 19
40 // Employee reference to a CommissionWorker 41 employee = commissionWorker; 42 43 output += employee.toString() + " earned $" + 44 precision2.format( employee.earnings() ) + "\n" + 45 commissionWorker.toString() + " earned $" + 46 precision2.format( 47 commissionWorker.earnings() ) + "\n"; 48 49 // Employee reference to a PieceWorker 50 employee = pieceWorker; 51 52 output += employee.toString() + " earned $" + 53 precision2.format( employee.earnings() ) + "\n" + 54 pieceWorker.toString() + " earned $" + 55 precision2.format( pieceWorker.earnings() ) + "\n"; 56 57 // Employee reference to an HourlyWorker 58 employee = hourlyWorker; 59