In: Computer Science
Create a class Employee. Your Employee class should include the following attributes:
First name (string)
Last name (string)
Employee id (string)
Employee home street address (string)
Employee home city (string)
Employee home state (string)
Write a constructor to initialize the above Employee attributes.
Create another class HourlyEmployee that inherits from the Employee class. HourEmployee must use the inherited parent class variables and add in HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the Employee class to initialize the common instance variables but also initializes the HourlyRate and HoursWorked. Add an earnings method to HourlyEmployee to calculate the earnings for a week. Note that earnings is hourly rate * hours worked.
Create a test class that prompts the user for the information for two hourly employees, creates the 2 two hourly employees objects, calls the earnings method then displays the attributes and earnings for each of the two hourly.
import java.util.Scanner;
class Employee {
private String firstName;
private String lastName;
private String id;
private String street;
private String city;
private String state;
public Employee() {
}
public Employee(String aFirstName, String
aLastName, String aId, String aStreet, String aCity, String aState)
{
super();
firstName = aFirstName;
lastName = aLastName;
id = aId;
street = aStreet;
city = aCity;
state = aState;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String aFirstName) {
firstName = aFirstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String aLastName) {
lastName = aLastName;
}
public String getId() {
return id;
}
public void setId(String aId) {
id = aId;
}
public String getStreet() {
return street;
}
public void setStreet(String aStreet) {
street = aStreet;
}
public String getCity() {
return city;
}
public void setCity(String aCity) {
city = aCity;
}
public String getState() {
return state;
}
public void setState(String aState) {
state = aState;
}
@Override
public String toString() {
return "FirstName : " + firstName +
"\nLastName : " + lastName + "\nId : " + id + "\nStreet : " +
street
+ "\nCity : " + city + "\nState : " +
state;
}
}
class HourlyEmployee extends Employee {
int hoursWorked;
double hourRate;
public HourlyEmployee() {
}
public HourlyEmployee(String aFirstName, String
aLastName, String aId, String aStreet, String aCity, String
aState,
int
aHoursWorked, double aHourRate) {
super(aFirstName, aLastName, aId,
aStreet, aCity, aState);
hoursWorked = aHoursWorked;
hourRate = aHourRate;
}
// returns earnings for that employee
public double earnings() {
return hourRate *
hoursWorked;
}
public int getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(int aHoursWorked)
{
hoursWorked = aHoursWorked;
}
public double getHourRate() {
return hourRate;
}
public void setHourRate(double aHourRate) {
hourRate = aHourRate;
}
public String toString() {
return super.toString() + "\nHours
Worked : " + hoursWorked + "\nPay Rate : " + hourRate + "\nEarnings
: "
+ earnings();
}
}
public class TestEmp {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter emp1
first name");
String fname = sc.nextLine();
System.out.println("Enter emp1 last
name");
String lname = sc.nextLine();
System.out.println("Enter emp1
Id");
String id = sc.nextLine();
System.out.println("Enter emp1
street");
String street =
sc.nextLine();
System.out.println("Enter emp1
City");
String city = sc.nextLine();
System.out.println("Enter emp1
state");
String state = sc.nextLine();
System.out.println("Enter Emp1
hours and hour rate");
int hours = sc.nextInt();
double rate =
sc.nextDouble();
HourlyEmployee h1 = new
HourlyEmployee(fname, lname, id, street, city, state, hours,
rate);
System.out.println("Enter emp2
first name");
fname = sc.nextLine();
fname = sc.nextLine();
System.out.println("Enter emp2 last
name");
lname = sc.nextLine();
System.out.println("Enter emp2
Id");
id = sc.nextLine();
System.out.println("Enter emp2
street");
street = sc.nextLine();
System.out.println("Enter emp2
City");
city = sc.nextLine();
System.out.println("Enter emp2
state");
state = sc.nextLine();
System.out.println("Enter emp2
hours and hour rate");
hours = sc.nextInt();
rate = sc.nextDouble();
sc.close();
HourlyEmployee h2 = new
HourlyEmployee(fname, lname, id, street, city, state, hours,
rate);
System.out.println("Employee 1
earnings : " + h1);
System.out.println("Employee 2
earnings : " + h2);
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me