In: Computer Science
Create a class named Employee and its child class named Salesperson. Save each class in its
own file. Name your class and source code file containing the main method
homework.java.
Make sure each class follows these specifications:
1. An employee has a name (String), employee id number (integer), hourly pay rate
(double), a timesheet which holds the hours worked for the current week (double array)
and email address (String). A salesperson also has a commission rate, which is a
percentage stored as decimal value (double).
2. Override the toString method in each class to display all data (except the timesheet array)
available about each object.
3. Each class should have an appropriate constructor to set the values of all private data
members belonging to that class and its parent classes at the time of instantiation.
4. Each class should have get and set methods for their unique private data members.
5. For the timesheet array, it is an array which stores 1 week of hours worked since
employees are paid every week on Monday. Weeks start on Monday and run through
Sunday so Monday hours are stored in the first position of the array and Sunday hours are
stored in the last position of the array.
6. Design a calculatePay method for each type of employee as follows:
A regular employee gets paid their hourly rate for their first 40 hours of work in a
week and then gets 1.5 times their hourly rate for any hours worked over 40 in a
week.
A salesperson gets their pay calculated the same as the regular employee (aka regular
pay) with an addition of their sales commission earned. Their sales commission is
earned by multiplying the salesperson's commission rate by the weekly sales total,
which is supplied as an argument when the method is called. The sales commission
earned for the week is then added to the regular pay to calculate the salesperson's total
pay for the week.
This method uses hours worked in the timesheet array and the hourly rate to make its
calculations of the regular pay for all types of employees, even salespeople.
Salespeople just have the additional sales commission added to their pay.
All employees have 35% of their total pay withheld from their checks for taxes.
Page 2 of 2
This method should return a double representing the employee's total pay for the
week accounting for sales commission (if any) and taxes withheld.
After creating the two classes, write a program with a main method that uses the Employee and
Salesperson classes to create objects for an employee and a salesperson being sure to assign data
to all data members including those inherited from the parent class. You can use any data you
wish for the objects, but you must use the constructors you defined to set the data values at the
time of object instantiation.
After creating the two objects, your program should then do the following things in order:
Invoke the toString method for each object.
Print the timesheet for the current pay period for each object.
Change the email address of the salesperson.
Print the saleperson's name and changed email address
Change the commission rate for the salesperson increasing it by 5%.
Invoke the toString method for the salesperson to show the change in commission rate.
Print the paycheck amounts for the current pay period for both objects.
For this assignment, you should end up with different source code files for each of the 2 classes
(Employee and Salesperson). The main method should also be in a different source code file
named homework.java
Hi,
Please find the answer below:
Solution:
--------
Java Classes:
package questions;
public class Employee {
private String name;
private int employeeId;
private double hourlyPayRate;
private double[] timesheet;
private String emailAddress;
/**
* @param name
* @param employeeId
* @param hourlyPayRate
* @param timesheet
* @param emailAddress
*/
public Employee(String name, int employeeId, double
hourlyPayRate,
double[]
timesheet, String emailAddress) {
super();
this.name = name;
this.employeeId = employeeId;
this.hourlyPayRate =
hourlyPayRate;
this.timesheet = timesheet;
this.emailAddress =
emailAddress;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the employeeId
*/
public int getEmployeeId() {
return employeeId;
}
/**
* @param employeeId the employeeId to set
*/
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
/**
* @return the hourlyPayRate
*/
public double getHourlyPayRate() {
return hourlyPayRate;
}
/**
* @param hourlyPayRate the hourlyPayRate to set
*/
public void setHourlyPayRate(double hourlyPayRate)
{
this.hourlyPayRate =
hourlyPayRate;
}
/**
* @return the timesheet
*/
public double[] getTimesheet() {
return timesheet;
}
/**
* @param timesheet the timesheet to set
*/
public void setTimesheet(double[] timesheet) {
this.timesheet = timesheet;
}
/**
* @return the emailAddress
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* @param emailAddress the emailAddress to set
*/
public void setEmailAddress(String emailAddress)
{
this.emailAddress =
emailAddress;
}
@Override
public String toString() {
return "Employee [name=" + name +
", employeeId=" + employeeId
+ ", hourlyPayRate=" +
hourlyPayRate + ", emailAddress="
+ emailAddress + "]";
}
// Method to calculate pay.
public double calculatePay(){
double totalWorkhours=0.0;
double totalPay=0.0;
double taxesWithheld=0.0;
//get the total work hours
for(int
i=0;i<timesheet.length;i++){
totalWorkhours+=
timesheet[i];
}
//Calculate Pay
// If hours exceed 40 pay would be
1.5*extra hours than 40
if(totalWorkhours < 40){
totalPay =
totalWorkhours*this.hourlyPayRate;
}
else{
totalPay =
40*this.hourlyPayRate + (totalWorkhours
-40)*1.5*this.hourlyPayRate;
}
taxesWithheld =
0.35*totalPay;
return Math. round((totalPay -
taxesWithheld) * 100.0) / 100.0;
}
}
//////////////////////////
package questions;
public class Salesperson extends Employee {
private double commissionRate;
/**
* @param name
* @param employeeId
* @param hourlyPayRate
* @param timesheet
* @param emailAddress
* @param commissionRate
*/
public Salesperson(String name, int employeeId, double
hourlyPayRate,
double[]
timesheet, String emailAddress, double commissionRate) {
super(name, employeeId,
hourlyPayRate, timesheet, emailAddress);
this.commissionRate =
commissionRate;
}
/**
* @return the commissionRate
*/
public double getCommissionRate() {
return commissionRate;
}
/**
* @param commissionRate the commissionRate to
set
*/
public void setCommissionRate(double commissionRate)
{
this.commissionRate =
commissionRate;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Salesperson
[commissionRate=" + commissionRate + "]";
}
// Method to calculate pay for Salesperson.
public double calculatePay(double
weeklySalesTotal){
double totalWorkhours=0.0;
double totalPay=0.0;
double taxesWithheld=0.0;
//get the total work hours
for(int
i=0;i<getTimesheet().length;i++){
totalWorkhours+=
getTimesheet()[i];
}
//Calculate Pay
// If hours exceed 40 pay would be
1.5*extra hours than 40
if(totalWorkhours < 40){
totalPay =
totalWorkhours*getHourlyPayRate();
}
else{
totalPay =
40*this.getHourlyPayRate() + (totalWorkhours
-40)*1.5*this.getHourlyPayRate();
}
totalPay=totalPay +
weeklySalesTotal*this.commissionRate;
taxesWithheld =
0.35*totalPay;
return Math. round((totalPay -
taxesWithheld) * 100.0) / 100.0;
}
}
//////////////////////////
/**
*
*/
package questions;
/**
* @author
*
*/
public class Homework {
/**
* @param args
*/
public static void main(String[] args) {
//Sample timesheets for employee
and saleperson.
double[] empTS =
{1.0,2.0,3.0,4.0,5.0,6.0,7.0};
double[] salesTS
={1.0,2.0,3.0,4.0,5.0,6.0,7.0};
// John @ $8.0
Employee emp =new
Employee("John",001,8.0,empTS,"[email protected]");
// Mark @ $7.0 comm=1.5%
Salesperson sales =new
Salesperson("Mark",001,7.0,salesTS,"[email protected]",0.015);
emp.toString();
sales.toString();
//Print timesheets
printTimesheet(emp);
printTimesheet(sales);
//Calculate Pays
System.out.println("Employee Pay:="
+ emp.calculatePay());
System.out.println("SalesPerson
Pay:=" + sales.calculatePay(2000));
}
public static void printTimesheet(Employee
e){
double[] timesheet=
e.getTimesheet();
String[] days=
{"Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday","Sunday"};
System.out.println("~~~~~~~Timesheet~~~~~~~~~~");
System.out.println("EmpName "+
e.getName());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
for(int
i=0;i<timesheet.length;i++){
System.out.println(days[i] +" " +
timesheet[i]+ " hrs");
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println();
}
}
////////////////////////////
Screenshot:
Hope this helps