2. You are given classes Employee and SalariedEmployee (the ones from your Lab 6
ID: 3840563 • Letter: 2
Question
2. You are given classes Employee and SalariedEmployee (the ones from your Lab 6). The classes create a hierarchy where the abstract class Employee is a super class for the SalariedEmployee. Please add an overloaded no argument constructor for the SalariedEmployee. The constructor should set the first, lastname and SSN to “ ” (a string with a single space character) and the salary to 600.00 (minimum salary for a 40 hour week). Do not use calls to set methods from the constructor, instead call the four argument constructor of the class SalariedEmployee class.
Question 2a: Did you use both a call to super() and this() in your implementation of the no argument constructor for the SalariedEmployee? If yes did this compile? If not is a call to the super class constructor taking place when you construct an object of the SalariedEmployee class using the no argument constructor? Explain.
Question 2b: Create a test class in order to showcase your new no argument constructor and its functionality. Please provide the code (.java file) for your new SalariedEmployee class and your test class. Please provide execution screenshots.
Employee.java
public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
// three-argument constructor
public Employee( String first, String last, String ssn )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // end three-argument Employee constructor
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// return String representation of Employee object
public String toString()
{
return String.format( "%s %s social security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber() );
} // end method toString
// abstract method overridden by subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
SalariedEmployee.java
public class SalariedEmployee extends Employee
{
private double weeklySalary;
// four-argument constructor
public SalariedEmployee( String first, String last, String ssn,
double salary )
{
super( first, last, ssn ); // pass to Employee constructor
setWeeklySalary( salary ); // validate and store salary
} // end four-argument SalariedEmployee constructor
// set salary
public void setWeeklySalary( double salary )
{
weeklySalary = salary < 0.0 ? 0.0 : salary;
} // end method setWeeklySalary
// return salary
public double getWeeklySalary()
{
return weeklySalary;
} // end method getWeeklySalary
// calculate earnings; override abstract method earnings in Employee
public double earnings()
{
return getWeeklySalary();
} // end method earnings
// return String representation of SalariedEmployee object
public String toString()
{
return String.format( "salaried employee: %s %s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary() );
} // end method toString
} // end class SalariedEmployee
Explanation / Answer
public abstract class Employee {
private String firstName;
private String lastName;
private String socialSecurityNumber;
// three-argument constructor
public Employee(String first, String last, String ssn) {
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // end three-argument Employee constructor
// set first name
public void setFirstName(String first) {
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName() {
return firstName;
} // end method getFirstName
// set last name
public void setLastName(String last) {
lastName = last;
} // end method setLastName
// return last name
public String getLastName() {
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber(String ssn) {
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber() {
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// return String representation of Employee object
public String toString() {
return String.format("%s %s social security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber());
} // end method toString
// abstract method overridden by subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
public class SalariedEmployee extends Employee {
private double weeklySalary;
public SalariedEmployee() {
// TODO Auto-generated constructor stub
this("", "", "", 600);
}
// four-argument constructor
public SalariedEmployee(String first, String last, String ssn, double salary) {
super(first, last, ssn); // pass to Employee constructor
setWeeklySalary(salary); // validate and store salary
} // end four-argument SalariedEmployee constructor
// set salary
public void setWeeklySalary(double salary) {
weeklySalary = salary < 0.0 ? 0.0 : salary;
} // end method setWeeklySalary
// return salary
public double getWeeklySalary() {
return weeklySalary;
} // end method getWeeklySalary
// calculate earnings; override abstract method earnings in Employee
public double earnings() {
return getWeeklySalary();
} // end method earnings
// return String representation of SalariedEmployee object
public String toString() {
return String.format("salaried employee: %s %s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary());
} // end method toString
} // end class SalariedEmployee
/**
*
*/
/**
* @author
*
*/
public class TestSalariedEmployee {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SalariedEmployee employee = new SalariedEmployee();
System.out.println("Salaried Employee with no argument constructor:");
System.out.println(employee);
}
}
OUTPUT:
Salaried Employee with no argument constructor:
salaried employee:
social security number:
weekly salary: $600.00
NOTE:
Question 2a:
In the SalariedEmployee no argument constructor, i am not using super() and this(), i am using only this() with arguments this will calls the SalariedEmployee constructor with arguments .
this will calls the super() and sets the weeklySalary member
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.