// Employee abstract superclass public abstract class Employee { private String
ID: 3806393 • Letter: #
Question
// Employee abstract superclass
public abstract class Employee
{
private String firstName;
private String lastName;
private String SSN;
private double salary;
// three-argument constructor
public Employee( String first, String last, String ssn, double salary )
{
firstName = first;
lastName = last;
SSN = ssn;
} // end three-argument Employee constructor
// set first name
public void setFirstName( String first )
{
firstName = first; // should validate
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last; // should validate
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSSN( String ssn )
{
this.SSN = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSSN()
{
return SSN;
} // end method getSocialSecurityNumber
// return String representation of Employee object
@Override
public String toString()
{
return String.format( "%-12s%-12s%-12s",
getFirstName(), getLastName(), getSSN() );
} // end method toString
// abstract method overridden by concrete subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
----------------------------------------------------------------
public class FullTimeEmployee extends Employee
{
private double weeklySalary;
FullTimeEmployee(String firstname, String lastname, String SSN, double salary)
{
this.firstName = firstName;
this.lastName = lastName;
this.ssn = SSN;
this.salary = weeklySalary;
}
public double getWeeklySalary()
{
return weeklySalary;
}
public void setWeeklySalary(double weeklySalary)
{
this.weeklySalary = weeklySalary;
}
public double earnings()
{
return weeklySalary;
}
public String toString()
{
return this.getFirstName() + " " + this.getLastName() + " " + this.getWeeklySalary() + " " + this.getSsn();
}
}
-----------------------------------------------------------------
public class PartTimeEmployee extends Employee
{
private double wage;
private int hours;
public double getWage()
{
return wage;
}
public void setWage(double wage)
{
this.wage = wage;
}
public int getHours()
{
return hours;
}
public void setHours(int hours)
{
this.hours = hours;
}
public PartTimeEmployee(String firstname, String lastname, String ssn, double wage, int hours)
{
this.firstName = firstname;
this.lastName = lastname;
this.ssn = ssn;
this.wage = wage;
this.hours = hours;
}
public double earnings()
{
return weeklySalary;
}
public String toString()
{
return this.getFirstName() + " " + this.getLastName() + " " + this.getSsn() + " " + (double)(this.getWage() * this.getHours());
}
}
---------------------------------------
I cannot get PartTimeEmployee and FullTimeEmployee's constructors to play nice with Employee. I am supposed to use polymorphism with Employee's abstract class. I cannot change Employee's code. How can I make them play nice?
Explanation / Answer
UPDATED CODE:
// Employee abstract superclass
public abstract class Employee
{
private String firstName;
private String lastName;
private String SSN;
private double salary;
public Employee(){}
// three-argument constructor
public Employee( String first, String last, String ssn)
{
firstName = first;
lastName = last;
SSN = ssn;
} // end three-argument Employee constructor
// set first name
public void setFirstName( String first )
{
firstName = first; // should validate
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last; // should validate
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSSN( String ssn )
{
this.SSN = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSSN()
{
return SSN;
} // end method getSocialSecurityNumber
// return String representation of Employee object
@Override
public String toString()
{
return String.format( "%-12s%-12s%-12s", getFirstName(), getLastName(), getSSN() );
} // end method toString
// abstract method overridden by concrete subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
class FullTimeEmployee extends Employee
{
private double weeklySalary;
FullTimeEmployee(String firstname, String lastname, String SSN, double salary)
{
super( firstname, lastname, SSN);
weeklySalary = salary;
}
public double getWeeklySalary()
{
return weeklySalary;
}
public void setWeeklySalary(double weeklySalary)
{
this.weeklySalary = weeklySalary;
}
public double earnings()
{
return weeklySalary;
}
public String toString()
{
return this.getFirstName() + " " + this.getLastName() + " " + this.getWeeklySalary() + " " + this.getSSN();
}
}
class PartTimeEmployee extends Employee
{
private double wage;
private int hours;
public double getWage()
{
return wage;
}
public void setWage(double wage)
{
this.wage = wage;
}
public int getHours()
{
return hours;
}
public void setHours(int hours)
{
this.hours = hours;
}
public PartTimeEmployee(String firstname, String lastname, String SSN, double wage, int hours)
{
super( firstname, lastname, SSN);
this.wage = wage;
this.hours = hours;
}
public double earnings()
{
return wage*hours ;
}
public String toString()
{
return getFirstName() + " " + getLastName() + " " + getSSN() + " " + (double)(getWage() * getHours());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.