Question

In: Computer Science

Create a class named Employee and its child class named Salesperson. Save each class in its...

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

Solutions

Expert Solution

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


Related Solutions

Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
This assignment will test your knowledge and skills in C++. Create a class named employee and...
This assignment will test your knowledge and skills in C++. Create a class named employee and have the data members of: Name ID Salary Create a class named manager that inherits the employee class and adds the data members: Managed_Employees (Array of up to 3 employees) Department Create methods to update each data member in the employee class. Create a method to print out the managed employees sorted by their salary in the manager class. (You can use one of...
Create a Java class named ReadWriteCSV that reads the attached cis425_io.txt file that you will save...
Create a Java class named ReadWriteCSV that reads the attached cis425_io.txt file that you will save in your workspace as src/cis425_io.txt and displays the following table: -------------------------- | No | Month Name | Days | -------------------------- | 1 | January | 31 | | 2 | February | 28 | | 3 | March | 31 | | 4 | April | 30 | | 5 | May | 31 | | 6 | June | 30 |. | 7...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Use the Date class to create an object for date hired. A faculty member has office hours and a rank. A...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
In java: -Create a class named Animal
In java: -Create a class named Animal
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT