In: Computer Science
JAVA 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↵
import java.util.*;
class Employee
{
private String firstName,lastName,ssn;
public Employee(String firstName,String lastName,
String ssn) // constructor
{
this.firstName = firstName;
this.lastName = lastName;
this.ssn = ssn;
}
public String toString()
{
return "employee : "+firstName +"
"+lastName +"\nsocial security number : "+ssn;
}
}
class HourlyEmployee extends
Employee
{
private double wage;
private double hours;
public HourlyEmployee(String firstName,String
lastName, String ssn, double wage,double hours)
{
super(firstName,lastName,ssn);
this.wage = wage;
this.hours = hours;
}
public double earnings()
{
return wage *
hours;
}
public void setWage(double wage)
{
if(wage > 0)
this.wage = wage;
else
wage = 0;
}
public void setHours(double hours)
{
if(hours > 0 && hours <=
168)
this.hours = hours;
else
hours = 0;
}
public String toString()
{
return "hourly
"+ super.toString()+"\nhours : "+hours+"\nwages : "+wage+
String.format("\nearnings :%.2f",earnings());
}
}
class TestEmployee{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter first name:");
String firstName = input.next();
System.out.println("Enter last name:");
String lastName = input.next();
System.out.println("Enter social security
number:");
String ssn = input.next();
System.out.println("Enter hours worked:");
double hours = input.nextDouble();
System.out.println("Enter wage:");
double wage = input.nextDouble();
HourlyEmployee emp = new
HourlyEmployee(firstName,lastName,ssn,hours,wage);
System.out.println(emp);
}
}
Output:
Enter first name:Mazie Enter last name:Payne Enter social security number:633-38-5740 Enter hours worked:45 Enter wage:22.5 hourly employee : Mazie Payne social security number : 633-38-5740 hours : 22.5 wages : 45.0 earnings :1012.50
Do ask if any doubt. Please upvote.