gpt4 book ai didi

java - Employee 和 EmployeeDemo,如何获得两个独立员工的以下输出?

转载 作者:行者123 更新时间:2023-12-02 09:39:51 25 4
gpt4 key购买 nike

我必须创建一个 Employee 类和一个使用它的演示类。我有以下内容:

员工类别:

public class KNW_Employee
{
private int empID;
private double payRate;
private double weeklyHrs;

public KNW_Employee()
{
this.empID = empID;
this.payRate = payRate;
this.weeklyHrs = weeklyHrs;
}

public KNW_Employee(int empID)
{
this.empID = empID;
}

public KNW_Employee(int empID, double payRate, double weeklyHrs)
{
this.empID = empID;
this.payRate = payRate;
this.weeklyHrs = weeklyHrs;
}

public void setID(int empID)
{
this.empID = empID;
}

public void setPayRate(double payRate)
{
this.payRate = payRate;
}

public void setWeeklyHrs(double weeklyHrs)
{
this.weeklyHrs = weeklyHrs;
}

public int getID()
{
return empID;
}

public double getPayRate()
{
return payRate;
}

public double getWeeklyHrs()
{
return weeklyHrs;
}

public double getWeeklyPay()
{
return weeklyHrs * payRate;
}

public String toString()
{
return "ID for First Employee: " + getID() +
"\nPayRate for First Employee: " + getPayRate() +
"\nWeekly Hours for First Employee: " + getWeeklyHrs() +
"\nWeekly Pay for First Employee: " + getWeeklyPay() +
"\n\nID for Second Employee: " + getID() +
"\nPayRate for Second Employee: " + getPayRate() +
"\nWeekly Hours for Second Employee: " + getWeeklyHrs() +
"\nWeekly Pay for Second Employee: " + getWeeklyPay();
}
}

员工演示:

class EmployeeDemo
{
public static void main (String[]args)
{
//Create scanner object
Scanner input = new Scanner(System.in);

//Declare variables
int emp1;
int emp2;
double hour1;
double hour2;
double pay1;
double pay2;

//Ask for employee's ID
System.out.println("Enter ID for Employee 1: ");
emp1 = input.nextInt();
System.out.println("Enter ID for Employee 2: ");
emp2 = input.nextInt();

//Ask for employee's hours
System.out.println("Enter Employee 1's Hours: ");
hour1 = input.nextDouble();
System.out.println("Enter Employee 2's Hours: ");
hour2 = input.nextDouble();

//Ask for employee's pay
System.out.println("Enter Employee 1's Pay: ");
pay1 = input.nextDouble();
System.out.println("Enter Employee 2's Pay: ");
pay2 = input.nextDouble();

//Call Employee Class
KNW_Employee e = new KNW_Employee();
e.getID();
e.getWeeklyHrs();
e.getPayRate();

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

我只需要为 2 个不同的员工打印 toString() 。它允许我输入数据,但一旦打印出数据,我就会不断得到零。有人有什么想法吗?

最佳答案

您没有设置employee对象的值,这需要按照下面带有内嵌注释的代码所示来完成:

    //create employee objects first
KNW_Employee emploee1 = new KNW_Employee();
KNW_Employee emploee2 = new KNW_Employee();


//Ask for employee's ID
System.out.println("Enter ID for Employee 1: ");
emp1 = input.nextInt();
emploee1.setID(emp1);//set the id value for employee1
System.out.println("Enter ID for Employee 2: ");
emp2 = input.nextInt();
emploee2.setID(emp2);//set the id value for employee2

//Ask for employee's hours
System.out.println("Enter Employee 1's Hours: ");
hour1 = input.nextDouble();
emploee1.setWeeklyHrs(hour1);
System.out.println("Enter Employee 2's Hours: ");
hour2 = input.nextDouble();
emploee1.setWeeklyHrs(hour2);

//Ask for employee's pay
System.out.println("Enter Employee 1's Pay: ");
pay1 = input.nextDouble();
emploee1.setPayRate(pay1);
System.out.println("Enter Employee 2's Pay: ");
pay2 = input.nextDouble();
emploee1.setPayRate(pay2);

System.out.println(emploee1);//print employee1 (this calls toString())
System.out.println(emploee2);//print employee2 (this calls toString())
<小时/>

另一个重要的一点是,您不需要显式调用 employee 对象上的 toString() 方法,而是在使用 System.out 时调用.println(emploee1),隐式调用了toString(),可以引用here关于这一点(下面复制的文字,强调我的)。

The Object's toString() method returns a String representation of theobject, which is very useful for debugging. The String representationfor an object depends entirely on the object, which is why you need tooverride toString() in your classes. You can use toString() along with > System.out.println() to display a text representation of an object, > such as an instance of Book:

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

关于java - Employee 和 EmployeeDemo,如何获得两个独立员工的以下输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43445251/

25 4 0