72029 Create an HourlyEmployee class that inherits from Employee and has two new
ID: 3815725 • Letter: 7
Question
72029
Create an HourlyEmployee class that inherits from Employee and has two new instance variables : hours, which
represents the hours worked, and wage, which represents the employee's pay per hour. (Both are doubles .)
Create a constructor that takes the arguments first name , last name , social security number, hourly wage,
and the number of hours worked. Also create accessors, mutators, an earnings method that returns the
money earned by the employee this week, and a toString method that returns information about the employee
in the form of a String . The setWage method should ensure that the wage is nonnegative, and the setHours
method should ensure that the value of hours is between 0 and 168 (the number of hours in a week).
Create a Driver class with a main method that prompts the user to enter a first name , last name ,
social security number, hours, and wage for an employee. Then, the program should create an HourlyEmployee
object and use its toString method to print information about it.
SAMPLE RUN #1: java Driver
Enter·first·name:Bobbi
Enter·last·name:Benton
Enter·social·security·number:765-42-0092
Enter·hours·worked:53
Enter·wage:15.8
hourly·employee:·Bobbi·Benton
social·security·number:·765-42-0092
hours:·53.0·
wage:·15.80·
earnings:·837.40
Explanation / Answer
HI, Please find my implementation.
##########################
//Employee.java
//Employee abstract superclass.
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
##############################
//HourlyEmployee.java
//HourlyEmployee class extends Employee.
public class HourlyEmployee extends Employee
{
private double wage; // wage per hour
private double hours; // hours worked for week
// five-argument constructor
public HourlyEmployee( String first, String last, String ssn,
double hourlyWage, double hoursWorked )
{
super( first, last, ssn);
setWage( hourlyWage ); // validate and store hourly wage
setHours( hoursWorked ); // validate and store hours worked
} // end five-argument HourlyEmployee constructor
// set wage
public void setWage( double hourlyWage )
{
wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
} // end method setWage
// return wage
public double getWage()
{
return wage;
} // end method getWage
// set hours worked
public void setHours( double hoursWorked )
{
hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
hoursWorked : 0.0;
} // end method setHours
// return hours worked
public double getHours()
{
return hours;
} // end method getHours
// calculate earnings; override abstract method earnings in Employee
public double earnings()
{
return getWage() * getHours();
} // end method earnings
// return String representation of HourlyEmployee object
public String toString()
{
return String.format( "hourly employee: %s %s: $%,.2f %s: %,.2f %s: %,.2f",
super.toString(),
"hours", getHours(),
"wage", getWage(),
"earnings",earnings() );
} // end method toString
} // end class HourlyEmployee
##################################
import java.util.Scanner;
public class TestHourlyEmployee {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// taking user input
System.out.print("Enter first name: ");
String first = sc.next();
System.out.print("Enter last name: ");
String last = sc.next();
System.out.print("Enter social security number: ");
String ssn = sc.next();
System.out.print("Enter hours worked: ");
double hoursWorked = sc.nextDouble();
System.out.print("Enter wage: ");
double hourlyWage = sc.nextDouble();
// creating an object of HourlyEmployee
HourlyEmployee emp = new HourlyEmployee(first, last, ssn, hourlyWage, hoursWorked);
System.out.println(emp);
}
}
/*
Sample run:
Enter first name: Bobbi
Enter last name: Benton
Enter social security number: 765-42-0092
Enter hours worked: 53
Enter wage: 15.8
hourly employee: Bobbi Benton
social security number: 765-42-0092
hours: $53.00
wage: 15.80
earnings: 837.40
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.