CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2012
    Posts
    1

    Polymorphism question, help needed

    Not sure how to approach the two tasks i have been given below..can anyone tell me where I am going wrong or give me a pointer or two?


    – Modify the supplied payroll system to include private instance variable joinDate in class Employee to represent when they joined the company. Use the java.util.Date for this variable.
    – Use a static variable in the Employee class to help automatically assign each new employee a unique (incremental) id number.



    Code:
    // Abstract base class Employee.
    import java.util.Date;
    public abstract class Employee {
    
        private String firstName;
        private String lastName;
        public Date birthDate;
        // constructor
        public Employee(String first, String last, Date dateOfBirth) {
            firstName = first;
            lastName = last;
            birthDate = dateOfBirth;
        }
    
        // get first name
        public String getFirstName() {
            return firstName;
        }
    
        // get last name
        public String getLastName() {
            return lastName;
        }
        
        public void setBirthDate(Date dateOfBirth)
        {
     		birthDate = dateOfBirth;
        }
      
        public Date getBirthDate()
        {
     	   return birthDate;
        }
        
        
        public String toString() {
            return firstName + ' ' + lastName;// how can add dates to this without creating a type problem?
        }
    
        public abstract double earnings();
    }
    Code:
    import java.util.Date;
    
    // CommissionWorker class derived from Employee
    
    public final class CommissionWorker extends Employee {
    
        private double salary; // base salary per week
        private double commission; // amount per item sold
        private int quantity; // total items sold for week
    
        // constructor for class CommissionWorker
        public CommissionWorker(String first, String last, Date dateOfBirth,
                double salary, double commission, int quantity) {
            super(first, last, dateOfBirth); // call superclass constructor
            setSalary(salary);
            setCommission(commission);
            setQuantity(quantity);
        }
    
        // set CommissionWorker's weekly base salary
        public void setSalary(double weeklySalary) {
            salary = (weeklySalary > 0 ? weeklySalary : 0);
        }
    
        // set CommissionWorker's commission
        public void setCommission(double itemCommission) {
            commission = (itemCommission > 0 ? itemCommission : 0);
        }
    
        // set CommissionWorker's quantity sold
        public void setQuantity(int totalSold) {
            quantity = (totalSold > 0 ? totalSold : 0);
        }
    
        // determine CommissionWorker's earnings
        public double earnings() {
            return salary + commission * quantity;
        }
    
        // get String representation of CommissionWorker's name
        public String toString() {
            return "Commission worker: " + super.toString();
        }
    } // end class CommissionWorker
    Code:
    import java.util.Date;
    
    // Boss class derived from Employee.
    
    public final class Boss extends Employee {
    
        private double weeklySalary;
    
        // constructor for class Boss
        public Boss(String first, String last, double salary, Date dateOfBirth) {
            super(first, last, dateOfBirth); // call superclass constructor
            setWeeklySalary(salary);
        }
    
        // set Boss's salary
        public void setWeeklySalary(double salary) {
            weeklySalary = (salary > 0 ? salary : 0);
        }
    
        // get Boss's pay
        public double earnings() {
            return weeklySalary;
            
        }
    
        // get String representation of Boss's name
        public String toString() {
            return "Boss: " + super.toString();
        }
    } // end class Boss
    Code:
    // Driver for Employee hierarchy
    
    // Java core packages
    import java.text.DecimalFormat;
    import java.util.Date;
    
    // Java extension packages
    import javax.swing.JOptionPane;
    
    public class Test {
    
        // test Employee hierarchy
        public static void main(String args[]) {
            Employee employee; // superclass reference
            String output = "";
    
            Boss boss = new Boss("John", "Smith",800.0,  new Date(8, 7,1979));
    
            CommissionWorker commissionWorker =
                    new CommissionWorker("Sue", "Jones",new Date(8, 7,1979),400.0, 3.0, 150);
    
            PieceWorker pieceWorker =
                    new PieceWorker("Bob", "Lewis",new Date(8, 7,1979),  2.5, 200);
    
            HourlyWorker hourlyWorker =
                    new HourlyWorker("Karen", "Price",new Date(8, 7,1979),  13.75, 40);
    
            DecimalFormat precision2 = new DecimalFormat("0.00");
    
    // Employee reference to a Boss
            employee = boss;
    
            output += employee.toString() + " earned $"
                    + precision2.format(employee.earnings()) + "\n"
                    + boss.toString() + " earned $"
                    + precision2.format(boss.earnings()) + "\n";
    
            // Employee reference to a CommissionWorker
            employee = commissionWorker;
    
            output += employee.toString() + " earned $"
                    + precision2.format(employee.earnings()) + "\n"
                    + commissionWorker.toString() + " earned $"
                    + precision2.format(
                    commissionWorker.earnings()) + "\n";
    
            // Employee reference to a PieceWorker
            employee = pieceWorker;
    
            output += employee.toString() + " earned $"
                    + precision2.format(employee.earnings()) + "\n"
                    + pieceWorker.toString() + " earned $"
                    + precision2.format(pieceWorker.earnings()) + "\n";
    
    // Employee reference to an HourlyWorker
            employee = hourlyWorker;
    
            output += employee.toString() + " earned $"
                    + precision2.format(employee.earnings()) + "\n"
                    + hourlyWorker.toString() + " earned $"
                    + precision2.format(hourlyWorker.earnings()) + "\n";
    
            JOptionPane.showMessageDialog(null, output,
                    "Demonstrating Polymorphism",
                    JOptionPane.INFORMATION_MESSAGE);
    
            System.exit(0);
        }
    } // end class Test
    Code:
    import java.util.Date;
    
    // PieceWorker class derived from Employee
    
    public final class PieceWorker extends Employee {
    
       
    	private double wagePerPiece; // wage per piece output
        private int quantity; // output for week
    
        // constructor for class PieceWorker
        public PieceWorker(String first, String last, Date dateOfBirth,
        		double wage, int numberOfItems) {
            super(first, last, dateOfBirth); // call superclass constructor
            setWage(wage);
            setQuantity(numberOfItems);
        }
    
        // set PieceWorker's wage
        public void setWage(double wage) {
            wagePerPiece = (wage > 0 ? wage : 0);
        }
    
        // set number of items output
        public void setQuantity(int numberOfItems) {
            quantity = (numberOfItems > 0 ? numberOfItems : 0);
        }
    
        // determine PieceWorker's earnings
        public double earnings() {
            return quantity * wagePerPiece;
        }
    
        public String toString() {
            return "Piece worker: " + super.toString();
        }
    }
    Code:
    import java.util.Date;
    
    // Definition of class HourlyWorker
    
    public final class HourlyWorker extends Employee {
    
        private double wage; // wage per hour
        private double hours; // hours worked for week
    
        // constructor for class HourlyWorker
        public HourlyWorker(String first, String last, Date dateOfBirth,
                double wagePerHour, double hoursWorked) {
            super(first, last, dateOfBirth); // call superclass constructor
            setWage(wagePerHour);
            setHours(hoursWorked);
        }
    
        // Set the wage
        public void setWage(double wagePerHour) {
            wage = (wagePerHour > 0 ? wagePerHour : 0);
        }
    
        // Set the hours worked
        public void setHours(double hoursWorked) {
            hours = (hoursWorked >= 0 && hoursWorked < 168
                    ? hoursWorked : 0);
        }
    
        // Get the HourlyWorker's pay
        public double earnings() {
            return wage * hours;
        }
    
        public String toString() {
            return "Hourly worker: " + super.toString();
        }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Polymorphism question, help needed

    .can anyone tell me where I am going wrong
    Posting your homework without showing any effort on your part

    give me a pointer or two?
    The two tasks clearly state what is required read them carefully and do exactly as they say. If you are still stuck please show what you have done so far and say what you don't you understand and we will help you?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured