QQ登录

只需一步,快速开始

Write a class Employee that represents a single hourly employee and a class P...

[复制链接]
福虎生威 发表于 2022-2-15 13:22:14 [显示全部楼层] 回帖奖励 倒序浏览 阅读模式 0 929
JAVA programming Language:
Write a class Employee that represents a single hourly employee and a class PayCheckthat calculates employee pay for the week. Employees know their names (String) the number of hours they worked in a given week, and their pay rate. PayChecks know an employee name, a rate, and the hours worked. PayChecks calculate the weekly pay based on rate * hours if the hours are 40 or less. If the hours worked exceeds 40, the PayCheck applies an overtime rate of 1.5x for all of the hours in excess of 40. Create:
  • An Employee constructor that takes a name and pay rate as parameters and sets the instance values appropriately. This constructor should also initialize the hours worked to zero (0) upon instantiation.
  • An Employee method addHoursWorked() which takes a parameter (double) and adds the value of that parameter to the current number of hours the Employee has worked this week.
  • An Employee method resetHoursWorked() that resets the employee’s hours worked for the week to zero.
  • An Employee method getWeeklyCheck() which, when called, returns a new PayCheck object that is initialized to the current Employee name, rate and hours worked.
  • A PayCheck constructor that takes the employee name, rate, and hours worked as parameters and calculates (and stores) the total pay for the week.
  • A PayCheck method getTotalPay() that returns the total pay for the week.
  • A toString method for BOTH classes, allowing Employee objects to be represented by the employee name, and PayCheck objects by the totalPay (in proper dollars/cents $xxx.yy format).


  • public class Employee extends PayCheck {    //Employee class is having below three informations    public static String employeeName;    public static double hoursWorked;    public static double payRate;    //Assignment of value to the variable using constructor    public Employee(String EmployeeName, double payRate) {        this.employeeName = EmployeeName;        this.payRate = payRate;        this.hoursWorked = 0.0;    }    public static double addHoursWorked(double addedWorkedHours) {        return hoursWorked = hoursWorked + addedWorkedHours;    }    public static void resetHoursWorked() {        hoursWorked = 0.0;    }    public static PayCheck getWeeklyCheck() {        PayCheck payCheck = new PayCheck(employeeName, payRate, hoursWorked);        return payCheck;    }    // Sample example has been taken just to check the functionality (can be removed from line number 29-34)    public static void main(String[] args) {        Employee employee = new Employee("John", 20);        employee.addHoursWorked(40);        System.out.println(employee.toString());        System.out.println(employee.getWeeklyCheck().toString());    }    @Override    public String toString() {        return employeeName;    }}class PayCheck {    //PayCheck class is having below three informations    public static String employeeName;    public static double payRate;    public static double totalHourWorked;    public PayCheck() {    }    //Assignment of value to the variable using constructor    public PayCheck(String employeeName, double payRate, double totalHourWorked) {        this.employeeName = employeeName;        this.payRate = payRate;        this.totalHourWorked = totalHourWorked;    }    public static double getTotalPay(double payRate, double totalHourWorked) {        double totalPayableAmount = 0.0;        if (totalHourWorked <= 40) { //if it's less than 40 hours            totalPayableAmount = payRate * totalHourWorked;        } else { // if working hour is more than 40. first 40 hour normal payment and 1.5 times for overTime work            totalPayableAmount = payRate * 40 + 1.5 * payRate * (totalHourWorked - 40);        }        return totalPayableAmount;    }    // PayCheck objects to be represented in proper dollars/cents $xxx.yy format    @Override    public String toString() {        return String.format("dollars/cents" + " " + "$" + PayCheck.getTotalPay(payRate, totalHourWorked));    }}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

官方微博
官方微博
模板大全
模板大全
意见
反馈