创建员工的新子类 [英] Creating a new subclass of employee

查看:98
本文介绍了创建员工的新子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(创建新的员工子类)其他类型的员工可能包括按周薪定薪的SalariedEmployees,按照他们生产的件数获得报酬的PieceWorkers或按时间获得按小时工资的HourlyEmployees每小时工资的1.5倍 - 小时工作超过40小时。

创建继承自Employee类(练习9.14)的类HourlyEmployee,并具有表示小时数的实例变量小时(双倍)工作,实例变量工资(一个双倍)代表每小时的工资,一个构造函数,作为参数的名字,姓氏,社会安全号码,小时工资和工作小时数,设置和获取操纵方法小时和工资,根据工作小时数计算HourlyEmployee收入的收入方法和返回HourlyEmployee的String表示的toString方法。方法setWage应该确保工资是非负的,并且setHours应该确保小时值在0到168之间(一周中的总小时数)。

在测试程序中使用类HourlyEmployee类似于图9.5中的那个



我尝试过:



HourlyEmployee.java

公共课HourlyEmployee扩展员工{



私人双倍小时;

私人双倍工资;



HourlyEmployee(String firstname,String lastname,int ssn,double hours,double wages){

super();

super.setFirstname(名字);

super.setLastname(姓氏);

super.setSsn(ssn);

this.hours =小时;

this.wages =工资;

}



public double getHours() {

返回时间;

}



publ ic void setHours(double hours){

if(hours> = 0 ||小时< = 168){

this.hours =小时;

}

}



public double getWages(){

返回工资;

}



public void setWages (双倍工资){

if(工资> = 0){

this.wages =工资;

}

}



public double totalEarning(){

if(this.getHours()< = 40){

return(this.getHours()* this.getWages());

} else {

double hour = this.getHours() - 40;

double total =(40 * this.getWages())+ hour *(工资* 1.5);

返回总额;

}

}



@Override

public String toString(){

returnFirstname :+ super.getFirstname()+姓氏:+ super.getLastname()+工作小时数: + this.getHours()+每小时工资:+ this.getWages()+总工资:+ this.totalEarning();

}

}

main.java

import java.util.Scanner;



public class main {

public static void main(String [] args){

Scanner sc = new Scanner(System.in);



String firtsname,lastname;

int ssn;

双倍小时;

双倍工资;



System.out.println(输入FirstName:);

firtsname = sc.next();

System.out.println(输入LastName:);

lastname = sc.next();

System.out.println(输入SSN:);

ssn = sc.nextInt();

System.out.println(输入小时:);

小时= sc.nextDouble();

System.out.println(输入工资:);

salary = sc.nextDouble( );



每小时雇员他=新的HourlyEmployee(firtsname,lastname,ssn,小时,工资);



System.out.println(he.toString());

}

}

解决方案

< pre lang =java> public void setHours( double 小时){
if (小时> = 0 ||小时< = 168 ){
.hours =小时;
}



您在此处使用了逻辑OR连接器,因此如果小时数大于或等于零,则该值是可接受的。所以5000的价值也是可以接受的。如果该值小于零,或者小于或等于168,则可以接受,因此值为-99将起作用。您应该重做该方法以检查该值是否大于或等于0且小于169.您还应该在该数字无效时为该案例添加一些操作。


(Creating a New Subclass of Employee) Other types of Employees might include SalariedEmployees who get paid a fixed weekly salary, PieceWorkers who get paid by the number of pieces they produce or HourlyEmployees who get paid an hourly wage with time-and-a-half—1.5 times the hourly wage—for hours worked over 40 hours.
Create class HourlyEmployee that inherits from class Employee (Exercise 9.14) and has instance variable hours (a double) that represents the hours worked, instance variable wage (a double) that represents the wages per hour, a constructor that takes as arguments a first name, a lastname, a social security number, an hourly wage and the number of hours worked, set and get methods for manipulating the hours and wage, an earnings method to calculate an HourlyEmployee’s earnings based on the hours worked and a toString method that returns the HourlyEmployee’s String representation. Method setWage should ensure that wage is nonnegative, and setHours should ensure that the value of hours is between 0 and 168 (the total number of hours in a week).
Use class HourlyEmployee in a test program that’s similar to the one in Fig. 9.5

What I have tried:

HourlyEmployee.java
public class HourlyEmployee extends employee{

private double hours;
private double wages;

HourlyEmployee(String firstname, String lastname, int ssn, double hours, double wages){
super();
super.setFirstname(firstname);
super.setLastname(lastname);
super.setSsn(ssn);
this.hours=hours;
this.wages=wages;
}

public double getHours() {
return hours;
}

public void setHours(double hours) {
if(hours>=0 || hours <= 168) {
this.hours = hours;
}
}

public double getWages() {
return wages;
}

public void setWages(double wages) {
if(wages>=0) {
this.wages = wages;
}
}

public double totalEarning(){
if(this.getHours()<=40){
return (this.getHours() * this.getWages());
}else{
double hour = this.getHours() - 40;
double total = (40 * this.getWages()) + hour * (wages * 1.5);
return total;
}
}

@Override
public String toString() {
return "Firstname: "+ super.getFirstname() + " Lastname: " + super.getLastname() + " Hours Worked: "+ this.getHours() + " Wages per hour: " + this.getWages() + " Total wages: " + this.totalEarning();
}
}
main.java
import java.util.Scanner;

public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

String firtsname,lastname;
int ssn;
double hours;
double wages;

System.out.println("Enter FirstName: ");
firtsname = sc.next();
System.out.println("Enter LastName: ");
lastname = sc.next();
System.out.println("Enter SSN: ");
ssn = sc.nextInt();
System.out.println("Enter Hours: ");
hours = sc.nextDouble();
System.out.println("Enter Wages: ");
wages = sc.nextDouble();

HourlyEmployee he = new HourlyEmployee(firtsname,lastname,ssn,hours,wages);

System.out.println(he.toString());
}
}

解决方案

public void setHours(double hours) {
if(hours>=0 || hours <= 168) {
this.hours = hours;
}


You have used the logical OR connector here, so if hours are greater than or equal to zero, then the value is acceptable. So a value of 5,000 would also be acceptable. And if the value is less than zero, OR less than or equal to 168, it would be acceptable, so a value of -99 would work. You should rework the method to check that the value is greater than or equal to 0 AND less than 169. You should also add some action for the case when the number is not valid.


这篇关于创建员工的新子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆