我正在尝试创建一个测试人员类,但在这样做时我遇到了一个错误,指出:
1 error found:
Error: The method printData(double, double) in the type PayCalculator is not
applicable for the arguments ()
我该如何解决?这是我的测试员代码
PayCalculator p1 = new PayCalculator();
p1.setHourlyRate(8.25);
p1.setHoursWorked(45.0);
p1.printData();
我的主要代码
{
private double hourlyRate;
private double hoursWorked;
public PayCalculator()
{
hourlyRate = 0.0;
hoursWorked = 0.0;
}
/**
* Two parameter constructor
* Add hourlyRate and hoursWorked
* @param the hourly rate
* @param the hours worked
*/
public PayCalculator(double aHourlyRate, double aHoursWorked)
{
hourlyRate = aHourlyRate;
hoursWorked = aHoursWorked;
}
/**
* sets the hourly rate
* @return hourlyRate
*/
public void setHourlyRate(double aHourlyRate)
{
hourlyRate = aHourlyRate;
}
/**
* gets the hourly rate
* @param hourlyRate
*/
public double getHourlyRate()
{
return hourlyRate;
}
/**
* sets the hours worked
* @return hoursWorked
*/
public void setHoursWorked(double aHoursWorked)
{
hoursWorked = aHoursWorked;
}
/**
* gets the hours worked
* @param hours worked
*/
public double getHoursWorked()
{
return hoursWorked;
}
public boolean workedOvertime()
{
if (hoursWorked > 40)
{
return true;
}
else
{
return false;
}
}
public double numHoursOvertime()
{
if (hoursWorked > 40)
{
return hoursWorked - 40;
}
else
{
return 0;
}
}
public double calculateGrossPay()
{
if (hourlyRate <= 10.25)
{
if (hourlyRate <= 40)
return hourlyRate * hoursWorked;
}
else
{
double grossPay = ((40 * hourlyRate) + ((hourlyRate * 2) * hoursWorked - 40));
return grossPay;
}
if (hoursWorked <= 60)
{
return hourlyRate * hoursWorked;
}
else
{
return 60 * hourlyRate;
}
}
public double determineTaxRate(double grossPay)
{
if (grossPay >= 800)
{
double tax = 0;
tax = grossPay * 0.37;
return tax;
}
else if ( grossPay >= 400)
{
double tax = 0;
tax = grossPay * 0.22;
return tax;
}
else
{
double tax = 0;
tax = grossPay * 0.12;
return tax;
}
}
public double calculateNetPay(double grossPay, double tax)
{
double calculateNetPay = grossPay - tax;
return calculateNetPay;
}
public void printData(double grossPay, double tax)
{
System.out.println("Hours Worked: " + hoursWorked);
System.out.println("Hourly rate: " + hourlyRate);
System.out.println("Number of hours of overtime worked: " + numHoursOvertime());
System.out.println("Worked overtime? " + workedOvertime());
System.out.println("Gross pay: " + calculateGrossPay());
System.out.println("Tax Rate: " + determineTaxRate(grossPay));
System.out.println("Net Pay: " + calculateNetPay(grossPay, tax));
}
}
答案 0 :(得分:2)
是的。这是方法声明:
public void printData(double grossPay, double tax)
以下是你试图称之为:
p1.printData();
您希望将哪些值用于grossPay
和tax
?
老实说,你应该在grossPay
已经指定工作小时数和小时费率的情况下传递,这似乎很奇怪,但这是另一回事。最直接的问题是你没有提供你的方法所需的参数。
答案 1 :(得分:0)
您的方法需要两个值grossPay
和tax
:
public void printData(double grossPay, double tax)
但是,在调用它时,您并未向其提供grossPay
和tax
:
p1.printData();
在调用您的方法时包含grossPay
和tax
的值,它可以正常工作:
p1.printData(123.00,2.5);
答案 2 :(得分:0)
您的方法的定义是:
public void printData(double grossPay, double tax)
它期望使用两个双参数调用,而您调用时不带参数:
p1.printData();
将双重参数传递给printData(44.85d, 77.56d)