Hi, can I please get some help with the following task. I've started the coding, although I have some of the methods I'm not sure on how to do (i.e the toString and equals)
The task and other provided are below.
Task.doc
public class Employee { private String name; public Employee() { } public Employee(String employeeName) { name = employeeName; } public Employee(Employee Employee) { } public String getName() { return name; } public void setName(String employeeName) { this.name = employeeName; } public String toString() { return name; } public boolean equals(Employee Object) { } }
public class HourlyEmployee extends Employee { private double wageRate; private double hours; public HourlyEmployee() { } public HourlyEmployee(String name, double employeeWageRate, double employeeHours) { super(name); wageRate = employeeWageRate; hours = employeeHours; } public HourlyEmployee(Employee HourlyEmployee) { } public double getRate() { return wageRate; } public double getHours() { return hours; } public double getPay() { return wageRate*hours; } public void setHours(double employeeHours) { this.hours = employeeHours; } public void setRate(double employeeWageRate) { this.wageRate = employeeWageRate; } public String toString() { return "Sam\n$50.5 per hour for 40.0 hours\n"; } // public boolean equals(Object) // { // // } }
public class SalariedEmployee extends Employee { private double salary; public SalariedEmployee() { } public SalariedEmployee(String name, double employeeSalary) { super(name); salary = employeeSalary; } public SalariedEmployee(Employee SalariedEmployee) { } public double getSalary() { return salary; } // public double getPay() // { // return ; // } public void setSalary(double employeeSalary) { this.salary = employeeSalary; } // public String toString() // { // return // } // public boolean equals(Object Employee) // { // // } }
public class EmployeeDemo { public static void main(String[] args) { ArrayList<Employee> emplist = new ArrayList<Employee>(); emplist.add(new SalariedEmployee("Josephine", 100000)); emplist.add(new HourlyEmployee("Sam",50.50, 40.0)); //this loop shows you how to call a public method in a superclass that all subclasses inherit from. //this also shows how to call methods of individual sub classes. for(Employee aEmp: emplist) { System.out.println(aEmp.getName()); if(aEmp instanceof SalariedEmployee) { System.out.println("$" + ((SalariedEmployee) aEmp).getSalary() + " per year"); } else if(aEmp instanceof HourlyEmployee) { System.out.print("$" + ((HourlyEmployee) aEmp).getRate() + " per hour for "); System.out.println(((HourlyEmployee) aEmp).getHours() + " hours"); } System.out.println(); } //the following will produce the same output as above. //Note that System.out.println() calls the toString() method initially defined in class Object but //overridden by SalariedEmployee and HourlyEmployee classes. for(Employee aEmp: emplist) { System.out.println(aEmp +"\n"); } } }